How to Setup SSH keys on Ubuntu 20.04

SSH protocol is used for establishing a secure connection between a client and its host or server. It uses the public key-based as well as password-based mechanism. It is supported by almost every Linux-based OS. In this post, we will learn about SSH key generation and connecting to remote servers.

SSH Key Generation on Ubuntu 20.04

Already generated SSH keys are placed in the ~/.ssh directory. You can verify if there already exists a key or not by typing the command

$ ls -l ~/.ssh/id_*

If the output is “No such file or directory” or “No matches found” then this means that you have not generated any key before. You can create anSSH key by typing the following command

$ ssh-keygen

After hitting Enter, it will ask you to provide the file where you want to save the key, hit Enter to select the default provided directory and file.

Right after selecting the file, it will ask for the passphrase. Either enter it or press Enter for the empty passphrase. It is just an extra security layer.

As you can see that a new SSH key is generated.

You can also create a 4096 bit-sized key with the RSA typed algorithm using this command.

$ ssh-keygen -t rsa -b 4096

The rest of the process will be the same as above about providing the file and setting the passphrase.

Once the key is generated, let’s copy it to the server.

Copying the SSH Key

For copying the SSH key to the server, you must know the username, password, and IP address of the server. The required information about my server is

Username: linuxuser

IP address: 192.168.18.141

Confirm that OpenSSH is installed on your server by running the command

$ sudo service ssh status

If it is not installed, install OpenSSH 

$ sudo apt install openssh-server

Once OpenSSH is installed and the SSH service is running on the server. Let’s get back to the client’s machine. 

Now, to copy the recently generated keys to the server, type the following command with your server’s username and IP address.

$ ssh-copy-id [email protected]

After running the command, it will ask to continue connecting so type “yes” to continue.

Enter the password of the specified user and keys are copied successfully.

Let’s SSH into the host.

Log In via SSH to your Remote Server

Log in to your remote server by simply using the command given below.

$ ssh [email protected]

Make sure to change the username and IP address with your server information.

You are logged in to your server.

Run the ip a command to verify that you are on the server’s machine.

$ ip a

Conclusion

This is how you can generate SSH keys and copy SSH keys to the server and login to the server’s machine remotely using the SSH.