Permission denied (publickey)

Posted July 24, 2019 3.4k views
ApplicationsUbuntu 18.04

good afternoon, i generated a ssh key into remote server and now i cannot login, it say: Permission denied (publickey). Can i recover this server?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

If you haven’t disabled password logins to your droplet, you could try:

ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no <your_droplet_address>

This will make ssh skip sending the key and the server will prompt for username / password.

If you can’t log in with a username and password any more, your only option is to use the console:

  • Step 1: enable password login on server side for temporary. We’ll change it back in step 4.

    Need to run from REMOTE SERVER

    echo "PasswordAuthentication yes # TO REMOVE ME" | sudo tee -a /etc/ssh/sshd_config
    sudo systemctl restart ssh
  • Step 2: upload SSH key file from localhost to server.

    Need to run from LOCAL HOST

    ssh-copy-id <user_id>@<server_ip_address>

    You should be asked for input the remote user’s login password.

  • Step 3: test if the SSH key is working.

    Need to run from LOCAL HOST

    ssh <user_id>@<server_ip_address>

    It should be logged in without login password.

  • Step 4: restore server side SSH daemon’s setting.

    Need to run from REMOTE SERVER

    sed -e '^PasswordAuthentication yes # TO REMOVE ME$' -i /etc/ssh/sshd_config
    sudo systemctl restart ssh

Hello there,

You can check our article on How to Upload an SSH Public Key to an Existing Droplet

You can access the droplet from the DigitalOcean console and then temporary enable the PasswordAuthentication on your droplet and access the droplet with a password to upload the ssh-key.

If you haven’t created new pair of keys you’ll need to do that first.

You can enable PasswordAuthentication for your Droplet by modifying your /etc/ssh/sshd_config file. Once set to Yes restart the SSH service and connect via an SSH client for a more stable connection. You can then modify your ~/.ssh/authorized_keys file to add the appropriate public key.

This change can be made from the DigitalOcean’s console. If you’re having issues accessing the console you can then reach to our amazing support team that can help you further with this.

To enable the PasswordAuthentication follow these steps:

  1. Login to the console on the DigitalOcean website.
  2. Type sudo nano /etc/ssh/sshd_config
  3. Change PasswordAuthentication from “no” to “yes” and save the file
  4. Open a terminal on your computer and type ssh username@[hostname or IP address] or if on a Windows box use PuTTY for password login making sure authentication parameters aren’t pointing to a private key
  5. Login with a password
  6. Type sudo nano ~/.ssh/authorized_keys
  7. Paste public key text here and save the file
  8. Type sudo nano /etc/ssh/sshd_config
  9. Change PasswordAuthentication from “yes” to “no” and save the file
  10. Log out and attempt to log back in (if using PuTTY make sure you set up auth parameters to point to your private key)

You can then upload the key using this command:

ssh-copy-id -i ~/.ssh/mykey user@droplet

Hope that this helps!