实验说明:

在自动化部署时,会经常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. [Java]ArrayList、LinkedList、Vector、Stack的比较

    一.介绍 先回顾一下List的框架图 由图中的继承关系,可以知道,ArrayList.LinkedList.Vector.Stack都是List的四个实现类. AbstractList是一个抽象类,它 ...

  2. Codeforces Round #563 (Div. 2) A. Ehab Fails to Be Thanos

    链接:https://codeforces.com/contest/1174/problem/A 题意: You're given an array aa of length 2n2n. Is it ...

  3. 洛谷 P3676 小清新数据结构题

    https://www.luogu.org/problemnew/show/P3676 这题被我当成动态dp去做了,码了4k,搞了一个换根的动态dp #include<cstdio> #i ...

  4. 072 Edit Distance 编辑距离

    给出两个单词 word1 和 word2,找出将 word1 转换成 word2 所使用的最少的步骤数 (每个操作记为一步).你可以对一个单词进行以下三种操作:a) 插入一个字符b) 删除一个字符c) ...

  5. final关键字,类的自动加载,命名空间

    final关键字 1.final可以修饰方法和类,但是不能修饰属性: 2.Final修饰的类不能被继承: 3.Fina修饰的方法不能被重写,子类可以对已被final修饰的父类进行访问,但是不能对父类的 ...

  6. [摘录]全面学习GFW

    转载自:https://cokebar.info/archives/253 GFW会是一个长期的存在.要学会与之共存,必须先了解GFW是什么.做为局外人,学习GFW有六个角度.渐进的来看分别是: 首先 ...

  7. C#oracle还原imp实例

    C#来做oracle还原,调用oracle自带函数imp.exe时,需要注意的是:1.imp.exe 中fromuser 和touser两个关键字: fromuser与exp.exe中的owner对应 ...

  8. spring boot + spring data jpa

    Spring Data Repository的核心接口是Repository(好像也没什么好惊讶的).这个接口需要领域类(Domain Class)跟领域类的ID类型作为参数.这个接口主要是让你能知道 ...

  9. jquery 一键复制到剪切板

    今天做项目有一个功能,通过点击事件复制一段文本到剪切板,在网上找了一些,整理了一下,方便需要的朋友使用. <a id="copy" data-text="12345 ...

  10. 【extjs6学习笔记】1.5 初始:关于布局

    absolute 绝对布局,这个布局使用 x 和 y 属性来指定组件的绝对定位 accordion 手风琴布局[可折叠布局]这个布局展示了在一个时间里只有一个内置的可支持折叠和展开的子级 panel ...