之前写了一个搭建hadoop集群的教程,而其中的一个步骤便是在集群中搭建ssh无密码互联,这其实是一件很费力的事,因此在此处将简述利用脚本配置ssh互联跟/etc/host的方法。
假设我们集群中共有9台机器,分别为node001...node009,然后密码都是123456,则配置步骤如下:
首先在node001上打开/etc/hosts,将整个集群的各个机器的hostname跟ip的对应关系写入;
然后执行:
yum install expect -y
然后新增文件/usr/bin/exp:
#!/usr/bin/expect
set timeout 20
set cmd [lrange $argv 1 end]
set password [lindex $argv 0]
eval spawn $cmd
expect "assword:"
send "$password\r";
interact
之后执行:
chmod +x /usr/bin/exp
打开~/generate-key.sh,编辑文件如下:
echo -e "\n"|ssh-keygen -t rsa -N ""
exp 123456 ssh -o "StrictHostKeyChecking no" node001 "hostname"
exp 123456 ssh-copy-id node001
打开~/setup_cluster.sh,编辑文件如下:
#!/bin/bash
for i in {1..9}
do
exp 123456 ssh -o "StrictHostKeyChecking no" node00${i} "hostname"
done
# install expected on all nodes
for i in {2..9}
do
exp 123456 ssh node00${i} "yum install expect -y"
done
# copy /usr/bin/exp to all nodes
for i in {2..9}
do
exp 123456 scp /usr/bin/exp node00${i}:/usr/bin/exp
done
# copy /etc/hosts to all nodes
for i in {2..9}
do
exp 123456 scp /etc/hosts node00${i}:/etc/hosts
done
# copy generate-key.sh to all nodes && generate key
for i in {2..9}
do
exp 123456 scp ~/generate-key.sh node00${i}:~/
done
# generate ssh key && send the key to node001
for i in {1..9}
do
exp 123456 ssh node00${i} "sh ~/generate-key.sh"
done
# send pub keys to all nodes
for i in {2..9}
do
exp 123456 scp ~/.ssh/authorized_keys node00${i}:~/.ssh
done
然后执行
cd ~
sh setup_cluster.sh
即可一键配置完成整个集群的ssh互联;