Passwordless SSH logins using private keys and .ssh/config

These instructions will work on any computer that uses OpenSSH such as macOS or most Linux/Unix systems. The process for generating keys on other platforms will vary but the general principle is the same.

  • Open your Terminal app and generate a new key pair by typing ssh-keygen at the shell prompt. You should see:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):

Press Enter to confirm the default location (that is, ~/.ssh/id_rsa) for the newly created key and the press enter twice more when prompted for a passphrase.

  • After this, you will be presented with a message similar to this:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is:
e7:97:c7:e2:0e:f9:0e:fc:c4:d7:cb:e5:31:11:92:14 alfie@example.com
The key's randomart image is:
+--[ RSA 2048]----+
|             E.  |
|            . .  |
|             o . |
|              . .|
|        S .    . |
|         + o o ..|
|          * * +oo|
|           O +..=|
|           o*  o.|
+-----------------+
  • Change the permissions of the ~/.ssh/ directory to 700 to ensure it is only accessible by your user ID:
$ chmod 700 ~/.ssh
  • Copy the content of ~/.ssh/id_rsa.pub into the ~/.ssh/authorized_keys on the machine to which you want to connect, appending it to its end if the file already exists.
  • Set the permissions of the ~/.ssh/authorized_keys file using the following command:
$ chmod 600 ~/.ssh/authorized_keys

How to create an SSH shortcut

To create a custom ssh connection so you don’t need to type the IP address of your server every time, type from your home directory:

nano .ssh/config

then enter the following below

Host shortcutname
HostName 10.11.12.13
IdentityFile ~/.ssh/id_rsa
PreferredAuthentications publickey
Port 22
User root

Save the file and exit nano. You can now connect to the server using the command ssh shortcutname without having to enter any additional connection information or password.