配置ssh免密登录

来源:互联网 发布:途宝网络如何设置 编辑:程序博客网 时间:2024/04/29 08:03

配置ssh免密登录:

<strong>原理解析</strong>

client A免登录到server B: 

1.在A上生成公钥私钥。 

2.将公钥拷贝给server B,要重命名成authorized_keys(从英文名就知道含义了) 

3.client A向Server B发送一个连接请求。 

4.Server B得到client A的信息后,在用户的authorized_key中查找,如果有相应的用户名和IP,则随机生成一个字符串,并用client A的公钥加密,发送给client A。 

5.client A得到Server B发来的消息后,使用私钥进行解密,然后将解密后的字符串发送给Server B。Server B进行和生成的对比,如 果一致,则允许免登录。 

总之:A要免密码登录到B,B首先要拥有A的公钥,然后B要做一次加密验证。对于非对称加密,公钥加密的密文不能公钥解开,只能 私钥解开

问题:

登录服务器的时候仍然要求输入密码。刚开始我以为添加ssh公钥的时候粘贴有问题,经过重新添加一次公钥后,仍然让输入密码,怀疑是~/.ssh/authorized_keys文件权限的问题。再通过查看日志,提示authorized_keys的权限错误。查看authorized_keys这个文件的权限为了664,/etc/ssh/sshd_config文件中设置有StrictModes yes 难怪添加了公钥仍然被要求输入密码

原因:

  This file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others。If this file, the ~/.ssh directory, or the user’s home directory are writable by other users,then the file could be modified or replaced by unauthorized users.  In this case, sshd will not allow it to be used unless the StrictModes option has been set to “no

将文件权限改为600 

<strong>chmod 600 authorized_keys</strong>

0 0
原创粉丝点击