扩大
缩小

Linux两台主机之间建立信任(ssh免密码)

背景: 有时候我们在两个主机之间复制文件的时候,提示输入密码,很不方便,那如何免密码复制呢?,就是使用通过linux公钥和秘钥,建立双机信任关系。

在整理之前,我先说下ssh免密码的要点 : 

你想免密码登陆到哪个主机哪个用户, 就把你自己的公钥文件内容追加到远程主机对应用户下的authorized_keys文件中(对面可能没有这个文件,创建即可)。

1. 生成秘钥,并添加信任

我的环境中node1的ip是192.168.168.201,node2的ip是192.168.168.202.

[root@node1 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa         #生成rsa
[root@node1 ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.168.202  #复制201的公钥到202机器上,这样就可以使用在201机器上免密码登录202机器了。

[root@node2 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa         #生成rsa
[root@node2 ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.168.201  #复制202的公钥到201机器上,这样就可以使用在202机器上免密码登录201机器了。

注意:

  •   如果远程主机的端口非22端口,需要指定-p port选项。
  •   ssh-copy-id是由openssh-clients包提供,没有这个命令可以安装这个包。
  •        centos6,7使用ssh-copy-id的时候可以不用指定-i选项,centos5必须指定的。

2.信任自己主机

[root@node1 ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.168.201 
[root@node2 ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.168.202 

3.测试

[root@node1 ~]# ssh 192.168.168.202 'ip addr show dev eth0 '
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:3f:42:13 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.202/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::250:56ff:fe3f:4213/64 scope link 
       valid_lft forever preferred_lft forever

[root@node2 ~]# ssh 192.168.168.201 'ip addr show dev eth0'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c9:20:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.201/24 brd 192.168.168.255 scope global eth0
    inet 192.168.168.200/24 brd 192.168.168.255 scope global secondary eth0
    inet6 fe80::20c:29ff:fec9:2088/64 scope link 
       valid_lft forever preferred_lft forever

 4 自动配置免密码登陆

这里有个情况, 有一个跳板机,内部好多自己的服务器。远程只能通过跳板机远程内部服务器,现在需要在跳板机上免密码登陆内部的服务器。

4.1 各个机器上面需要先生成各自的公钥和密码

4.2 跳板机编写脚本和配置文件如下

#!/bin/bash 
#================================================
#FileName   :expect_ssh.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2018-01-05 08:26:06
#Version    :V1.0
#Other      :
#================================================
host_username_password_file=hosts.txt

# install expect 
rpm -q expect &>/dev/null || yum install -yq expect &>/dev/null

# create id_rsa.pub file 
pubkey=~/.ssh/id_rsa.pub
if [ ! -e "$pubkey" ] ; then
        ssh-keygen  -P "" -t rsa  -f ~/.ssh/id_rsa
fi
while read host username password ; do
        con=${username}"@"${host}
        echo $password
        expect <<EOF
        set timeout 20 
        spawn ssh-copy-id $con
        expect {
                "yes/no"  { send "yes\n" ; exp_continue }
                "password:" { send "${password}\n"; exp_continue }
        }
EOF
done < $host_username_password_file

样例的配置文件是这样的。

[root@centos74 bin]$ cat hosts.txt 
172.18.46.6 root oracle

 4.3 集群主机免密码配置

上面的跳板机配置免密码登陆都是单向的, 但是对于集群环境我们需要配置任意2个主机的主机信任的。

 

#!/bin/bash 

# 配置项
# 文件格式是 ip usename password port 中间使用空格分割
host_username_password_file=/root/hosts.txt 

# 配置是否copy文件到其他目标主机上去,只有第一个执行的主机需要copy,其他不需要的
need_copy_to_other=0
src_host="current_host_ip"
#echo $#
if [ "$#" -eq 0 ] ; then 
    need_copy_to_other=1
else
    src_host=$1
fi    

# 安装 expect
rpm -q expect &> /dev/null || yum install -yq expect &>/dev/null 

# 创建 秘钥文件 

pubkey=~/.ssh/id_rsa.pub 
prikey=~/.ssh/id_rsa

if [ ! -e "$pubkey" ]  ; then 
    ssh-keygen -P "" -t rsa -f $prikey &>/dev/null
fi 
while read host username password port ; do 
    if [ -z "$host" ] ; then 
        continue
    fi    
    printf "%16s=======>%-16s\n" $src_host  $host

    #echo "==============================$host $username $password $port ========================================= "
    con=${username}"@"${host}
    cmd_with_argv="ssh-copy-id  -p $port $con "
    #echo $password 
    expect  <<-EOF 
    set timeout 600 
    log_user 0
    spawn -noecho $cmd_with_argv
    expect {
        "yes/no" { send "yes\n"; exp_continue } 
        "password:" { send "${password}\n"; exp_continue } 
    } 
    EOF
    &> /dev/null

    if [ "$need_copy_to_other" -eq 1 ] ; then 
        ip a | grep $host &> /dev/null
        if [ "$?" -ne 0 ] ; then 
            #echo "==>复制必要的文件到远程主机($host)上去"
            scp -q -p -P $port  $host_username_password_file $host:$host_username_password_file
            scp -q -p -P $port  $0  $host:$0
            #echo "==>在目标主机${host}上执行下"
            ssh -f -p $port $con "$0  $host "
        fi
    fi

done < $host_username_password_file
sleep 2
[root@localhost ~]# cat hosts.txt 
192.168.46.151 root oracle 22
192.168.46.152 root oracle 22
192.168.46.153 root oracle 22
192.168.46.154 root oracle 22
192.168.46.157 root oracle 22

 

使用方式

[root@localhost ~]# /root/expect.sh 
 current_host_ip=======>192.168.46.151  
 current_host_ip=======>192.168.46.152  
 current_host_ip=======>192.168.46.153  
  192.168.46.152=======>192.168.46.151  
  192.168.46.152=======>192.168.46.152  
  192.168.46.152=======>192.168.46.153  
 current_host_ip=======>192.168.46.154  
  192.168.46.153=======>192.168.46.151  
  192.168.46.152=======>192.168.46.154  
  192.168.46.153=======>192.168.46.152  
  192.168.46.152=======>192.168.46.157  
  192.168.46.153=======>192.168.46.153  
 current_host_ip=======>192.168.46.157  
  192.168.46.154=======>192.168.46.151  
  192.168.46.153=======>192.168.46.154  
  192.168.46.154=======>192.168.46.152  
  192.168.46.153=======>192.168.46.157  
  192.168.46.154=======>192.168.46.153  
  192.168.46.154=======>192.168.46.154  
  192.168.46.154=======>192.168.46.157 

 

posted on 2017-05-13 09:51  LinuxPanda  阅读(8937)  评论(1编辑  收藏  举报

导航