Skip to Content

How to login Ubuntu server using SSH Key

If you are looking for a more secure way to login to your Ubuntu server, then using an ssh key is a good option. By using an ssh key, you can avoid having to enter your password each time. In addition, ssh keys are more difficult to brute force than passwords.

In this blog post, we are going to discuss two ways that you can login to your Ubuntu server using an ssh key. The first way is by specifying the ssh key file in ssh command, and the second way is by adding ssh key in ssh configure file.

We will go over the benefits of each method and how to set them up. Let’s get started!

specify SSH key file in SSH command

The easiest way to login in Ubuntu server with SSH key is specifying the ssh key file in the SSH command. For example, if your private key file is named id_rsa, you would use the following command to login:

ssh -i ~/.ssh/id_rsa user@server

You will be prompted to enter the passphrase for your private key if you have one. Once you have entered the correct passphrase, you will be logged in to your server.

   -i identity_file:  Selects a file from which the identity (private key) for public key authentication is read. The default is ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ecdsa_sk, ~/.ssh/id_ed25519, ~/.ssh/id_ed25519_sk and ~/.ssh/id_rsa.

It is possible to have multiple -i options (and multiple identities specified in configuration files).

specify SSH key file in SSH config file

Identity files can also be specified on a per-host basis in the configuration file.

The second way to use ssh keys to login Ubuntu server is by adding your ssh key to the ~/.ssh/config file. This file is used to store ssh configuration options. To add your ssh key to this file, you will need to edit it with a text editor and add the following lines:

Host server
IdentityFile ~/.ssh/id_rsa
User user

After you have saved the changes to the ~/.ssh/config file, you can login by simply using the following command: ssh server.

Both of these methods have their benefits. Specifying the ssh key file with the -i option is a good option if you only need to use that key for a specific server. Adding your ssh key to the ~/.ssh/config file is a good option if you need to login to multiple servers with the same key.

Which method do you prefer? Let us know in the comments!

Do you want to learn more about Ubuntu? Check out our other blog posts or our Ubuntu course! If you have any questions, feel free to ask in the comments below or reach out to us on social media. We are happy to help!