If you want to login from host_A (user a) to Host_B (user b), it is annoying to enter passwords everytime, because you want to call ssh from shell script.

Ok it’s quite easy to do that:

Generate public key

First in host_A you need to generate a pair of authentication keys;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
40:55:82:6b:a4:93:d7:6c:8d:5b:36:6c:b8:ac:80:44 name@domain.com
The key's randomart image is:
+--[ RSA 4096]----+
| .oo.. |
| E .o . |
| . +.+ = |
| . + +.* B |
| . . + oS* . |
| . . + |
| . . |
| . |
| |
+-----------------+

Create .ssh directory in host_B

Create a directory ~/.ssh as user b on host_B. (The directory may already exist, which is fine):

1
2
a@A:~> ssh b@B mkdir -p .ssh
b@B's password:

Append a’s public key to b

Append a’s new public key to b@B:.ssh/authorized_keys and enter b’s password one last time:

1
2
a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password:

Voilà, from now on you can log into B as b from A as a without password:

1
a@A:~> ssh b@B