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
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
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.
Set a Key Password (optional)
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
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
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
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
After typing your credentials you will be shown a screen telling you that the key has been successfully added.
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
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
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
Next simply press “Ctrl+x” then the “Y” key to save the config file.
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
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
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.