Advance

How To Set Up SSH keys On Ubuntu 20.04

How to set up SSH keys on Ubuntu 20.04

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

 

 

 

How To Set Up SSH keys On Ubuntu 20.04

Join us to finish the steps of this tutorial to show you How to set up SSH keys on Ubuntu 20.04. The plan is that we would focus on setting up SSH keys for an Ubuntu 20.04 installation. SSH keys provide a secure way of logging into your server and are recommended for all users.

 

Step 1- How To Create the Key Pair

Firstly, let’s create a key pair on the client machine.

ssh-keygen

Recent versions of ssh-keygen will create a 3072-bit RSA key pair by default, which is secure enough for most use cases.

Note: You may optionally pass in the -b 4096 flags to create a larger 4096-bit key.

Output
Generating public/private rsa key pair.  Enter file in which to save the key (/your_home/.ssh/id_rsa):

Do not forget to press enter to save the key pair into the .ssh/ subdirectory in your home directory or specify an alternate path.

You may see the below output if you had previously generated an SSH key pair.

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

In case you choose to overwrite the key on disk, you will not be able to authenticate using the previous key anymore.

Note: Be very careful when selecting yes, as this is a destructive process that cannot be reversed.

So, you need to receive output as below:

Output
Enter passphrase (empty for no passphrase):

At this point, you may need to enter a secure passphrase, which is highly recommended. A passphrase adds an additional layer of security to prevent unauthorized users from logging in. To learn more about security, consult our guide on Tutorial Configure SSH Key-Based Authentication on a Linux Server.

You should then see the output similar to the following:

Output
Your 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 [email protected]  The key's randomart image is:  +---[RSA 3072]----+  |                .|  |               + |  |              +  |  | .           o . |  |o       S   . o  |  | + o. .oo. ..  .o|  |o = oooooEo+ ...o|  |.. o *o+=.*+o....|  |    =+=ooB=o.... |  +----[SHA256]-----+

From now on, you 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- How To Copy The Public Key To Your Ubuntu Server

In this step, you will verify the quickest way to copy your public key to the Ubuntu host. It 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 to you on your client machine, you may use one of the two alternate 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 in many operating systems, so you may have it available on your local system.

Note: For this method to work, you must already have password-based SSH access to your server.

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

ssh-copy-id [email protected]_host
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

The above output shows you that your 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.

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

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:

Type in the password and 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 into a file in the remote account’s home ~/.ssh directory called authorized_keys.

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.

As you guess, your id_rsa.pub key has been uploaded to the remote account. You can continue on to Step 3.

 

Copying The Public Key Using SSH

The ssh-copy-id may not be available but you have password-based SSH access to an account on your server. So you can upload your keys using a conventional SSH method.

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

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

Also, you can then output the content you piped over into a file called authorized_keys within this directory. You can use the >> redirect symbol to append the content instead of overwriting it. by this, it would let you 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 && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
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

The above output shows you that your 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, to enter the remote user account password:

Output
[email protected]'s password:

Once you enter your password, the content of your id_rsa.pub key will be copied to the end of the authorized_keys file of the remote user’s account. Continue on to Step 3 if this was successful.

 

Copying The Public Key Manually

Please consider that if you do not have password-based SSH access to your server available, you will have to complete the above process manually.

Also, you can manually append the content of your id_rsa.pub file to the ~/.ssh/authorized_keys file on your remote machine.

Display the content of your id_rsa.pub key, by typing:

cat ~/.ssh/id_rsa.pub
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== [email protected]

Try to access your remote host using whichever method you have available.

Make sure the ~/.ssh directory exists after accessing your account. This command will create the directory if necessary, or do nothing if it already exists:

mkdir -p ~/.ssh

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, substitute the public_key_string with the output from the cat ~/.ssh/id_rsa.pub command that you executed on your local system. It should start with ssh-rsa AAAA…

Finally, you will ensure that the ~/.ssh directory and authorized_keys file have the appropriate permissions set:

chmod -R go= ~/.ssh

At this point, this recursively removes all “group” and “other” permissions for the ~/.ssh/ directory.

If you’re using the root account to set up keys for a user account, it’s also important that the ~/.ssh directory belongs to the user and not to root:

chown -R noodi:noodi ~/.ssh

Note: We named the user noodi in this guide, but you should substitute the appropriate username into the above command.

Of course, you can attempt passwordless authentication with our Ubuntu server now.

 

Step 3- Authenticating To Your Ubuntu Server Using SSH Keys

When one of the procedures above is successfully completed, you should be able to log into the remote host without providing the remote account’s password.

The basic process is the same:

ssh [email protected]_host
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

Point: You can see the above output if this is your first time connecting to this host.

The above output shows you that your local computer does not recognize the remote host. Type “yes” and then press ENTER to continue.

If you did not supply a passphrase for your private key, you will be logged in immediately. If you supplied 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 display in the terminal session for security). After authenticating, a new shell session should open for you with the configured account on the Ubuntu server.

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

Recommended Article: How To Set Up SSH keys On Ubuntu 20.04

Step 4- How To Disable Password Authentication On Your Server

When you are able to log into your account using SSH without a password, it means that you have successfully configured SSH-key-based authentication to your account. However, your password-based authentication mechanism is still active, meaning that your server is still exposed to brute-force attacks.

Notice that you should make sure that you either have SSH-key-based authentication configured for the root account on this server, or preferably, that you have SSH-key-based authentication configured for a non-root account on this server with sudo privileges before completing the steps in this section. This step will lock down password-based logins, so ensuring that you will still be able to get administrative access is crucial.

After confirming that your remote account has administrative privileges, log into your remote server with SSH keys, either as root or with an account with sudo privileges. Then, open up the SSH daemon’s configuration file:

sudo nano /etc/ssh/sshd_config

Inside the file, try to search 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 set the value to no. This will disable your ability to log in via SSH using account passwords:

/etc/ssh/sshd_config
. . .  PasswordAuthentication no  . . .

You can now save and close the file when you are finished, however, to do this pressCTRL+X, then Y to confirm saving the file, and finally ENTER to exit nano. To actually activate these changes, we need to restart the sshd service:

sudo systemctl restart ssh

As a precaution, open up a new terminal window and test that the SSH service is functioning correctly before closing your current session:

ssh [email protected]_host

You can safely close all current server sessions after verifying your SSH service is functioning properly.

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

 

 

Conclusion

In this article, you have successfully finished the guide. From now on you should now have SSH-key-based authentication configured on your server, allowing you to sign in without providing an account password. In case you are interested in reading more, find a related article on Tutorial connect to ssh on Linux.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.
  • Omar Borghi
    Omar Borghi
    1 year ago

    Thank you. I need to send my public key.

    Reply
    • Dani
      Dani
      1 year ago

      To send a public key, follow the below path.
      • Open your public key — the ~/. ssh/id_rsa. pub file — with a text editor. Select the contents, copy to your clipboard, and paste into a message to the person that requested it.
      • Send your public key — the ~/. ssh/id_rsa. pub file — to the person that requested it.

      Reply
  • Adam Gorgio
    Adam Gorgio
    1 year ago

    very good. I want to find the public key of someone else.

    Reply
    • Dani
      Dani
      1 year ago

      You can download it from a keyserver by selecting Keyserver , then search for keys and insert as the search term a part of the name or email address of this person.

      Reply
  • Beverly Baksi
    Beverly Baksi
    1 year ago

    Where is the private key located?

    Reply
    • Dani
      Dani
      1 year ago

      The private key is stored in ( ~/. ssh/id_rsa ) by default and the public key is stored in ( ~/. ssh/id_rsa. Pub )

      Reply
  • Scott Day
    Scott Day
    1 year ago

    Is is possible to have multiple private keys on a client system?

    Reply
    • Dani
      Dani
      1 year ago

      Yes, sure. for example, to specify the private key file to be used when launching the ssh client as follows:
      ssh -l neilsmyth -i ~/.ssh/id_work 35.194.18.119

      Reply
  • Lisa Brighton
    Lisa Brighton
    1 year ago

    When could we log in to the remote server without a password?

    Reply
    • Dani
      Dani
      1 year ago

      After completing the step 2, you can log in to your server via SSH using the command below:
      ssh [email protected]_ip_address

      Reply

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+18054214518

7 days a week, 24 hours a day