实验说明:

在自动化部署时,会经常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 "y\r";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 "yes\r";exp_continue}
"password" {send "000000\r";exp_continue}
} # 拷贝/etc/hosts文件到远程机器
spawn scp /etc/hosts $ip:/etc
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "root\r";exp_continue}
}
EOF
done

[提供可行性脚本] RHEL/CentOS 7 多节点SSH免密登陆的更多相关文章

  1. shell脚本配置ssh免密登陆

    通过shell脚本配置免密登陆,分为两个脚本,一个是配置文件config.env,一个是正式脚本sshkey.sh. # config.envexport HOST_USER=(root) expor ...

  2. 多节点ssh免密匙登录

    1,在所有节点上,使用yourname用户名执行: ssh-keygen -t dsa -P '' -f /home/yourname/.ssh/id_dsa 2,在node1的/home/yourn ...

  3. [提供可行性脚本] RHEL 7/CentOS 7/Fedora28 重命名网卡名称

    实验说明: 在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(CentOS7/RHEL7),网卡命名采用“一致性网络设备接口”的命名方法,导致不同设备的不同网卡名称各 ...

  4. Centos下 自动化配置SSH免密码登陆

    hosts文件,存储要部署的节点IP地址,其中以#开头表示注释掉 192.168.101.52 192.168.101.53 192.168.101.54 192.168.101.55 192.168 ...

  5. CentOS SSH免密登陆

    #环境说明客户机:Mac OS X服务器:CentOS 6.5客户端:OpenSSH,OS X及大多数Linux都内置了OpenSSH.’ssh -v’命令可以查看版本. #大致流程1.在客户机创建一 ...

  6. centos或者ubuntu设置ssh免密码登陆

    1. 输入  # ssh-keygen -t rsa -P ""  然后一路回车 2.输入  # cat ~/.ssh/id_rsa.pub >> ~/.ssh/aut ...

  7. Centos 集群配置SSH免登陆脚本

    首先编写脚本生成集群服务器列表: hostsList.sh #!/bin/bash preIp="11.11.225." pwd="dyj2017" for i ...

  8. Shell脚本实现SSH免密登录及批量配置管理

    本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...

  9. CentOS 6.5配置SSH免密码登录

    centos 系统对权限的设置非常微妙,如果权限设置大了则ssh 拒绝,如果权限小了,则ssh 更是被拒绝(我曾经配置好久没有打通,就是因为权限过大的原因) 参考链接:http://www.linux ...

随机推荐

  1. NOI2015品酒大会 后缀数组

    自己尝试敲后缀数组,发现难看(tiao)的不行,于是抄了板子 考虑建出hei以后转化出的问题: 对于一个数组中权值大于等于k的连续部分,求取两个数的方案数和两数积的最大值 (好气啊,可以有负数) 把询 ...

  2. openstack安装newton版本dashboard+cinder(六)

    一.dashboard 1.安装dashboard及配置 [root@linux-node1 ~]# yum install openstack-dashboard -y #可以装任何地方只要能连接 ...

  3. openstack安装newton版本keyston部署(一)

    一.部署环境:     两台centos7,  内存2G 控制计算节点: Hostname1:                    ip:172.22.0.218 计算节点及存储节点 Hostnam ...

  4. Spark Mllib里的Mllib基本数据类型(图文详解)

    不多说,直接上干货! Spark Mllib基本数据类型,根据不同的作用和应用场景,分为四种不同的类型 1.Local  vector : 本地向量集,主要向spark提供一组可进行操作的数据集合 2 ...

  5. 在东京生活的中国IT程序员

    应之前文章的博友邀请,我来开一篇在日本东京生活的中国IT程序员自谈,文中的讨论对象多为我自己或者是我的中国人(前)同事,有以偏概全之处还请包涵. 首先,我之前说日本的IT并不发达,不发达到什么程度呢? ...

  6. document.getElementById(...) is null

    <html> <head> <script type="text/javascript"> document.getElementById('b ...

  7. php时间戳存在8小时误差

    当将PHP时间戳转化为正常的时间格式一般的操作方法如下: $mytime=time(); echo $mytime.'<br />'; echo date('Y-m-d H:i:s',$m ...

  8. P3930 SAC E#1 - 一道大水题 Knight

    TLE,额 ,有空再写吧. #include<queue> #include<cstdio> #include<vector> #include<algori ...

  9. JAVA常量介绍

    常量: 在程序执行过程中,其值不发生改变的量: 1.分类:     字面值常量和自定义常量: 1.字面值常量有以下几种: 字符串常量.小数常量.整数常量.字符常量.布尔常量(true.false).空 ...

  10. Appium基础五:appium相关API

    1.获取信息类: 1.1 获取当前界面的组件: driver.currentActivity(); //获取当前界面的activity,可用于断言是否跳转到预期的activity 1.2 获取当前页面 ...