How to Set Up SSH Keys on Ubuntu 20.04 – DigitalOcean

Introduction

SSH, or secure shell, is an encrypted protocol used to manage and communicate with servers. When working with an Ubuntu server, you are likely to spend most of your time in a terminal session connected to your server via SSH.

In this guide, we will focus on setting up SSH keys for an Ubuntu 20.04 installation. SSH keys provide a secure way to log in to your server and are recommended for all users.

Step 1 — Creating

the key pair

The first step

is to create a key pair on the client computer (usually the computer): ssh-keygen

By default, recent versions of ssh-keygen will create a 3072-bit RSA key pair, which is secure enough for most use cases (you can optionally pass the -b flag 4096 to create a larger 4096-bit key).

After entering the command, you should see the following output:

OutputGenerating public/private rsa key pair. Enter the file where you want to save the key (/your_home/.ssh/id_rsa): Press Enter to save the key pair in the .ssh/ subdirectory

of the parent directory, or specify an alternate path

.

If you previously generated an SSH key pair, you may see the following message:

Output/home/your_home/.ssh/id_rsa already exists. Overwrite (y/n)?

If you choose to overwrite the key on disk, you can no longer authenticate with the old key. Be very careful when selecting yes, as this is a destructive process that cannot be reversed.

You should then see the following message:

OutputEnter passphrase (empty for no passphrase):

Here you can optionally enter a secure passphrase, which is highly recommended. A passphrase adds an extra layer of security to prevent unauthorized users from logging in. For more information about security, see our tutorial on How to configure SSH key-based authentication on a Linux server.

Then, you should see output that resembles the following:

OutputYour ID has been saved in /your_home/.ssh/id_rsa Your public key has been saved in /your_home/.ssh/id_rsa.pub The key fingerprint is: SHA256:/hk7MJ5n5aiqdfTVUZr+2Qt+qCiS7BIm5Iv0dxrc3ks user@host The randomart image of the key is: +-[RSA 3072]-+ | .| | + | | + | | . or. | |o S . or | | + o. . . Oo. .. .o| |o = oooooEo+ … o| |.. or *o+=.*+o….| | =+=ooB=o…. | +-[SHA256]-+

You now have a public and private key that you can use to authenticate. The next step is to place the public key on your server so that you can use SSH key-based authentication to log in.

Step 2 – Copy

the public key to your Ubuntu server

The quickest way to copy your public key to the Ubuntu host is to use a utility called ssh-copy-id. Due to its simplicity, this method is highly recommended if available. If you do not have ssh-copy-id available on your client machine, you can use one of the two alternative methods provided in this section (copy via password-based SSH or manually copy the key).

Copying the public key using

ssh-copy-id

The ssh-copy-id tool is included by default on many operating systems, so you may have it available on your local system. For this method to work, you must already have password-based SSH access to your server.

To use the utility, specify the remote host to which you want to connect and the user account to which you have password-based SSH access. This is the account to which your public SSH key will be copied.

The syntax is

:

  1. ssh-copy-id username@remote_host

You may see the following message:

OutputUnable to set authenticity of host ‘203.0.113.1 (203.0.113.1)’. The ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? Yes

This means that the local computer does not recognize the remote host. This will happen the first time you connect to a new host. Type “yes” and press ENTER to continue.

Next, the utility will scan your local account for the id_rsa.pub key we created earlier. When it finds the key, it prompts you for the password of the remote user’s account

: Output/usr/bin/ssh-copy-id: INFO: trying to log in with the new key(s), to filter out the ones already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) left to install – if prompted now is to install the new keys username@203.0.113.1’s password:

Type your password (your type will not be displayed, for security reasons), and then press ENTER. The utility will connect to the account on the remote host using the password you provided. It will then copy the contents of your ~/.ssh/id_rsa.pub key to a file in the remote account’s primary ~/.ssh directory called authorized_keys.

You should see the following output:

OutputNumber of keys added: 1 Now try to log in to the machine, with: “ssh ‘username@203.0.113.1′” and make sure that only the keys you wanted were added.

At this point, your id_rsa.pub key has been uploaded to the remote account. You can proceed to step 3.

Copy

the public key using SSH

If you don’t have ssh-copy-id available, but you have password-based SSH access to an account on your server, you can upload your keys using a conventional SSH method

.

We can do this by using the cat command to read the contents of the public SSH key on our local computer and pipe it through an SSH connection to the remote server.

On the other hand, we can make sure that the ~/.ssh directory exists and has the correct permissions under the account we are using.

We can then generate the content we pipe into a file called authorized_keys within this directory. We’ll use the >> redirect symbol to add the content instead of overwriting it. This will allow us to add keys without destroying previously added keys.

The full command looks like this

: cat ~/.ssh/id_rsa.pub | ssh username@remote_host”mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys &&

  1. chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys”

You may see the following message:

OutputUnable to set host authenticity ‘203.0.113.1 (203.0.113.1)’. The ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? Yes

This means that the local computer does not recognize the remote host. This will happen the first time you connect to a new host. Type yes, and then press ENTER to continue.

Afterward, you will be prompted to enter the password of the remote user account:

Outputusername@203.0.113.1’s:

After entering your password, the contents of your id_rsa.pub key will be copied to the end of the authorized_keys file of the remote user’s account. Continue to step 3 if this was successful.

Copy

the public key manually

If you do not have password-based SSH access to your server available, you will need to complete the above process manually

. We will manually add the contents of your id_rsa.pub file

to the ~/.ssh/authorized_keys file on your remote machine.

To display the contents of the id_rsa.pub key, Type this into your local computer:

  1. cat ~/.ssh/id_rsa.pub

You will see the contents of the key, which should look like this:

Outputssh-rsa

AAAAB3NzaC1yc2EAAAADAQABAAACAQCqql6MzstZYh1TmWWv11q5O3pISj2ZFl9HgH1JLknLLx44+tXfJ7mIrKNxOOwxIxvcBF8PXSYvobFYEZjGIVCEAjrUzLiIxbyCoxVyle7Q+bqgZ8SeeM8wzytsY+dVGcBxF6N4JS+zVk5eMcV385gG3Y6ON3EG112n6d+SMXY0OEBIcO6x+PnUSGHrSgpBgX7Ks1r7xqFa7heJLLt2wWwkARptX7udSq05paBhcpB0pHtA1Rfz3K2B+ZVIpSDfki9UVKzT8JUmwW6NNzSgxUfQHGwnW7kj4jp4AT0VZk3ADw497M2G/12N0PPB5CnhHf7ovgy6nL1ikrygTKRFmNZISvAcywB9GVqNAVE+ZHDSCuURNsAInVzgYo9xgJDW8wUw2o8U77+ xiFxgI5QSZX3Iq7YLMgeksaO4rBJEa54k8m5wEiEE1nUhLuJ0X/vh2xPff6SQ1BL/zkOhvJCACK6Vb15mDOeCSq54Cr7kvS46itMosi/uS66+PujOO+xt/2FWYepz6ZlN70bRly57Q06J+ZJoc9FfBCbCyYH7h7UU66FfBCbCyYH7h7Us66 /ASsmY095ywPsBo1XQ9PqhnN1/YOorJ068foQDNVpm146mUpILVxmq41Cj55YKHEazXGsdBIbXWhcrRf4G2fJLRcGUr9q8/lERo9oxRm5JFX6TCmj6kmiFqv+Ow9gI0x8GvaQ== demo@test

Access your remote host using any method available to you.

Once you have access to your account on the remote server, you need to make sure that the ~/.ssh directory exists. This command will create the directory if necessary, or do nothing if it already exists:

  1. mkdir -p ~/.ssh

Now, you can create or modify the authorized_keys file within this directory. You can add the contents of your id_rsa.pub file to the end of the authorized_keys file, creating it if necessary, using this command:

echo public_key_string>> ~/.ssh/authorized_keys In the above command, replace the public_key_string with the output of the cat ~/ command.

  1. ssh/

id_rsa.pub file you ran on your local system. You should start with ssh-rsa AAAA…

Finally, we will make sure that the directory ~/.

ssh and the authorized_keys file have the appropriate permissions set

:

  1. chmod -R go=

~/.ssh

This recursively removes all “group

” and “other” permissions for the ~/.ssh/ directory. If you are using the root account to

configure keys for a user account, it is also important that the ~/.ssh directory belongs to the user and not to the root

: chown -R sammy:sammy

  1. ~/.ssh

In this tutorial, our user is called sammy, but you need to substitute the appropriate username in the above command

.

Now we can try passwordless authentication with our Ubuntu server

.

Step 3 – Authenticate to your Ubuntu server using SSH keys

If you

have successfully completed one of the previous procedures, you should be able to log on to the remote host without providing the remote account password.

The basic process

is the same:

  1. ssh username@remote_host

If this is your

first time connecting to this host (if you used the last method above), You might see something like this:

OutputUnable to set host authenticity ‘203.0.113.1 (203.0.113.1)’. The ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? Yes

This means that the local computer does not recognize the remote host. Type “yes,” and then press ENTER to continue.

If you did not provide a passphrase for your private key, you will be logged in immediately. If you provided a passphrase for the private key when you created the key, you will be prompted to enter it now (note that your keystrokes will not be displayed in the terminal session for security). After authenticating, a new shell session should be opened with the account configured on the Ubuntu server.

If key-based authentication was successful, continue to learn how to further secure your system by disabling password authentication.

Step 4 — Disable

password authentication on your server

If you were able to log in to your account using SSH without a password, you have successfully configured SSH key-based authentication on your account. However, its password-based authentication mechanism is still active, which means that your server is still exposed to brute force attacks.

Before you complete the steps in this section, make sure that you have SSH key-based authentication configured for the root account of this server

or, preferably, that you have SSH key-based authentication configured for a non-root account on this server with sudo privileges. This step will block password-based logins, so it’s crucial to make sure you’ll still be able to gain administrative access.

Once you have confirmed that your remote account has administrative privileges, log in to your remote server with SSH keys, either as root or with an account with sudo privileges. Then, open the SSH daemon configuration file:

  1. sudo nano /etc/ssh/sshd_config

Within the file, look for a directive called PasswordAuthentication. This line can be commented with a # at the beginning of the line. Uncomment the line by removing # and set the value to no. This will disable your ability to log in via SSH using account passwords:

. . . PasswordAuthentication does not . . .

Save and close the file when you are done by pressing CTRL+X, then Y to confirm the file saving, and finally ENTER to exit nano. To actually activate these changes, we need to

restart the sshd service:

  1. sudo systemctl restart

ssh As a precaution, open a new terminal window and test that the SSH service is working properly before closing your current session:

  1. ssh username@remote_host

Once you have verified that your SSH service is working properly, you can safely close all current server sessions.

The SSH daemon on your Ubuntu server now only responds to SSH key-based authentication. Password-based logins have been disabled.

Conclusion

You should now have SSH key-based authentication set up on your server, allowing you to log in without providing an account password

.

If you want to learn more about working with SSH, take a look at our SSH Essentials Guide.