Linux多台服务器共用密钥ssh自动登陆

linux的秘钥产生与服务器无关,只和加密的方式(采用rsa或dsa)还有passphrase(密码短语,在生成秘钥的时候输入)有关。
如果是这样,同一对秘钥可以使用在多台服务器上,因为对于服务器和客户端来说,他们在通信的时候只需验证秘钥和公钥是否匹配。
即存在一个global的公钥存放在ssh服务器上,而多台客户端则使用的同一秘钥登陆ssh服务器上。


测试环境:本机windows使用secureCRT客户端,两台redhat 6.3的虚拟机(linuxA和B,192.168.1.2/3)。虚机与主机使用桥接网络,处于同一局域网。


测试过程:
注:公钥一般是pub结尾,但是服务器验证的文件是authorized_key,所以要把pub文件的内容转入authorized_key。pub文件本身没用。
1、使用CRT生成秘钥对,将公钥上传到linuxA,成功登陆后,cpoyA机中的公钥至B机中,实现CRT自动登陆B机,验证公钥为通用。
首先使用CRT生成秘钥:
1.使用SecureCRT创建私钥和公钥(Set Passphrase 可以设置为空密码,比较方面验证)
SecureCRT: Quick Connect -> Authentiation -> Public Key -> Properties -> Create Identity File -> DSA/RSA -> Set Passphrase -> Done
这个时候在指定目录会生成两个文件,例如,私钥my_rsa和公钥my_rsa.pub
2.linux服务器上建立.ssh目录,一般情况下,已经有这个目录(更改权限很重要,认证的时候权限不是700不给通过)
# mkdir /root/.ssh
# chmod 700 /root/.ssh
3.将公钥 my_rsa.pub 传到linux服务器,将SSH2兼容格式的公钥转换成为Openssh兼容格式(查看/etc/ssh/sshd_config文件的#AuthorizedKeysFile这行是否为ssh/authorized_keys,有说法是自ssh3版本之后就不默认为ssh/authorized_keys2了)
# ssh-keygen -i -f Identity.pub >> /root/.ssh/authorized_keys
# chmod 600 /root/.ssh/authorized_keys
4.在SecureCRT里面设置登录模式为PublicKey,并选择刚刚创建的my_rsa文件作为私钥
5.重启Linux服务器上SSH服务器(测试貌似不用重启服务也能生效)
#service sshd restart 或者 /etc/rc.d/init.d/sshd restart


此阶段,测试成功,A和B机使用的同一对authorized_keys2,CRT都能实现自动登陆。


2、在B机中生成秘钥对,将公钥复制到A中,实现B机自动登陆A,然后将B的秘钥传过去,把A的authorized_keys2内容写入B的authorized_keys2文件中,实现A自动登陆B。


【Linux多台服务器共用密钥ssh自动登陆】步骤1: 用 ssh-key-gen 在本地主机上创建公钥和密钥
local-host$ ssh-keygen -trsa


Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is: 33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9
local-host


步骤2: 用 ssh-copy-id 把公钥复制到远程A主机上
local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pubroot@192.168.1.2
remote-host‘s password:
Now try logging into the machine, with ―ssh ?remote-host‘‖, and check in:
.ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting.
[注: ssh-copy-id 把密钥追加到远程主机的 .ssh/id_rsa 上.]


步骤3: 直接登录A远程主机
local-host$ ssh root@192.168.1.2
Last login: Sat Mar8 12:37:48 2014 from 192.168.1.3
[注: SSH 不会询问密码.]


然后通过sftp,将B机的id_rsa传到A机,把A的authorized_keys2传过来。此时A和B都可互相自动登陆。


测试结果:验证猜想成功,最好是有第三台linux再可以验证下就好了。


补充安全问题:由于.ssh文件夹和privatekey都权限为700和600,同时sftp服务器只开通sftp登陆权限和控制home文件目录,只要妥善保管privatekey,在ssh协议下是没有安全顾虑的。参见SSH认证原理(http://qujunorz.blog.51cto.com/6378776/1371344)
https://www.ibm.com/developerworks/cn/linux/security/openssh/part1/


转载于:https://blog.51cto.com/xiexf/1962504

    推荐阅读