How to access a Linux server through ssh and config key pairs?

By duartecancela, 5 April, 2023
How to access a Linux server through ssh and config key pairs?

To access a Linux server with SSH and configure key pairs, follow these steps:

Generate a new key pair on your local machine (if you don't already have one) using the ssh-keygen command. This will create two files: a private key (usually stored in ~/.ssh/id_rsa) and a public key (stored in ~/.ssh/id_rsa.pub).

Copy the public key to the server using the ssh-copy-id command. This will add your public key to the authorized_keys file on the server, allowing you to authenticate using your private key.

ssh-copy-id user@server_address

If ssh-copy-id is not installed on your local machine, you can manually copy the contents of the public key file to the authorized_keys file on the server using an editor or the following command:

cat ~/.ssh/id_rsa.pub | ssh user@server_address "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Secure the private key on your local machine by setting the correct permissions:

chmod 600 ~/.ssh/id_rsa

Connect to the server using SSH, specifying the private key file with the -i option:

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

If your key file is named something other than id_rsa, replace id_rsa in the above command with the name of your key file.

That's it! You should now be able to access your Linux server using SSH with key pairs.

 

Photo by FLY:D on Unsplash