使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录

1、在A机器上使用ssh-keygen产生公钥私钥对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ssh-keygen -t rsa -b 1024
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
0c:f8:73:aa:46:52:06:ba:54:e0:fc:d4:40:58:07:33 root@HA-1
The key's randomart image is:
+--[ RSA 1024]----+
| .=E.. |
|o...*. |
| +.o... |
|..o o. o |
|.. + o S |
|. . . + |
| o . |
| .. |
| .. |
+-----------------+

2、第二步:用ssh-copy-id将公钥复制到远程机器中

1
2
3
4
5
 #ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
`

ssh-copy-id 将key写到远程机器的 ~/ .ssh/authorized_key.文件中

#ssh remote-host