最近拋棄非ssh連線的hadoop叢集部署方式了,還是回到了用ssh key 驗證的方式上了。這裡面就有些麻煩,每臺機器都要上傳公鑰。恰恰我又是個很懶的人,所以寫幾個小指令碼完成,只要在一臺機器上面就可以做公鑰的分發了。

首先是生成ssh key指令碼

#!/bin/sh
ssh-keygen -t rsa -P `` -f ~/.ssh/id_rsa
cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys

ssh-keygen一般來說需要輸入passphrase,但是一般都是三個回車過去了,我懶的輸入,加上-P “就不用了。

然後是新增公鑰到從節點的指令碼

#!/bin/sh
read -p "輸入遠端伺服器IP: " ip
ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa.pub root@$ip
ssh root@$ip `sed -i "s/^#RSAAuthentication yes/RSAAuthentication yes/g" /etc/ssh/sshd_config`
ssh root@$ip `sed -i "s/^#PubkeyAuthentication yes/PubkeyAuthentication yes/g" /etc/ssh/sshd_config`
ssh root@$ip `sed -i "s/^#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config`
ssh root@$ip `service sshd restart`
hostname=`ssh root@${ip} `hostname``
echo "新增主機名和IP到本地/etc/hosts檔案中"
echo "$ip    $hostname" >> /etc/hosts
echo "遠端主機主機名稱為$hostname, 請檢視 /etc/hosts 確保該主機名和IP新增到主機列表檔案中"
echo "主機公鑰複製完成"

然後是第三個指令碼讀取主機列表然後把/etc/hosts複製到所有主機上

#!/bin/sh
cat /etc/hosts | while read LINE
do
    ip=`echo $LINE | awk `{print $1}` | grep -v "::" | grep -v "127.0.0.1"`
    echo "Copying /etc/hosts to ${ip}"
    scp -o StrictHostKeyChecking=no /etc/hosts root@${ip}:/etc/
done

不解釋了