SSH Key-Pair Authentication

Increase your server security by configuring the login to using SSH key-pair authentication. Key authentication allows for an automated encrypted login procedure which can be used across SSH servers. No more remembering passwords, just one public and one private key to manage your login(s). All more secure than those long and complicated passwords.

So let’s get rid of that password authentication and start configuring SSH key-pair authentication.

Generate a key pair

First step is to generate the key pair. To generate the key pair, enter the following command at the terminal:

ssh-keygen -t rsa

Hit enter to accept the path:

Enter file in which to save the key:

Next you are prompted to enter a passphrase as an additional security for your key. You have the option to enter one or leave it blank.

Enter passphrase (optional):

If you leave the passphrase blank, you will not be prompted for a passphrase when you log in using the key. If you entered a passphrase, you need both the key and the passphrase to log in which makes the key even more secure.

When everything is successful, you should see the following outcome:

Successful creation of your private and public key (id_rsa and id_rsa.pub)

This generated a private key id_rsa and a public key id_rsa.pub in the .ssh directory of your  logged in user’s home directory.

cd .ssh followed by ls -a to display the id_rsa and the id_rsa.pub file

Install the public key

Next step is to install the public key onto the server. The easiest way to do this is to run ssh-copy-id.

Type ssh-copy-id followed by the user for which you want to use the key, an @ sign and the server’s ip:

ssh-copy-id demouser@10.0.18.228

An authentication message will appear prompting if you want to continue connecting. Type yes and press enter.

Accept fingerprint and continue connecting?

Now enter the password of the specified user in the ssh-copy-id command.

Enter the password of the connecting user.

If everything is ok, you should get the following message:

Number of keys added successfully

If you log onto the server, go to the user’s home directory and into the directory .ssh, you should see 2 files called authorized_keys and known_hosts.

ls -a to check for files authorized_keys and known_hosts

SSH Key-Pair Authentication from Windows Client (Putty)

Transfer the private key to your Windows client first. My preference is to use WinSCP.

Navigate to the location where the private key (id_rsa) is located and download it to your computer.

WinSCP private key file transfer

Convert your private key for use with Putty

Open PuTTYgen to convert the private key.

Start PuTTYgen from the Windows start menu.

Click on “Load” and select the private key (id_rsa).

Putty load private key by clicking the load button.

If you set a passphrase, enter the passphrase and click on OK.

Putty request to enter passphrase when one is configured.

If the conversion is successful, the following message will appear:

Putty message indicating successful converstion of private key.

Now save your converted private key with a .ppk extension so it can be used by Putty.

Putty click the save private key button to save your converted private key.

Putty save as window to save your converted private key with the .ppk extension.

Load your private key in Putty

Open Putty, fill in your host name or IP and select SSH.

Putty set hostname or IP

Go to Connection -> Data and specify the username in the Auto-login username field.

Putty set the Auto-login username under Connection, Data.

Go to Connection -> SSH -> Auth, click on Browse and select your private key (file with the .ppk extension).

Putty attach private key under Connection, SSH, Auth.

For convenience, save your configuration.

When everything is setup correctly, you should get the following message when connecting:

If you setup a passphrase, enter it here and press enter.

Request to enter the passphrase when configured.

Congratulations, you have successfully configured SSH Key-Pair Authentication!

Successful SSH key login.

Disable password authentication

Now that we setup the SSH keys and use them to log in, we can disable password-only authentication. This way you increase security by restricting SSH access to your server with SSH keys only.

Log in as root or with a sudo user and open the SSH deamon configuration.

sudo nano /etc/sshd_config

Find the line that specifies PasswordAuthentication, uncomment it by deleting the preceding # and change its value to “no“.

SSH deamon configuration set PasswordAuthentication to no

There are two other settings that are important for key-only authentication. Make sure you check them before saving and closing the file.

SSH deamon configuration check if PubkeyAuthentication is set to yes and if ChallengeResponseAuthentication is set to no.

Now save the file and close it (CTRL + X, then Y and Enter)

Reload the SSH deamon:

sudo systemctl reload sshd

You have now disabled password authentication. From now on, your server is only accessible with SSH key authentication.

When you try to log in using password authentication, you get the following message:

Disconnected: No support authentication methods available (server sent: publickey)

Leave a comment