多台linux服务器ssh相互无密码访问,多台linux服务器ssh


A服务器:10.1.11.82

B服务器:10.1.11.210

最终目的:让A服务器能无密码的访问B服务器

 

操作思路:让A服务器单向无密码访问B服务器,只需要在A服务器生成密钥对将生成的公钥上传到服务器B的相关用户目录下的.ssh目录中(没有的话手动创建,注意,它的目录权限是700),并将公钥文件名称改为authorized_keys(注意,这个文件的权限应该是644),请注意.ssh目录及authorized_keys文件的权限,权限不符,会使配置无效。【用 ls -la命令查看所有目录的执行权限】

 

操作方法如下:

1、在服务器A生成密码对:

在生成的过程中有几个选项让你输入密钥对的保存目录及输入私钥,直接enter就行了。

[root@mysqlcluster ~]# 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:
0e:4c:ec:e3:04:98:b0:71:00:91:75:57:ee:56:a1:82 root@mysqlcluster
执行上面一步,会在~/.ssh目录下生成两个文件id_rsa和id_rsa.pub,其中id_rsa是私钥,保存在本机;id_rsa.pub是公钥,是要上传到远程服务器的。

 

2、上传公钥到需要无密码登陆的远程服务器B上并改名为authorized_keys:
远程服务器B上如果没有.ssh目录的话,先手动创建:
[root@www1bak ~]# mkdir .ssh
[root@www1bak ~]# chmod 755 .ssh

然后从服务器A上传公钥文件到远程服务器B:

[root@mysqlcluster ~]# scp .ssh/id_rsa.pub root@10.1.11.210:/root/.ssh/authorized_keys

The authenticity of host ’10.1.11.210 (10.1.11.210)’ can’t be established.
RSA key fingerprint is c9:ef:0c:1b:ac:6c:ef:84:a4:a7:e5:d1:20:58:c8:73.
Are you sure you want to continue connecting (yes/no)? yes                              
Warning: Permanently added ’10.1.11.210′ (RSA) to the list of known hosts.    //这一步会将远程服务器B加入到本机(服务器A)的known_hosts列表中
root@10.1.11.210′s password:
id_rsa.pub                                                                                        100%  399     0.4KB/s   00:00

 

3、测试

上传完公钥文件到远程后,马上从服务器A登陆到服务器B,如果没有输入密码登陆到了服务器B,表示成功,如果还要输入密码,则请检查远程服务器B上的.ssh目录权限是否为700,上传的远程服务器上的公钥名是否改为了authorized_keys,权限是否为644

 

相关内容