A-A+
ssh 无密码访问
一. 两台服务器
test1 192.168.0.57
test2 192.168.0.52
二. ssh-keygen 生成密钥
在test1上生成密钥
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
sh-keygen -t rsa
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: 2a:7b:e5:06:ac:73:5f:c5:bd:95:37:4c:5a:77:88:25 root@test1 The key's randomart image is: +--[ RSA 2048]----+ | E . | | + . | | . .oo| | . .= +| | . S o..+o| | o.. . oo| | ...+ . . | | ooo o. | | .+ o. | +-----------------+ [root@test1 .ssh]# ls \ 检查生成的文件 id_rsa id_rsa.pub [root@test1 .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.0.52 \ 将密钥拷贝到test2上 Warning: Permanently added '192.168.0.52' (RSA) to the list of known hosts. root@192.168.0.52's password: Now try logging into the machine, with "ssh 'root@192.168.0.52'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. |
在test2 上执行上述同样的步骤
三. 验证结果
在test2 上ssh 登录test1
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# ssh root@192.168.0.57
The authenticity of host '192.168.0.57 (192.168.0.57)' can't be established. RSA key fingerprint is fa:1d:0b:91:86:11:76:3a:06:fa:94:e1:ea:bc:c7:f9. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.57' (RSA) to the list of known hosts. root@192.168.0.57's password: \第一次需要输入密码 Last login: Thu May 2 16:28:47 2013 from test2 [root@test1 ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:54:82:19 inet addr:192.168.0.57 Bcast:192.168.0.127 Mask:255.255.255.128 inet6 addr: fe80::20c:29ff:fe54:8219/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:15753998 errors:4 dropped:71 overruns:0 frame:0 TX packets:115445 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1068130239 (1018.6 MiB) TX bytes:20615551 (19.6 MiB) Interrupt:18 Base address:0x2000 |
在test1 上ssh 登录test2
1
2 3 4 5 6 7 8 9 10 11 |
[root@test1 ~]# ssh root@192.168.0.52
The authenticity of host '192.168.0.52 (192.168.0.52)' can't be established. RSA key fingerprint is fa:1d:0b:91:86:11:76:3a:06:fa:94:e1:ea:bc:c7:f9. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.52' (RSA) to the list of known hosts. root@192.168.0.52's password: \第一次需要输入密码 Last login: Thu May 2 17:04:45 2013 from test1 [root@test2 ~]# logout [root@test1 ~]# ssh root@192.168.0.52 Last login: Thu May 2 17:07:53 2013 from test2 [root@test2 ~]# |
经上述验证,均无误,之后便可不用密码登录了!不过线上环境不建议用这种方法!!!