点击原文打开
(注意生成文件默认保存~/.ssh/下。两个机子的文件位置应该一致)
SSH不输入密码连接远程Linux主机
1) 在本地主机生成密钥对
ssh-keygen -t rsa
这个命令生成一个密钥对:id_rsa(私钥文件)和id_rsa.pub(公钥文件)。默认被保存在~/.ssh/目录下。
2) 将公钥添加到远程主机的 authorized_keys 文件中
将文件上传到远程主机中
scp ~/.ssh/id_rsa.pub root@192.168.1.23:/root/
SSH到登陆到远程主机,将公钥追加到 authorized_keys 文件中
cat /root/id_rsa.pub >> /root/.ssh/authorized_keys
或直接运行命令:
cat ~/.ssh/id_dsa.pub|ssh root@192.168.1.23 'sh -c "cat - >>~/.ssh/authorized_keys2"'
3) 重启 open-ssh 服务
4) 测试
ssh root@192.168.1.23
scp /home/onion/.ssh/id_rsa.pub root@192.168.1.23:/root/
呵呵,不用输入密码了:)
== 附录 ==
/etc/ssd/sshd_config 几个关键配置点
PermitRootLogin yes
PasswordAuthentication yes
# AuthorizedKeysFile .ssh/authorized_keys # 这个地方尽量使用默认,gentoo 下 ~/.ssh/authorized_keys 与 ~/.ssh/authorized_keys2 等效
You can follow any responses to this entry through the RSS 2.0 feed.You can skip to the end and leave a response. Pinging is currently not allowed.
可以简化一点操作,
scp ~/.ssh/id_rsa.pub root@192.168.1.23:/root/.ssh/authorized_keys
而且个人的公钥操作不影响服务器,服务器不必重启。
解决多人合用一个authorized_keys的情况:
cat ~/.ssh/id_rsa.pub|ssh root@192.168.1.23 ‘cat >> /root/.ssh/authorized_keys’修正 :
cat ~/.ssh/id_rsa.pub|ssh 192.168.1.23 `cat – >> ~/.ssh/authorized_keys`