Pete Freitag Pete Freitag

Setting up public key authentication over SSH

Updated on June 25, 2024
By Pete Freitag
linuxmiscapple

Every time I want to setup public key authentication over SSH, I have to look it up, and I've never found a simple guide, so here's mine.

Generate key on local machine

ssh-keygen -t rsa

It will ask you for a password, you can leave it blank if you do not want to be prompted for the password every time you login. If you do this, understand that this means the private key file is as good as a password.

You can also specify a key size, like this:

ssh-keygen -t rsa -b 4096

Note you could also pick -t ed25519 which offers better performance, but may not be compatible with older systems.

Ensure that the remote server has a .ssh directory

Make sure the server your connecting to has a .ssh directory in your home directory. If it doesn't exist you can run the ssh-keygen command above, and it will create one with the correct permissions.

Copy your local public key to the remote server

If your remote server doesn't have a file called ~/.ssh/authorized_keys2 then we can create it. If that file already exists, you need to append to it instead of overwriting it, which the command below would do:

scp ~/.ssh/id_rsa.pub remote.server.com:.ssh/authorized_keys2

You should also make sure that the file permissions on the authorized_keys2 file is correct. You can do this by running the command:

chmod 600 ~/.ssh/authorized_keys2

If the file is writable by any user other than the owner it SSH will not use the file, and skip public key authentication.

Now ssh to the remote server

Now you can ssh to the remote server without entering your password.

Security

Now keep in mind that all someone needs to login to the remote server, is the file on your local machine ~/.ssh/id_rsa, so make sure it is secure.

To keep up to date on the latest OpenSSH vulnerabilities, try stack.watch.



ssh authentication rsa unix

Setting up public key authentication over SSH was first published on January 18, 2006.

If you like reading about ssh, authentication, rsa, or unix then you might also like:

Weekly Security Advisories Email

Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).

Comments

Thanks for the simple, straightforward tutorial. I hate looking through pages of text just to find out how to do something simple.
by Jonathan Haddad on 12/16/2006 at 1:04:01 PM UTC
Good guide, but found the file on server needed to be authorized_keys (not authorized_keys2).

Also, in response to harry who commented that there is no need to secure the file because it's a public key: You absolutely need to make sure this file is at least not writable by any other users (or they can simply append their keys to the file). As for why it shouldn't be readable by other users, that's just the first rule of security. If they don't need access, they don't have access.
by Rhys on 02/14/2010 at 3:42:10 PM UTC
Correcct command
scp ~/.ssh/id_rsa.pub remote.server.com:~/.ssh/authorized_keys2
by Oleg on 11/19/2010 at 8:16:33 AM UTC
not sure that I have done this correct I have the keys in /home/admin/.shh/authorized_keys
what goes in the /etc/sshd_config file and where does it go please
I have chmod 600 the files in .ssh

thanks
tim
by tim smy on 03/02/2011 at 5:34:47 PM UTC
Worked on my Snow Leopard install and Fedora Linux box. BUT, needed to do this to get it to work - thanks Niel!

server$ chmod 700 ~/.ssh server$ chmod 600 ~/.ssh/authorized_keys
by aeht on 03/18/2011 at 3:18:37 AM UTC
make sure the permissions for
./ssh - 700
~/.ssh/authorized_keys - 644
by Jayaprakash on 04/29/2011 at 5:52:20 AM UTC
guys,

regarding authorized_keys or authorized_keys2 .. please check your sshd server config file , i.e. /etc/ssh/sshd_config , prameter AuthorizedKeysFile. It will tell you what file your sshd server is using:)))
by Peter on 10/20/2011 at 5:49:52 PM UTC