ssh免密登录

生成公钥和私钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
fe:57:d1:05:6b:84:7d:02:8e:5d:d5:11:c1:eb:f7:c7 root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
| .+=**|
| +.ooo+|
| . o o+o|
| ....|
| S .. |
| . ...|
| . . .o|
| . . E|
| .. .|
+-----------------+
#一直回车,不设置私钥密码

将公钥拷贝到需要免密登录的服务器

1
2
ssh-copy-id  xx.xx.xx.xx
#提示输入目标服务器的密码

如果ssh端口不是默认的22可以通过-p参数指定

1
ssh-copy-id  -p 6666  xx.xx.xx.xx

如果公钥的位置不是默认的~/.ssh/id_rsa.pub,可以通过-i 参数指定

1
ssh-copy-id -i /id_rsa.pub  xx.xx.xx.xx

如果不是默认的root用户

1
ssh-copy-id user@xx.xx.xx.xx

坚持原创技术分享,您的支持将鼓励我继续创作!