操作系统:Windows10家庭版

Linux版本:Centos 7 x64(三台)

远程工具:Xshell 6


# 生成密钥(公钥)三台机器

ssh-keygen -t rsa

解释:

Enter file in which to save the key (/root/.ssh/id_rsa):密钥的生成路径保存的位置 (/root/.ssh/id_rsa),如果默认即回车,不默认即在冒号后方输入自己的路径

/root/.ssh/id_rsa already exists.
Overwrite (y/n)?出现这个是因为之前配置过(生成过)密钥,询问是否覆盖,y是覆盖,n是不覆盖。

Enter passphrase (empty for no passphrase):输入密钥的密码,不需要直接回车

Enter same passphrase again:再次输入相同的密码(和上一步一样,跳过。如果上一步输入了密码这一步就要输入同样的密码。)

回车密钥生成

# 三台主机汇总密钥到一台机器:复制ssh密钥

ssh-copy-id 主机名

举个栗子👨‍💻:

master主机:

ssh-copy-id master

slave1主机:

ssh-copy-id master

slave2主机:

ssh-copy-id master

也就是三台主机都重复一个命令,将自己的密钥汇总到一台机器,方便后边的发送。

 

前提:vi /etc/hosts 需要配置三台主机:

[root@localhost .ssh]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.38.137 master
192.168.38.138 slave1
192.168.38.139 slave2

划掉的是需要你自己的ip


完成后进入master主机的/root/.ssh目录,ls查看有无authorized_keys,发送给三台主机:

scp /root/.ssh/authorized_keys root@slave1:/root/.ssh

scp /root/.ssh/authorized_keys root@slave2:/root/.ssh


ssh去尝试连接看能否无需密码连接上。

ssh master(slave1,slave2)

如果提示Are you sure you want to continue connecting (yes/no)?解释:是否确实要继续连接(是/否)?

输入yes

无需密码登陆成功即可。

验证方法,改名后exit退出:

演示:

[root@localhost .ssh]# ssh slave1
The authenticity of host 'slave1 (192.168.38.138)' can't be established.
ECDSA key fingerprint is SHA256:5sNoOMjdYz0QmvUYBaP8HTUgbfEGUyoHT8k3l8t2XPk.
ECDSA key fingerprint is MD5:53:**:e2:d1:ae:**:**:**:**:**:af:9a:b1:83:c9:16.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave1,192.168.38.138' (ECDSA) to the list of known hosts.
Last login: Wed Nov 4 16:01:59 2020 from slave2
[root@localhost ~]# hostname
anaconda-ks.cfg .bash_profile .config/ .tcshrc
.bash_history .bashrc .cshrc .viminfo
.bash_logout .cache/ .ssh/
[root@localhost ~]# hostname slave1
[root@localhost ~]# bash
[root@slave1 ~]# exit
exit
[root@localhost ~]#

改了名字,立即生效“bash”发现主机名字变化为slave1,退出“exit”后发现主机名字又变成了“localhost”

试验成功。