有一个master机器经常向slave机器发送文件和执行命令,免密码配置可以省去输入密码的麻烦。
master配置
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
ssh localhost # 测试是否可以免密码访问本机
#ssh客户端免yes/no输入
vi /etc/ssh/ssh_config
> StrictHostKeyChecking no
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
ssh localhost # 测试是否可以免密码访问本机
#ssh客户端免yes/no输入
vi /etc/ssh/ssh_config
> StrictHostKeyChecking no
slave配置
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
master分发公钥
scp ~/.ssh/id_rsa.pub [email protected]:~/.ssh/id_rsa.pub
scp ~/.ssh/id_rsa.pub [email protected]:~/.ssh/id_rsa.pub
slave授权
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
=============
错误诊断:
Q:配置都OK的,但master访问slave还是需要密码
扫描二维码关注公众号,回复:
1334112 查看本文章
A:这个通常是目录权限的问题,如/root/.ssh权限要求是700,/root权限也要是700,诊断方法:slave机器执行:tail /var/log/secure -f,然后mster执行连接命令,会有如下提示:
Sep 3 16:34:30 localhost sshd[17960]: Authentication refused: bad ownership or modes for directory /root/.ssh
Sep 3 16:34:30 localhost sshd[17960]: Authentication refused: bad ownership or modes for directory /root/.ssh
Sep 3 16:34:48 localhost sshd[17961]: Connection closed by 172.16.11.189
Sep 3 16:34:49 localhost sshd[17963]: Authentication refused: bad ownership or modes for directory /root
Sep 3 16:34:49 localhost sshd[17963]: Authentication refused: bad ownership or modes for directory /root
Sep 3 16:34:30 localhost sshd[17960]: Authentication refused: bad ownership or modes for directory /root/.ssh
Sep 3 16:34:48 localhost sshd[17961]: Connection closed by 172.16.11.189
Sep 3 16:34:49 localhost sshd[17963]: Authentication refused: bad ownership or modes for directory /root
Sep 3 16:34:49 localhost sshd[17963]: Authentication refused: bad ownership or modes for directory /root
可见: /root/.ssh 和/root两个目录权限有问题,改为700就OK了。