Linux快速配置集羣ssh互信

以前在《記錄一則Linux SSH的互信配置過程》、《Vertica 7.1安裝最佳實踐(RHEL6.4)》中,都分別提到了配置ssh互信的方法,本文在此基礎上進一步整理配置ssh互信的方法,目的是將步驟儘量的簡化,從而更加適合在較大規模的集羣中對ssh互信進行快速配置。html

場景:適合較大規模集羣ssh互信配置.node

1.配置節點1的/etc/hosts文件

節點1編輯/etc/hosts文件,添加集羣全部主機的IP地址和其對應的主機名:
vi /etc/hosts安全

192.168.56.102 JY-DB
192.168.56.103 JY-DB2

2.在節點1新建2個腳本

在節點1上傳兩個安裝腳本到/usr/local/bin目錄下。bash

腳本1:用來集羣間同步拷貝文件。
cluster_copy_all_nodesssh

#!/bin/bash
SELF=`hostname`
if [ -z "$NODE_LIST" ]; then
  echo
  echo Error: NODE_LIST environment variable must be set in .bash_profile
  exit 1
fi

for i in $NODE_LIST; do
  if [ ! $i = $SELF ]; then
    if [ $1 = "-r" ]; then
      scp -oStrictHostKeyChecking=no -r $2 $i:$3
    else
      scp -oStrictHostKeyChecking=no $1 $i:$2
    fi
  fi
done
wait

腳本2:用來集羣間同步運行命令。
cluster_run_all_nodescode

#!/bin/bash
if [ -z "$NODE_LIST" ]; then
  echo
  echo Error: NODE_LIST environment variable must be set in .bash_profile
  exit 1
fi

if [[ $1 = '--background' ]]; then
  shift
  for i in $NODE_LIST; do
    ssh -oStrictHostKeyChecking=no -n $i "$@" &
  done
else
  for i in $NODE_LIST; do
    ssh -oStrictHostKeyChecking=no $i "$@"
  done
fi
wait

授予兩個腳本的可執行權限:htm

chmod +x /usr/local/bin/cluster_*

3.配置節點1的環境變量

配置節點1的環境變量:
vi ~/.bash_profileblog

export NODE_LIST='JY-DB JY-DB2'

將集羣中全部的主機名稱列出,而後從新登陸當前會話,或者執行下面命令使環境變量生效:get

source ~/.bash_profile

4.配置整個集羣間的ssh互信

4.1 各節點ssh-keygen生成RSA密鑰和公鑰同步

cluster_run_all_nodes "hostname; ssh-keygen -q -t rsa  -N \"\" -f  ~/.ssh/id_rsa"

輸出示例:

[root@JY-DB bin]# cluster_run_all_nodes "hostname ; ssh-keygen -q -t rsa  -N \"\" -f  ~/.ssh/id_rsa"
root@jy-db's password: 
JY-DB
root@jy-db2's password: 
JY-DB2

若是配置有誤,或者清除ssh互信的當前全部配置信息:

cluster_run_all_nodes "hostname ; rm -rf ~/.ssh"
rm -rf ~/.ssh

4.2 將全部的公鑰文件彙總到一個總的受權key文件中
在192.168.56.102執行彙總:
編輯腳本內容:

IP_NET="192.168.56."
for((i=102;i<=103;i++))
do
ssh $IP_NET$i cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
echo Summarize ssh info from $IP_NET$i into a single file.
done

注意:IP_NET是定義網段的變量,for循環包含了整個集羣的IP範圍,根據實際狀況修改。

上述腳本內容是能夠直接拷貝執行的,結果示例:

[root@JY-DB ~]# IP_NET="192.168.56."
[root@JY-DB ~]# for((i=102;i<=103;i++))
> do
> ssh $IP_NET$i cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
> echo Summarize ssh info from $IP_NET$i into a single file.
> done
root@192.168.56.102's password: 
Summarize ssh info from 192.168.56.102 into a single file.
root@192.168.56.103's password: 
Summarize ssh info from 192.168.56.103 into a single file.

出於安全性考慮,將這個受權key文件賦予600權限:

chmod 600 ~/.ssh/authorized_keys

4.3 將這個包含了全部互信機器認證key的認證文件,分發到各個機器中去

cluster_copy_all_nodes ~/.ssh/authorized_keys ~/.ssh/

4.4 驗證ssh互信
節點1運行,都不輸入密碼返回主機名和時間便可:

cluster_run_all_nodes "hostname;date"

至此,ssh集羣間的互信已經配置完成。
但爲了更加靈活的再其餘節點也能夠用到咱們自定義的腳本,咱們還能夠作如下工做:

同步拷貝節點1的配置文件到其餘節點:

cluster_copy_all_nodes ~/.bash_profile ~/
cluster_copy_all_nodes /etc/hosts /etc
cluster_copy_all_nodes /usr/local/bin/cluster_copy_all_nodes /usr/local/bin/
cluster_copy_all_nodes /usr/local/bin/cluster_run_all_nodes /usr/local/bin/

這時任意登陸其餘節點,也能夠使用cluster_run_all_nodes驗證ssh互信了:

cluster_run_all_nodes "hostname;date"
相關文章
相關標籤/搜索