#!/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 "主机公钥复制完成"
#!/bin/sh
# 读取主机列表然后把/etc/hosts复制到所有主机上
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