配置ssh无密钥登陆

ssh 无密码登录要使用公钥与私钥。

linux下可以用用ssh-keygen生成公钥/私钥对,下面以CentOS为例。

有机器LxfN1(192.168.136.128),LxfN2(192.168.136.129)。现想LxfN1通过ssh免密码登录到LxfN2。

1.在LxfN1 机下生成公钥/私钥对。
   [hadoop@LxfN1 ~]$ ssh-keygen -t rsa -P ''
   -P表示密码,-P '' 就表示空密码,也可以不用-P参数。
   它在/home/hadoop下生成.ssh目录,.ssh下有id_rsa和id_rsa.pub。

2.把LxfN1 机下的id_rsa.pub复制到LxfN2 机下
[hadoop@LxfN1 ~]$ scp .ssh/id_rsa.pub hadoop@192.168.1.181:/home/hadoop/id_rsa_1.pub
hadoop@192.168.136.129's password:
id_rsa.pub 100% 223 0.2KB/s 00:00

由于还没有免密码登录的,所以要输入密码。

3.追加id_rsa_1.pub内容到LxfN2机的.ssh/authorized_keys文件里

[hadoop@LxfN2 ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[hadoop@LxfN2 ~]$ chmod 600 .ssh/authorized_keys

authorized_keys的权限要是600。

4.LxfN1机登录LxfN2机。
[hadoop@A ~]$ ssh 192.168.1.181
The authenticity of host '192.168.1.181 (192.168.1.181)' can't be established.
RSA key fingerprint is 00:a6:a8:87:eb:c7:40:10:39:cc:a0:eb:50:d9:6a:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.181' (RSA) to the list of known hosts.
Last login: Thu Jul 3 09:53:18 2008 from hadoop
[hadoop@B ~]$

第一次登录是时要你输入yes。

现在LxfN1机可以无密码登录LxfN2机了。

小结:登录的机子可有私钥,被登录的机子要有登录机子的公钥。

这个公钥/私钥对一般在私钥宿主机产生。

上面是用rsa算法的公钥/私钥对,当然也可以用dsa(对应的文件是id_dsa,id_dsa.pub)

想让LxfN1,LxfN2机无密码互登录,那LxfN2机以上面同样的方式配置即可。

多机无密码互访总结:
1、可以分别生成id_rsa.pub, 拷贝到第一台机器上成id_rsa_x.pub后,
2、追加所有的id_rsa_x.pub到authorized_keys
3、拷贝汇总的authorized_keys到所有机器上。
验证多机之间的ssh互访

原文地址:https://www.cnblogs.com/lexiaofei/p/6760449.html