默认远程主机上已经存在 ~/.ssh 目录。在自己PC上执行下面脚本,输入服务器用户名和ip地址,下次登录可实现无密码登录。
#!/bin/bash
id_res_pub=~/.ssh/id_rsa.pub
if [ ! -f "$id_res_pub" ];then
ssh-keygen -t rsa
else
echo "id_rsa.pub is exist!"
fi
echo
echo "Enter remote host user name"
read rname
echo
echo "Enter remote host ip address"
read ip_address
echo
echo "You remote host :"
echo "${rname}@${ip_address}"
cat .ssh/id_rsa.pub | ssh ${rname}@${ip_address} 'cat >> .ssh/authorized_keys'