Connect to a Linux host using SSH key instead of credentials

Generate a new SSH key on the client Link to heading

ssh-keygen -t rsa -b 4096

Copy the public key to the host/server Link to heading

Method 1: Automatically copy the key to the host Link to heading

Command alternative 1:

cat ~/.ssh/id_rsa.pub | ssh user@host 'cat >> ~/.ssh/authorized_keys'

Command alternative 2:

ssh user@host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub

Method 2: Manually copy the key Link to heading

On the client: View the public key to be able to copy it.

cat .ssh/id_rsa.pub

On the host: Create the authorized keys file.

mkdir ~/.ssh
touch ~/.ssh/authorized_keys

On the host: Paste the client public key in the authorized keys file.

nano ~/.ssh/authorized_keys

Troubleshooting Link to heading

The client is unable to connect Link to heading

Change the permissions to the authorized keys file on the host.

chmod 600 ~/.ssh/authorized_keys

The host still asks for a username/password Link to heading

Change the permissions to the .ssh directory.

chmod 700 ~/.ssh

Use a different SSH key file to connect to the host Link to heading

ssh user@host -i "~\.ssh\other_id_rsa_file"