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

- Published on
- Authors
- Name
- Duarte Cancela
- @duartecancela
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
) - A public key (stored in
~/.ssh/id_rsa.pub
)
- A private key (usually stored in
Copy the public key to the server using the
ssh-copy-id
command. This will add your public key to theauthorized_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 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
, replaceid_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.