Passwordless SSH login


You want to use Linux and OpenSSH to run an automated task. Therefore you need an automatic login from your PC with user A to a server with user B. You can't use any passwords, since you're calling SSH from within a shell script.


Setup

The first thing to do is to generate authentication keys on your PC. Remember to not set up a passphrase on the authentication keys. Use the following command for that:
ssh-keygen -t rsa

Next you need to create a .ssh folder on the server, to store the authentication key in:
ssh [email protected] mkdir -p .ssh

The final step is to add your generated authentication keys to the authorized keys list on the server:
ssh-copy-id [email protected]

Now you should be able to SSH into the server without typing a password.
ssh [email protected]

If it doesn't work and says
Agent admitted failure to sign using the key., try running this: ssh-add


For root user

If you need to set this up for the root user as well, just follow these very similar steps after logging in as root:
sudo -s

Setup

The first thing to do is to generate authentication keys on your PC. Remember to not set up a passphrase on the authentication keys. Use the following command for that:
ssh-keygen -t rsa

Since we've already created the .ssh-folder on the server we can skip this step.

The final step is to add your generated authentication keys to the authorized keys list on the server:
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

Note that you're copying the information from the /root-folder this time.

Now you should be able to SSH into the server without typing a password as root as well.
ssh [email protected]

If it doesn't work and says
Agent admitted failure to sign using the key., try running this: ssh-add