Skip to Content

2 Ways to login Linux server with SSH key

In this blog post, we are going to discuss two ways that you can login to your Linux 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!

If you are looking for a more secure way to login to your Linux 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. There are two ways that you can use ssh keys for logging in. Let’s take a look at each method.

specify SSH key in SSH command to login Linux server

The easiest way to login in Linux 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. Identity files may also be specified on a per-host basis in the configuration file. It is possible to have multiple -i options (and multiple identities specified in configuration files). If no certificates have been explicitly specified by the CertificateFile directive, ssh will also try to load certificate information from the filename obtained by appending -cert.pub to identity filenames.

specify SSH key in SSH config file to login Linux server

The second way to use ssh keys to login Linux 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. You will be prompted for your passphrase for this key file and then logged in to your 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 Linux? Check out our other blog posts or our Linux 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! Until next time, happy coding!