For the longest time to achieve passwordless login on a server I was doing the following:
- manually connected to the server
- created the
.ssh
directory if it didn’t exist - created the
authorized_keys
file inside that directory and pasted the contents of previously copied public key - crucial step was to set the correct permissions:
700
for the directory and600
for the file
But as with many things, there’s a script for that! It’s named ssh-copy-id
and it’s part of the openssh package.
Copying the public key to a server myserver
is as easy as doing this:
ssh-copy-id user@myserver
And if you need to specify a non-default public key just use the -i
option:
ssh-copy-id user@myserver -i ~/.ssh/some_id.pub