喵♂呜 的博客

一个刚毕业就当爹的程序猿 正在迷雾中寻找道路...

用命令直接设置远程主机的公钥

今天在研究rsync同步的时候 发现 部署公钥不一定要到远程主机输入 可以通过命令直接复制

生成公钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ssh-keygen
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:
38:3c:57:d5:fa:9f:77:06:2d:1a:2f:d5:b7:55:ac:fd root@iZ28cqmea34Z
The key's randomart image is:
+--[ RSA 2048]----+
| .. |
| . . |
| . . . |
| . . . . o|
| = S . =.|
| + . * *|
| = +*|
| o .oE|
| . .o|
+-----------------+

复制公钥到目标主机

1
2
3
4
5
6
7
$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.2.200
root@192.168.2.200's password: //输入密码
Now try logging into the machine, with "ssh '用户名@远程主机'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

欢迎关注我的其它发布渠道