使得每台机器之间都能免密登陆

尝试验证:

www.845566.com,使得每台机器之间都能免密登陆。在自动化陈设时,会临时SSH别的机器去操作,不过每回的密码验证却很令人窝火,特别是相当长的密码,因而SSH免密登录就显得必要;

在机器数目非常多的时候,使用更过的高频是Ansible分发并实行SSH免密登录脚本,使得每台机械之间都能免密登录。

使得每台机器之间都能免密登陆。尝试步骤:

  1. ### 安装系统并计划互联网(全数虚构机都需联网)

  2. 使得每台机器之间都能免密登陆。### 先操作第风流倜傥台设想机(ha1)

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

    1 [root@ha1 ~]# vi /etc/hosts
    2 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    3 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    4 192.168.122.57    ha1
    5 192.168.122.58    ha2
    6 192.168.122.59    ha3
    
  4. ### 创立公有密钥

     1 [root@ha1 ~]# ssh-keygen -t rsa 
     2 Generating public/private rsa key pair.
     3 Enter file in which to save the key (/root/.ssh/id_rsa): 
     4 /root/.ssh/id_rsa already exists.
     5 Overwrite (y/n)? y
     6 Enter passphrase (empty for no passphrase): 
     7 Enter same passphrase again: 
     8 Your identification has been saved in /root/.ssh/id_rsa.
     9 Your public key has been saved in /root/.ssh/id_rsa.pub.
    10 The key fingerprint is:
    11 40:c3:81:eb:60:49:2e:f7:fe:59:bb:ef:7d:ad:bb:06 root@ha2
    12 The key's randomart image is:
    13 +--[ RSA 2048]----+
    14 |     o+.         |
    15 |  . ....         |
    16 | o . ..          |
    17 |. * .  .         |
    18 | + +    S        |
    19 |    o       E    |
    20 |   .    .    . . |
    21 |    .  o . .  o .|
    22 |     .o o+o .o++ |
    23 +-----------------+
    
  5. ### 发送公有密钥至远程机器

    1 [root@ha1 ~]# ssh-copy-id root@192.168.122.58
    2 [root@ha1 ~]# ssh-copy-id root@192.168.122.59
    
  6. ### 以上是单台设想机的各样实行命令的办法,将上述操作写成脚本(脚本在本文末尾PS处)

  7. ### 上边操作其余虚构机(ha2、ha3)

    1 # 虚拟机ha2
    2 [root@ha2 ~]# chmod 777 build-ssh-credit.sh 
    3 [root@ha2 ~]# ./build-ssh-credit.sh 
    
    1 # 虚拟机ha3
    2 [root@ha3 ~]# chmod 777 build-ssh-credit.sh 
    3 [root@ha3 ~]# ./build-ssh-credit.sh 
    
  8. ### 至此,三台虚构机之间人机联作走访都不必要输入密码,实现了SSH的免密登入

  9. ### Complete!!!

PS:公钥开头化和兑现SSH免密登入的剧本(build-ssh-credit.sh卡塔尔(英语:State of Qatar),一贯拷贝就可使用。

#!/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

 

实验遇到:

  • 宿主机系统   :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
You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图