How to Install SSH Keys on Ubuntu 20.04
Introduction
SSH, or Secure Shell, is an encrypted protocol used to manage and communicate with servers. When working with an Ubuntu server, it's likely that you'll spend most of your time in a terminal session connected to your server via SSH. Stay tuned with us for how to install SSH Keys on Ubuntu 20.04.
In this HelpIn this article, we will focus on setting up SSH keys for your Ubuntu 20.04 installation. SSH keys provide a secure way to log in to your server and are recommended for all users.
Creation DigitalOcean Droplet allows you to instantly add your computer's SSH keys so you can connect privately and securely.
Step 1 – Create a key pair
The first step is to create a key pair on the client device (usually your computer):
$ ssh-keygenBy default, recent versions of ssh-keygen generate a 3072-bit RSA key pair, which is secure enough for most purposes (you can optionally pass the -b 4096 flag to generate a larger 4096-bit key).
After entering the command, you should see the following output:
Output
Generating public/private rsa key pair.
Enter file in which to save the key (/your_home/.ssh/id_rsa):Press Enter to save the key pair in the /ssh. subdirectory of your home directory, or specify an alternative path.
If you have previously created an SSH key pair, you may see the following prompt:
Output
/home/your_home/.ssh/id_rsa already exists.
Overwrite (y/n)?If you choose to overwrite the key to disk, you will no longer be able to authenticate using the previous key. Be very careful when selecting (Yes), as this is a destructive process that cannot be reversed.
You should then see the following notification:
Output
Enter 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 on security, see our tutorial on how to configure SSH key-based authentication on a Linux server.
You should then see output similar to the following:
OutputYour identification 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 key's randomart image is: +---[RSA 3072]----+ | .| | + | | + | | . o . | |o S . o | | + o. .oo. .. .o| |o = oooooEo+ ...o| |.. o *o+=.*+o....| | =+=ooB=o.... | +----[SHA256]-----+
You now have a public and private key that you can use for authentication. 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 the Ubuntu server
The fastest way to copy the public key on an Ubuntu host is to use a tool called ssh-copy-id. Due to its simplicity, this method is highly recommended if available. If you don't have ssh-copy-id available on your client machine, you can use one of the two alternative methods provided in this section (copying via password-based SSH or manually copying 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 this tool, you specify the remote host you want to connect to and the user account that you have password-based SSH access to. This is the account to which your SSH public key will be copied.
Like this:
$ ssh-copy-id username@remote_hostYou may see the following message:
Output
The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established.
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)? yesThis means that your local computer does not recognize the remote host. This is the first time you are connecting to a new host. Type “yes” and press ENTER to continue.
Next, the tool will scan your local account for the id_rsa.pub key we created earlier. When it finds the key, it will ask you for the remote account password:
Output
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:Enter the password (your typing will not be displayed for security purposes) and press ENTER. The tool 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 ssh./~ home directory called authorized_keys.
You should see the following output:
Output
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) 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.
Copying 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 the regular SSH method.
We can do this by using the cat command to read the contents of the SSH public key on our local computer and piping it through an SSH connection to the remote server.
Alternatively, we can make sure that the ssh./~ directory exists and has the correct permissions for the account we are using.
We can then output the content we entered into a file called authorized_keys in this directory. We will use the redirection symbol << to append the content instead of overwriting it. This allows us to add keys without destroying previously added keys.
The complete command is as follows:
$ cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
You may see the following message:
Output
The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established.
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)? yesThis means that your local computer does not recognize the remote host. This is the first time you are connecting to a new host. Type Yes and press ENTER to continue.
After that, you should be asked to enter the password for the remote user account:
Output
[email protected]'s password:After entering the password, the contents of your id_rsa.pub key will be copied to the end of the remote account's authorized_keys file. If successful, continue to Step 3.
Copying the public key manually
If you don't have password-based SSH access to your server, you will need to complete the above process manually.
We 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 on your local computer:
# cat ~/.ssh/id_rsa.pubYou will see the contents of the key, which should look something like this:
Output
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCqql6MzstZYh1TmWWv11q5O3pISj2ZFl9HgH1JLknLLx44+tXfJ7mIrKNxOOwxIxvcBF8PXSYvobFYEZjGIVCEAjrUzLiIxbyCoxVyle7Q+bqgZ8SeeM8wzytsY+dVGcBxF6N4JS+zVk5eMcV385gG3Y6ON3EG112n6d+SMXY0OEBIcO6x+PnUSGHrSgpBgX7Ks1r7xqFa7heJLLt2wWwkARptX7udSq05paBhcpB0pHtA1Rfz3K2B+ZVIpSDfki9UVKzT8JUmwW6NNzSgxUfQHGwnW7kj4jp4AT0VZk3ADw497M2G/12N0PPB5CnhHf7ovgy6nL1ikrygTKRFmNZISvAcywB9GVqNAVE+ZHDSCuURNsAInVzgYo9xgJDW8wUw2o8U77+xiFxgI5QSZX3Iq7YLMgeksaO4rBJEa54k8m5wEiEE1nUhLuJ0X/vh2xPff6SQ1BL/zkOhvJCACK6Vb15mDOeCSq54Cr7kvS46itMosi/uS66+PujOO+xt/2FWYepz6ZlN70bRly57Q06J+ZJoc9FfBCbCyYH7U/ASsmY095ywPsBo1XQ9PqhnN1/YOorJ068foQDNVpm146mUpILVxmq41Cj55YKHEazXGsdBIbXWhcrRf4G2fJLRcGUr9q8/lERo9oxRm5JFX6TCmj6kmiFqv+Ow9gI0x8GvaQ== demo@testAccess 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:
$ mkdir -p ~/.sshNow you can create or modify the authorized_keys file in this directory. You can add the contents of your id_rsa.pub file to the end of the authorized_keys file and create it if necessary using this command:
$ echo public_key_string >> ~/.ssh/authorized_keysIn the above command, replace public_key_string with the output of the cat ~/.ssh/id_rsa.pub command you ran on your local system. It should start with ssh-rsa AAAA….
Finally, we will make sure that the ssh./~ directory and the authorized_keys file have the appropriate permissions set:
$ chmod -R go= ~/.sshThis recursively removes all "group" and "other" permissions for the /ssh./~ directory.
If you are using the root account to set up keys for a user account, it is also important that the ssh./~ directory is owned by the user and not root:
chown -R sammy:sammy ~/.sshIn this tutorial, our user is named sammy, but you should substitute the appropriate username in the above command.
Now we can perform passwordless authentication with Ubuntu Server.
Step 3 – Authenticate to Ubuntu Server Using SSH Keys
If you have successfully completed one of the steps above, you should be able to log in to the remote host without providing the remote account password.
The basic process is the same:
$ ssh username@remote_hostIf this is your first time connecting to these hosts (if you use the last method above), you may see something like this:
Output
The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established.
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 your 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 (note that your keystrokes will not be displayed in the terminal session for security). After authentication, a new shell session should open for you 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, your password-based authentication mechanism is still enabled, meaning your server is still vulnerable to brute-force attacks.
Before completing the steps in this section, make sure you have either configured SSH key-based authentication for the root account on this server, or preferably configured SSH key-based authentication for a non-root account on this server with sudo privileges. This step locks down password-based logins, so it is very important to ensure that you can still have administrative access.
Once you have verified that your remote account has administrative privileges, log in to your remote server with your SSH keys, either as root or with an account with sudo privileges. Then, open the SSH Daemon configuration file:
$ sudo nano /etc/ssh/sshd_configInside the file, look for a directive called PasswordAuthentication. This line may be commented out with a # at the beginning of the line. Uncomment the line by removing the # and setting the value to no. This will disable your ability to log in via SSH using account passwords:
/etc/ssh/sshd_config
. . .
PasswordAuthentication no
. . .Once done, save and close the file by pressing CTRL+X, then Y to confirm saving the file, and finally ENTER to exit nano. To activate these changes, we need to restart the sshd service:
$ sudo systemctl restart sshAs a precaution, open a new terminal window and test that the SSH service is working properly before closing your current session:
# ssh username@remote_hostOnce 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 responds only to SSH key-based authentication. Password-based logins have been disabled.
Result
Now you need to configure SSH key-based authentication on your server, allowing you to log in without providing an account password.









