RHEL/CentOS 7 多节点SSH免密登陆[附可行性脚本]

实验说明:

在自动化部署时,会经常SSH别的机器操作,然而每次的密码认证却很令人烦躁,尤其是很长的密码,因此SSH免密登陆就显得必不可少;

在机器数目很多的时候,使用更过的往往是Ansible分发并执行SSH免密登陆脚本,使得每台机器之间都能免密登陆。

实验环境

宿主机系统  :Fedora 28 WorkStation
虚拟机管理器 :Virt-Manager 1.5.1
虚拟机配置  :ha1  CentOS 7.2 1511 (minimal)  virbr0: 192.168.122.57
            ha2  CentOS 7.2 1511 (minimal)  virbr0: 192.168.122.58
            ha3  CentOS 7.2 1511 (minimal)  virbr0: 192.168.122.59

实验步骤

1.安装系统并配置网络(所有虚拟机都需联网)

2.先操作第一台虚拟机(ha1)

3.编写主机名与IP的映射关系

[root@ha1 ~]# vi /etc/hosts
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.57    ha1
192.168.122.58    ha2
192.168.122.59    ha3

4.创建公有密钥

[root@ha1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
40:c3:81:eb:60:49:2e:f7:fe:59:bb:ef:7d:ad:bb:06 root@ha2
The key’s randomart image is:
+–[ RSA 2048]—-+
|    o+.        |
|  . ….        |
| o . ..          |
|. * .  .        |
| + +    S        |
|    o      E    |
|  .    .    . . |
|    .  o . .  o .|
|    .o o+o .o++ |
+—————–+

5.发送公有密钥至远程机器

[root@ha1 ~]# ssh-copy-id root@192.168.122.58
[root@ha1 ~]# ssh-copy-id root@192.168.122.59

6.以上是单台虚拟机的逐条执行命令方式,将以上操作写成脚本(脚本在本文末尾PS处)

7.下面操作其他虚拟机(ha2、ha3)

# 虚拟机ha2
[root@ha2 ~]# chmod 777 build-ssh-credit.sh
[root@ha2 ~]# ./build-ssh-credit.sh

# 虚拟机ha3
[root@ha3 ~]# chmod 777 build-ssh-credit.sh
[root@ha3 ~]# ./build-ssh-credit.sh

8.至此,三台虚拟机之间相互访问都无需输入密码,实现了SSH的免密登陆

9.Complete!!!

PS:公钥初始化和实现SSH免密登陆的脚本(build-ssh-credit.sh),直接拷贝就可使用。

#!/usr/bin/bash

# 安装expect,minimal没有此rpm包,需联网或有本地yum
yum install expect -y
expect << EOF
set timeout 10

# 创建公有密钥

spawn ssh-keygen -t rsa
expect {
        “*to save the key” {send “n”;exp_continue}
        “*(y/n)” {send “yr”;exp_continue}
        “Enter passphrase” {send “n”;exp_continue}
        “Enter same passphrase” {send “n”;exp_continue}
}

EOF

#  获取/etc/hosts文件中除localhost的映射关系
ip_list=`grep -v ‘localhost’ /etc/hosts | awk -F ‘ ‘ ‘{print $1,$2}’`
for ip in $ip_list
do
expect << EOF
        set timeout 2

        # 发送公有密钥
        spawn ssh-copy-id root@$ip
        expect {
                “yes/no” {send “yesr”;exp_continue}
                “password” {send “000000r”;exp_continue}
        }

        # 拷贝/etc/hosts文件到远程机器
        spawn scp /etc/hosts $ip:/etc
        expect {
                “yes/no” {send “yesr”;exp_continue}
                “password” {send “rootr”;exp_continue}
        }
EOF
done

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 RHEL/CentOS 7 多节点SSH免密登陆[附可行性脚本] https://www.lizi.tw/soft/10643.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

RHEL/CentOS 7 多节点SSH免密登陆[附可行性脚本]-海报

分享本文封面