Setup public key ssh login and troubleshooting

Short note on how to setup ssh to login without a password

Generate and copy key

ssh-keygen -t rsa

Copy key

ssh username@remotehost mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh username@remotehost 'cat >> .ssh/authorized_keys'

Try logging in!

If that doesn’t work

Some host need proper permission and a second authorized_keys2

At the remote host

chmod 700 .ssh
cd .ssh
cp authorized_keys authorized_keys2
chmod 600 authorized_keys
chmod 600 authorized_keys2

SSH may settings may need to be modified

At the remote host

cd /etc/ssh
sudo vi sshd_config

Find the AuthorizedKeysFile line and uncomment it (or add it if it doesn’t exist)

AuthorizedKeysFile %h/.ssh/authorized_keys

Save file, restart sshd with

sudo service sshd restart

Further troubleshooting

Start a second instance of sshd on the remote machine, dump all output to console so you can diagnose

/usr/sbin/sshd -d -p 2222

On your machine, do

ssh -p 2222

Look for Authentication refused: in the logs, it should contains the reason your connection fails

Leave a Reply

Your email address will not be published. Required fields are marked *