最近要给集群设置ssh无密码登陆,如果需要手动设置这个无密码登陆,所以在网上找了几个脚本,亲测下面这个好使,并且设置比较简单。
需要用root账户执行,我也是要给root账户设置无密码登陆。
首先我们要创建密码,执行下面这句命令:
ssh-keygen -t rsa
脚本如下:我们仅仅需要修改的就是node=()里面的主机名就可以了,当前前提是你必须在/etc/hosts 配置了。
#!/bin/bash
node=(hadoop.Master hadoop.SlaveT1 hadoop.SlaveT2) # hostname for each node
username=root # username to be interconnected
homename=$username # home dir, i.e. home/zhangyang
if [ "$username" = "root" ];
then
homename=root
else
homename=home/$username
fi
for((i=0; i<${#node[*]}; i++))
do
ssh $username@${node[i]} 'ssh-keygen -t rsa; chmod 755 ~/.ssh'
done
#cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
echo "batch authorized_keys created..."
echo "start scp..."
#scp node003:/$homename/.ssh/authorized_keys /$homename/.ssh/node003.key
for((i=0; i<${#node[*]}; i++))
do
scp ${node[i]}:/$homename/.ssh/id_rsa.pub /$homename/.ssh/${node[i]}.key
echo "scp from ${node[i]} finished..."
done
echo "append key to authorized_keys..."
for((i=0; i<${#node[*]}; i++))
do
cat /$homename/.ssh/${node[i]}.key >> /$homename/.ssh/authorized_keys
echo "append ${node[i]}.key finished..."
done
echo "append all key finished..."
loop=${#node[*]}
let subloop=loop-1
echo "starting scp complete authorized_keys to ${node[1]}~${node[subloop]}"
for((i=1; i<${#node[*]}; i++))
do
scp /$homename/.ssh/authorized_keys ${node[i]}:/$homename/.ssh/authorized_keys
echo "scp to ${node[i]} finished..."
done
echo "scp all nodes finished..."
# delete intermediate files
rm -rf /$homename/.ssh/*.key
echo "all configuration finished..."
然后给脚本授予执行的权限 chmod +x batchSSH.sh
然后执行./batchSSh.sh
当然这个脚本也有缺陷,它需要自己手动输入密码,如果节点多的话,还是很痛苦的,不过还好啦,比起自己手动搞的话,好多了,谁叫我不懂shell呢,哈哈!
时间: 2024-10-31 13:54:24