ssh免密码登录快速配置方法
环境需求:
两台Linux主机 A (192.168.3.101)和 B(192.168.3.102),如何使用主机 A 免密码登录 主机B ?
配置步骤:
首页登录主机 A ,在主机A上生成自己的ssh-keygen
ssh-agent bash ssh-keygen -t rsa ssh-add ~/.ssh/id_rsa ls -la /root
生成 .ssh 文件目录
authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥
id_rsa : 生成的私钥文件
id_rsa.pub : 生成的公钥文件
know_hosts : 已知的主机公钥清单
如果希望ssh公钥生效需满足至少下面两个条件:
1) .ssh目录的权限必须是700 2) .ssh/authorized_keys文件权限必须是600
拷贝主机A的公钥到主机B(拷贝需要输入主机B的密码)
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.3.102
出现下面内容,说明添加成功
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.3.107's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.3.102'"
and check to make sure that only the key(s) you wanted were added.
尝试密码登录
ssh root@192.168.3.102
如无意外,无需密码可以直接登录主机B中。