How to Set Up SSH Keys on Ubuntu 22.04

Published on March 24, 2023 · Updated on March 24, 2023
How to Set Up SSH Keys on Ubuntu 22.04

SSH (Secure Shell) is a network protocol that provides a secure way to access a remote computer over an unsecured network. SSH keys are used to authenticate a user to a remote server without requiring the user to enter a password. In this tutorial, we will show you how to set up SSH keys on Ubuntu 22.04.

Creating the Key Pair

The first step to setting up SSH keys is to create a key pair. A key pair consists of a public key and a private key. The private key is kept on the local machine, and the public key is copied to the remote server.

To create a new key pair, open a terminal on your local machine and run the following command:

ssh-keygen

Follow the prompts to generate the key pair. By default, the keys will be saved in the .ssh directory in your home directory.

Copying the Public Key to Your Ubuntu Server

The easiest way to copy your public key to your Ubuntu server is to use the ssh-copy-id command. To use this command, run the following command:

ssh-copy-id username@server_ip_address

Replace username with your username on the server, and server_ip_address with the IP address of your Ubuntu server. You will be prompted to enter your password for the server.

Copying the Public Key Using SSH

Another way to copy your public key to your Ubuntu server is to use the ssh command. To use this command, run the following command:

ssh username@server_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo 'YOUR_PUBLIC_KEY' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Replace username with your username on the server, server_ip_address with the IP address of your Ubuntu server, and YOUR_PUBLIC_KEY with the contents of your public key.

Copying the Public Key Manually

If you prefer to copy your public key manually, you can do so by copying the contents of your public key and pasting it into the ~/.ssh/authorized_keys file on your Ubuntu server. To do this, run the following command:

cat ~/.ssh/id_rsa.pub

Copy the output of this command, then open the ~/.ssh/authorized_keys file on your Ubuntu server with a text editor, and paste the contents of the public key into the file.

Authenticating to Your Ubuntu Server Using SSH Keys

Once you have copied your public key to your Ubuntu server, you can authenticate to the server using your private key. To do this, run the following command:

ssh username@server_ip_address

Replace username with your username on the server, and server_ip_address with the IP address of your Ubuntu server. You will be logged in to the server without being prompted for a password.

Disabling Password Authentication on Your Server

To increase the security of your Ubuntu server, you may want to disable password authentication and only allow SSH key authentication. To do this, open the SSH configuration file on your Ubuntu server with a text editor:

sudo nano /etc/ssh/sshd_config

Find the following line in the file:

#PasswordAuthentication yes

Remove the # at the beginning of the line to uncomment it, then change the value to no:

PasswordAuthentication no

From now on, you will only be able to authenticate to your Ubuntu server using SSH keys.

Conclusion

In this tutorial, we have shown you how to set up SSH keys on Ubuntu 20.04. SSH keys provide a secure way to access a remote server without having to enter a password. By using SSH keys, you can improve the security of your Ubuntu server and make it more difficult for unauthorized users to access your system.

Load Comments