作者:手机用户2502895961 | 来源:互联网 | 2023-07-16 13:41
免密登录比较常用,例如场景:
三个虚拟机:
192.168.2.2
192.168.2.4
192.168.2.5
使用一个kkk.sh脚本用于启动和停止kafka集群,希望通过在192.168.2.2上执行脚本批量启动和停止
![](https://img1.php1.cn/3cdc5/32ca/882/8880fe19416b5b2f.png)
如果没设置免密登录,执行脚本需要输入系统的密码
![](https://img1.php1.cn/3cdc5/32ca/882/84216dd16a58c423.png)
设置免密登录步骤
192.168.2.2下
执行命令:
root@localhost bin]# ssh-keygen -t rsa
连续回车
![](https://img1.php1.cn/3cdc5/32ca/882/4b87e1fcaf938e89.png)
![](https://img1.php1.cn/3cdc5/32ca/882/7bf8645faa592b2f.png)
把本地主机的公钥复制到另外两台机器的authorized_keys文件
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.2.5
![](https://img1.php1.cn/3cdc5/32ca/882/f469560eb12d970f.png)
根据场景,需要把当前机器也需要复制到自己的authorized_keys文件
![](https://img1.php1.cn/3cdc5/32ca/882/15e061d273156bd6.png)
备注:需要A机器免密登录其他系统,则需要把A机器id_rsa.pub公钥文件,复制其他机器的authorized_keys文件,步骤和上面一致
检查效果
如下,192.168.2.2可以免密登录其他系统
![](https://img1.php1.cn/3cdc5/32ca/882/039b5fb3833d3a69.png)
或者可以执行ssh登录看下
未免密(需要输入密码)
![](https://img1.php1.cn/3cdc5/32ca/882/766e4672aecf469d.png)
已免密(不需要输入密码)
![](https://img1.php1.cn/3cdc5/32ca/882/6d42bd5f3073e50e.png)