最近老是在群里看到许多同学问,linux之间无密码互访怎么弄啊,百度的看得我头晕之类的,所以我就写写怎么样在两台linux服务器之间实现无密码互访,也就是让它们互相信任的意思,废话不多说,直接上菜。
环境介绍
两台CentOS服务器,主机名和IP分别是: server1 192.168.1.69 server2 192.168.1.70
准备
- #两台都要安装
- yum install -y openssh-clients
分别打开两台服务器的hosts文件,在文件末尾添加以下内容,让主机名和IP对应。(还有就是别忘了分别修改两台服务器的/etc/sysconfig/network文件里的HOSTNAME,改成对应的服务器的主机名即可。)
- vim /etc/hosts
- server1 192.168.1.69
- server2 192.168.1.70
开始
准备工作做好了,现在就可以开工了,首先在server1运行ssh-keygen,直接一路回车
- [[email protected] ~]# ssh-keygen
- 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:
- d8:0c:6c:f8:44:a3:61:c3:53:5c:09:93:2d:1d:d1:82 [email protected]
- The key‘s randomart image is:
- +--[ RSA 2048]----+
- | .+o*B+= |
- | .oBEo= . |
- | o.=. . |
- | + = |
- | o S |
- | |
- | |
- | |
- | |
- +-----------------+
切换到server2,同上。
回到server1,运行ssh-copy-id -i .ssh/id_rsa.pub [email protected]
- [[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
- The authenticity of host ‘server2 (192.168.1.70)‘ can‘t be established.
- RSA key fingerprint is 99:3b:ea:56:48:73:07:08:79:1f:69:b7:f2:a8:01:5c.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added ‘server2,192.168.1.70‘ (RSA) to the list of known hosts.
- [email protected]‘s password: #输入对方服务器root密码
- Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
- .ssh/authorized_keys
- to make sure we haven‘t added extra keys that you weren‘t expecting.
切换到server2,运行ssh-copy-id -i .ssh/id_rsa.pub [email protected]
现在,你就可以不需要输入对方服务器的密码而直接登录了,试试看吧 O(∩_∩)O
时间: 2024-10-12 02:52:56