1、三台虚拟机关闭防火墙
systemctl stop firework
setenforce 0
10.254.50.30 | test0 |
---|---|
10.254.50.31 | test1 |
10.254.50.32 | test2 |
2、vim /etc/hosts 追加所有ip和域名
10.254.50.30 test0
10.254.50.31 test1
10.254.50.32 test2
3、首先操作test0
ssh-keygen -t rsa -P '' # 生成密钥对,默认存在/root/.ssh/ 下
4、把公玥追加到授权的key里面去
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
vim /etc/ssh/sshd_config
RSAAuthentication yes # 启用 RSA 认证
PubkeyAuthentication yes # 启用公钥私钥配对认证方式
AuthorizedKeysFile .ssh/authorized_keys # 公钥文件路径
5、重启ssh
systemctl restart sshd
# 测试一下
ping localhost
# 可以无密码登录本机
6、 上面实现了本机无密码登录,现在把公玥复制到所有虚拟机上
scp ~/.ssh/id_rsa.pub root@10.254.50.31:~/ # test1
scp ~/.ssh/id_rsa.pub root@10.254.50.32:~/ # test2
7、去test1 和 test2 上 (~/ 目录下已经有id_rsa.pub了)
mkdir ~/.ssh
chmod 700 ~/.ssh
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
vim /etc/ssh/sshd_config # 一定要是root用户
RSAAuthentication yes # 启用 RSA 认证
PubkeyAuthentication yes # 启用公钥私钥配对认证方式
AuthorizedKeysFile .ssh/authorized_keys # 公钥文件路径
# 重启 ssh
systemctl restart sshd
8、此时用test0去连接test1和test2
ssh test1
ssh test2
# 可以免密登录