May 25, 2017

How to Exchange SSH Keys for Passwordless Login

Administration Logo

You don’t want to enter any passwords, because you want to call ssh or rsync from within a shell script? For this passwordless login you need to exchange the SSH keys between the systems.

Root access is required to edit the following files and to execute commands. Log in as root (su) or simply prepend sudo to all commands that require root privileges.

Create authentication keys on Server 1

First log in on Server 1 as user and generate a pair of authentication keys. Do not enter a passphrase.

ssh-keygen -t rsa

Create the ~/.ssh directory on Server 2 with SSH from Server 1

ssh user@server2 mkdir -p .ssh

Transfer your public key from Server 1 to Server 2

cat /root/.ssh/id_rsa.pub | ssh user@server2 'cat >>.ssh/authorized_keys'

Bidirectional passwordless login

For the same passwordless login from Server 2 to Server 1 follow the next steps. Otherwise you can stop here.

Create authentication keys on Server 2

Log in on Server 2 as user and generate a pair of authentication keys. Do not enter a passphrase.

ssh-keygen -t rsa

Transfer your public key from Server 2 to Server 1

cat /root/.ssh/id_rsa.pub | ssh user@server1 'cat >>.ssh/authorized_keys'

Now you can log into the respective other as user without password.

Speak Your Mind