When accessing remote systems using SSH it can be handy to use RSA keys rather than having to enter a password every time. Especially handy if doing automation using Ansible or similar tools. Here’s how to do it:
Generate the key pair
One public and one private key will be created. The private key is kept securely on the client system. The public key is copied to the target server. The passphrase is optional. It helps secure they key if the private key is compromised. In this example we skip entering a passphrase.
Copy the key to the remote system
We now copy over the public key to the remote system. Note that we need to enter the password to get the key copied. This is what we’re trying to fix. Note that we copy the .pub public key to a new name: “authorized_keys” in the .ssh directory for your user on the remote system. For example “/home/jonas/.ssh/authorized_keys”.
Verify the solution
Repeat the SCP command but this time copy the public key to a random name to verify that SSH/SCP can be done without entering a password:
…and with Ansible
Below we finally compare pinging a host with RSA key auth enabled vs. server with password login only. Predictably one succeeds and one fails.