It is common to use SSH and scp to communicate and transfer files with remove servers. Sometimes we want to SSH login without password, especially within a shell script.
Create an encryption key pair
Log into local server. Run ssh-keygen to create an encryption key pair, the public and private keys. Just hit (Enter or Return) button for the key and the passphrases. If you already have an SSH key, you can skip this step.
ant@z:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ant/.ssh/id_rsa):
Created directory '/home/ant/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ant/.ssh/id_rsa.
Your public key has been saved in /home/ant/.ssh/id_rsa.pub.
The key fingerprint is:
The key's randomart image is:
+--[ RSA 2048]----+
| E .=.|
| C = o.o.|
| o * . .o.|
| = o oo.o|
| A * ..+. |
| = * o |
| . |
Copy the public key to the remote server
We still on the local server. Run ssh-copy-id to install the local server’s public key in a remote server’s authorized_keys.
ant@<local-server>:~$ ssh-copy-id <username>@<remote-server>
The authenticity of host '<remote-server> (xx.xxx.123.xx)' can't be established.
RSA key fingerprint is cd:12:34:56:12:8b:c0:e4:35:83:c1:cc:e6:df:d7:ab.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '<remote-server>,xx.xxx.123.xx' (RSA) to the list of known hosts.
If no ssh-copy-id
cat ~/.ssh/id_rsa.pub | ssh firstname.lastname@example.org 'cat >> ~/.ssh/authorized_keys'
Try logging into the remote server with ssh without password
And check in ” .ssh/authorized_keys” to make sure we haven’t added extra keys that you weren’t expecting.