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?
5 Replies
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 account@your-ip
Then, you can try logging into your Linode with the following SSH command:
$ ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no account@your-ip
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:
PubKeyAuthentication Yes
#PasswordAuthentication no
Would change to:
PubKeyAuthentication No
PasswordAuthentication Yes
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 root@your-ip
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:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC3tvOQFGAnY3p1t6gv6rXEat8maN
YghZYuAuci3Pd0gEr3MHMFwZ3NqYA87VM+HLbu9EbBjvPjuFmNkdT7yN8TJkv1Z61g
+NJ3+aJBGHNe8MDKs69z3yNgakiI2ynT8+GDOz545fQfZdyl5oQ9IvcODz0k7yoKP9
yQdSj8l9dCN9Zf8GBLQTbryHgaSEoinpX5SFmNkdT7yN8TJkv1Z61gpB+NJ3+aJBGH
Jvl72P8ePqG2nIvSqHsm/4OfdJshaXHA+j6DpvSQ== user@users-laptop.local
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
Type 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
More information
Use Public Key Authentication with SSH - Linode
Configure SSH key based secure authentication - SSH.com
This is great! I had the same issue …Logging in with Lish and editing my config worked instantly. Thanks a lot.
You can also get this error if you have explicitly denied remote logins by the superuser using ssh(1) by adding:
PermitRootLogin no
to /etc/sshd_config.
stevewi:~ $ ssh root@dave
root@dave: Permission denied (publickey).
-- sw