安装集群的时候要进行免密登陆,不想每台机器都手动人工操作,从网上找了一段shell脚本,如下:#!/bin/bash
# for self configure
wd=.tmp_ssh
#mkdir -p $wd
echo "mkdir success"
hosts=(`sed -n '3,$p' /etc/hosts | awk '{print $1}'`)
for i in "${hosts[@]}"
do
script=$(cat << \EOF
ssh-keygen -t rsa <<eof
eof
EOF
)
exe='bash << EOF
'"$script"'
EOF'
echo =======$i=======
ssh -o StrictHostKeyChecking=no $i "$exe"
# eval `ssh-agent -s`
# ssh-add -L
ssh-copy-id -i ~/.ssh/id_rsa.pub $i
scp $i:~/.ssh/id_rsa.pub $wd/id_rsa.pub_$i
done
cat $wd/id_rsa.pub_* > $wd/authorized_keys
chmod 600 $wd/authorized_keys
for i in "${hosts[@]}"
do
scp $wd/authorized_keys $i:~/.ssh/
scp ~/.ssh/known_hosts $i:~/.ssh/
done
rm -rf $wd
echo "ssh finished!"
以root运行之后结果如下:报/usr/bin/ssh-copy-id: ERROR: No identities found错误,不知道什么原因,求高手解答
mkdir success
=======192.168.2.185======= root@192.168.2.185's password:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /usr/bin/ssh-copy-id: ERROR: No identities found root@192.168.2.185's password:
scp: /root/.ssh/id_rsa.pub: No such file or directory
=======192.168.2.186======= root@192.168.2.186's password:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /usr/bin/ssh-copy-id: ERROR: No identities found root@192.168.2.186's password:
scp: /root/.ssh/id_rsa.pub: No such file or directory
=======192.168.2.187======= root@192.168.2.187's password:
Unable to get valid context for root
/usr/bin/ssh-copy-id: ERROR: No identities found root@192.168.2.187's password:
Unable to get valid context for root
./ssh.sh: line 24: .tmp_ssh/authorized_keys: No such file or directory
chmod: cannot access `.tmp_ssh/authorized_keys': No such file or directory root@192.168.2.185's password:
.tmp_ssh/authorized_keys: No such file or directory root@192.168.2.185's password:
known_hosts 100% 1201 1.2KB/s 00:00 root@192.168.2.186's password:
.tmp_ssh/authorized_keys: No such file or directory root@192.168.2.186's password:
known_hosts 100% 1201 1.2KB/s 00:00 root@192.168.2.187's password:
Unable to get valid context for root root@192.168.2.187's password:
Unable to get valid context for root
ssh finished!