centos7配置ssh免密码登陆
1.生成密钥,执行命令sh-keygen -t rsa,默认回车
[root@master ~]# 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:
SHA256:mD7BQPCmXfPwkK1OnT9HVFHFYHFNEwia1nSb6UFR1RE root@master
The key's randomart image is:
+---[RSA 2048]----+
| ... o.+XE#|
| o o = o++.=|
| + * .+ ..= |
| + + @.. .. . |
| . . B S .. |
| + . . . |
| + o . |
| . o |
| |
+----[SHA256]-----+
[root@master ~]#
2.将密钥导入到认证文件中
[root@master ~]# cd .ssh/
[root@master .ssh]# cat id_rsa.pub>>authorized_keys
[root@master .ssh]# ll
总用量 12
-rw-r--r--. 1 root root 393 7月 15 15:11 authorized_keys
-rw-------. 1 root root 1675 7月 15 15:08 id_rsa
-rw-r--r--. 1 root root 393 7月 15 15:08 id_rsa.pub
[root@master .ssh]#
3.将集群中所有主机ip地址和对应的主机名添加到hosts文件中
[root@master .ssh]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.101 master
192.168.1.102 slave1
192.168.1.103 slave2
192.168.1.104 slave3
4.然后将master的authorized_keys传到slave1上的.ssh目录下
[root@master .ssh]# scp -r authorized_keys root@slave1:~/.ssh
The authenticity of host 'slave1 (192.168.1.102)' can't be established.
ECDSA key fingerprint is SHA256:NmkN2Uza23ugzRxBDc/xnUDJ/YRUMNBE3DleGPhyFUo.
ECDSA key fingerprint is MD5:65:22:2c:ed:83:9c:9c:18:f9:9f:d5:d6:ab:4a:88:d7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave1,192.168.1.102' (ECDSA) to the list of known hosts.
root@slave1's password:
authorized_keys 100% 393 161.7KB/s 00:00
[root@master .ssh]#
5.将slave1的密钥添加到认证文件中,重复以上步骤,直至所有的密钥都添加到认证文件中,最后将包含所有主机密钥的认证文件分发到每一台主机上覆盖原文件