I get the error 'Permission denied (publickey)' when I connect with SSH.
I'm trying to connect to my Linode with SSH but I get this error:
Permission denied (publickey)
How can I get around this?
If you're getting the Permission denied (publickey) error when connecting to your Linode with SSH, one of three things may be happening.
- You don't have the matching key on your local machine.
- You have too many keys on your local machine.
- Your key isn't listed in the authorized_keys file on the Linode (or the file doesn't exist).
First, check to see if you can find out why you can't connect. This command will tell you everything that SSH is trying to do and trying to check when accessing your Linode:
$ ssh -vvv [email protected]
Then, you can try logging into your Linode with the following SSH command:
$ ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]
This command tells SSH to prefer passwords over public keys. It only works if you have both PubKeyAuthentication and PasswordAuthentication enabled -- and it won't work with PuTTy, unfortunately.
If you don't have the matching key on your local machine, then you'll need to:
-- Enable password authentication on your server.
-- (optional) Disable public key authentication on your server.
-- Restart the SSH server.
First, you will need to log in to your Linode via the Lish console.
Once logged in, you'll need to edit your /etc/ssh/sshd_config file. To use Nano to edit it, run this command (use 'sudo' if not logged in as root):
$ (sudo) nano /etc/ssh/sshd_config
Make the following changes (disabling PubKeyAuthentication is optional). Don't forget to remove
# symbols before each value that you are changing.
So, for example:
Would change to:
When you're done, hit CTRL+O to save and CTRL+X to exit Nano. You'll just need to restart the SSH server with one of the following commands.
$ systemctl restart sshd $ service sshd restart
Now you should be able to try and connect with SSH. If you're still having trouble, take a look at the changes you made and make sure everything looks right -- the
# is removed before each key and there are no extra spaces or anything.
If you have too many keys on your local Machine, then you can try specifying which key you want to use:
$ ssh -i /path/to/key/id_rsa [email protected]
If your key isn't in the authorized_keys file (or the file doesn't exist) on your Linode
To fix this, you'll need to manually insert your public key into the authorized_keys file on your Linode (or create the file and then insert it).
If you haven't generated a public key, you can try disabling the public key authentication and then enabling password authentication using the instructions above. Otherwise, you can generate a key using these instructions.
First, log in to your Linode via the Lish console.
Next, you need to view the contents of your public key file (usually, id_rsa.pub). On MacOS, it should be found in /Users/[username]/.ssh/ . On Linux systems, it should be found in /home/[username]/.ssh/. To view, just type:
$ cat /Users/[username]/.ssh/id_rsa.pub
On Windows systems, it usually you would most likely have chosen the location for this file. To view it, you can typically open the file with Notepad.
A valid key will look something like this:
Keep this file open for now.
Next, verify whether the key is in the authorized_keys file on your Linode.
Open a Lish console and log in (with the same user you use when logging in via SSH).
View your authorized_keys file with the following command:
$ cat ~/.ssh/authorized_keys
You can visually compare them or use a tool like diffchecker -- just remember that there could be multiple keys listed in the authorized_keys file. Each line contains a single public key (word wrapping may make it look like the key takes up several lines).
If they don't match, or your key isn't listed in the file, you will need to add it.
Go back to the window with your public key, and select the entire key (from 'ssh-rsa' to 'users-laptop.local') and then copy it to your clipboard. They key should be on a single line (word wrap may look like it is on multiple lines).
If the authorized_keys file doesn't exist or you received an error when trying to view it, we need to confirm that you have the .ssh folder and the authorized_keys file on your Linode.
$ ls ~/.ssh/ $ ls ~/.ssh/authorized_keys
If you receive an error from either of these commands, you will need to create the folder or file.
To create the .ssh folder:
$ mkdir -p ~/.ssh $ chmod 700 ~/.ssh
To create the authorized_keys file:
$ touch ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys
Next, open the authorized_keys file in VIM.
$ vim ~/.ssh/authorized_keys
shift+g to go to the end of the file, and then type
shift+a to edit the file. Press
return/enter to create a new line. Next, paste your public key into the authorized_keys file. Remember, each key takes up one line (word wrapping may make it look like this takes up multiple lines).
Finally, hit the
escape key, and then type
:wq! to save the file and close VIM.
Last but not least, restart the SSH server with one of the following commands.
$ systemctl restart sshd $ service sshd restart
This is great! I had the same issue …Logging in with Lish and editing my config worked instantly. Thanks a lot.