How To Generate An SSH Keypair To Allow Password-less Logins

It’s quite common to use SSH/SCP in scripts, particularly for backup purposes. Unfortunately, this would mean storing a password in the script, which would consequently appear in logs etc… A much better plan is to use SSH keypairs. Once you’ve created a passphrase-less keypair and copied it to both machines, you can login without a password.

Firstly we’ll create the key on the client machine. This is the machine that you want to be able to connect FROM without using a password. In this scenario, the machine “www” is going to run a backup script that needs to store data on “neo” without using a password. So we create the key on “www”.

ssh-keygen -t rsa -b 4096

The -b 4096 is optional, but will instruct the machine to generate a more secure 4096bit key rather than the default 2048 bit one.

When asked for a passphrase, simply hit enter for none.

Now we need to copy the key to the machine that we want to be able to access without a password:

ssh-copy-id -i ~/.ssh/

You will be prompted for your password for the user

All done 🙂

You can now type “ssh neo” and it will log you straight in without asking for your password! 😀