How to Setup OpenSSH with Keys on Ubuntu 22.04

ssh key splash

In this tutorial you will learn how to setup a SSH server to use a public key pair on Ubuntu. SSH is a secure protocol used for remotely administrating servers. We will be using OpenSSH to setup the server. SSH servers are great as they allow you to easily remote into any machine and execute commands. With public key authentication you get robust security. It is generally recommended that all users running an SSH server should enable key based authentication. You also don’t have to remember passwords when using key based authentication. For this tutorial we will be using Ubuntu 22.04 server, but you should be able to use any Linux operating system.

Generate Key Pair

generate key pair

The first step is to generate a key pair on your client machine. Type “ssh-keygen” and press “Enter”. If you get an error you may have to install the Openssh server as shown below.

sudo apt install openssh-server

Select Key Save Location

select key save file location

Next you will be asked to choose a save location for the key. Press “Enter” to use the default, or type a path and filename for the key.

Sponsored Products

Set a Key Password (optional)

select password

You will then be asked if you want to set a key on the password. This improves security but will require you to enter the password every time you connect to the SSH server. Either type a password or press the “Enter” key twice to not use a password.

Key Generated Successfully

key generated

Now you will see a message telling you that the key pair has been generated successfully. It will also list the fingerprint and the save location for the key.

Copying Public Key

copy the public key

Next we will copy the newly created key to your SSH server. Type in the following command to copy the key.

ssh-copy-id username@ssh-server-ipaddress

Enter the SSH Users Password

optionally select password for key

The server will then ask you to type in the password for the SSH user. Additionally if you set up a password on the key pair, you will also be prompted to type that.

Keys Successfully Added

keys added message

After typing your credentials you will be shown a screen telling you that the key has been successfully added.

SSH into the Server

ssh into the server

Now that the key has been added to the SSH server we need to connect back to the server. Connect to it using the SSH command as shown above.

Edit the OpenSSH Config File

edit the openssh config

Once you are connected to the server you will need to edit a file. Open the sshd_config file using the nano text editor.

sudo nano /etc/ssh/sshd_config

Disable Password Authentication

disable password authentication

Now that the file is open, use “Ctrl+W” to search for the line “PasswordAuthentication”. Once you locate the line change it from “PasswordAuthentication yes” to “PasswordAuthentication no”. This will cause the SSH server to only log you in using your key.

Save OpenSSH Config File

save openssh config

Next simply press “Ctrl+x” then the “Y” key to save the config file.

Restart OpenSSH Server

restart openssh server

Finally we will need to restart the ssh service on the server. Run the following command to complete this action.

sudo systemctl restart ssh

After the SSH service restarts, you will need to disconnect from the SSH server.

Test the SSH Connection

test ssh connection

Now we will test that we are able to login to the SSH server using our key pair. Simply try to reconnect to the SSH server. You will be automatically logged in if you don’t have a password on the key pair. Else you will need to enter the password you created for the key.

OpenSSH with a Key Pair is Enabled

successful key ssh

If you are able to connect to the SSH server then you have successfully setup a key for OpenSSH. Remember to keep the key pair in a secure location. Anyone with access to it would be able to login to your server.

SFTP Server (optional)

You can also enable secure file transfer with SFTP using OpenSSH. If you would like to activate the server view our How to Setup an SFTP Server on Ubuntu 22.04 using OpenSSH tutorial.

Additional Questions?

If you have any additional questions or thoughts feel free to comment them below.

Related Resources

View our list of The Top 10 Programming Languages to Learn in 2022.

Learn How to Install Ubuntu Server 22.04 [Step by Step].

Click here to learn How to Install MySQL on Ubuntu Server 22.04 LTS.

View all of our Linux Articles and Tutorials.

Learn How to install and configure Nginx – Ubuntu 20.04.

View all of our available online tools and converters at Formatswap.com.

Leave a Reply

Your email address will not be published. Required fields are marked *