https://askubuntu.com/questions/311558/ssh-permission-denied-publickey
PubKeyAuthentication
Set up your client
Generate your key.
ssh-keygen
Configure ssh to use the key.
vim ~/.ssh/config
Your config file should have something similar to the following:
Host SERVERNAME Hostname ip-or-domain-of-server User USERNAME PubKeyAuthentication yes IdentityFile ./path/to/key
You can add
IdentitiesOnly yes
to ensuressh
uses the specifiedIdentityFile
and no other keyfiles during authentication. SettingIdentitiesOnly
prevents failed authentications from occurring, whenssh
would otherwise attempt to login with multiple keys. Setting this is also considered more secure, as you're not leaking information about other keys you have installed, and maintaining separation of your keys between different levels of access.Copy your key to your server.
ssh-copy-id -i /path/to/key.pub SERVERNAME`
For example,
ssh-copy-id -i ~/.ssh/id_res.pub -p 22 user@1.1.1.1
Troubleshooting
use "-vvv" option
Make sure the server has your PUBLIC key (.pub).
Make sure your IdentiyFile points to your PRIVATE key.
Make sure your
.ssh
directory has 700 and the files within are 600 permissions.
ssh-keygen
will create files and directories for you with the proper permissions
tail -f /var/log/auth.log
(on the server) and monitor errors when you attempt to loginIf you have many key files, try
IdentitiesOnly yes
to limit the authentication to use the single, specified key.
Follow
20.6k77 gold badges4444 silver badges7171 bronze badges
answered Jun 23, 2013 at 21:04
11.1k11 gold badge3636 silver badges6060 bronze badges
4
FYI, I created a small script at github.com/centic9/generate-and-send-ssh-key which runs the necessary steps in one go and additionally ensures all the file/directory permissions which always caused me headaches…
– centic
6
Just to elaborate step 2: the
IdentityFile
line in ~/.ssh/config must point to the PRIVATE key.1
6
I wonder why you'd want to set files to have execute permission in step 4?
1
It's also very important right permissions per user (use chown and chmod) otherwise you will get an authentication denied even if you server has your public key.
1
Why does every question like this contain an answer that tells you to start by generating the key using
ssh-keygen
? OP already said he has a public and private key.2
Posterity and completeness is why.
6
While the directory requires 700 permissions, the files do not, they should be 600.
tail -f /var/log/auth.log
gave the required info for meauthorized_keys
needs644
I think.– KuN
600 on
authorized_keys
is the default and all you need under most circumstances.