ssh-keys.png
好久之前在公司的 PC 机上设置了 alias 登录服务器,感觉挺方便的.例如:
alias 184='ssh -lroot xxx.xxx.xxx.184'
输入 184 就可以登录到 IP 以184结尾的服务器上了.可是后来有些服务器修改了密码,不再使用默认密码了,随着这种情况越来越多,想记住密码也越来越难.
想不用自己记住密码,选择有两个:一种是使用 expect 做登录时自动填写密码;另一种是使用 ssh 的公钥,免密码登录.看起来 ssh 至少不需要写代码,我又懒得要死,所以就选了免密码登录.
这个方法真的是非常简单先在本机生成ssh公钥和密钥,输入 ssh-keygen 然后一路回车,搞定.
# ssh-keygen
接下来将 ~/.ssh/id_rsa.pub 中的内容复制进 ~/.ssh/authorized_keys 里面就可以了.
# 184 vod_dev:~ #
按照这个方法我很快的搞定了大部分的服务器免密码登录,就剩下一台服务器尝试了好几遍都不行.
我首先想到的是看看 ssh 登录命令的输出中能不能看出什么问题.
# ssh -lroot -vv 10.18.207.25 debug2: we sent a gssapi-with-mic packet, wait for reply debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug2: we did not send a packet, disable method debug1: Next authentication method: publickey debug1: Offering RSA public key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_rsa debug2: we sent a publickey packet, wait for reply debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug1: Offering DSA public key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa debug2: we sent a publickey packet, wait for reply debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug1: Trying private key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_ecdsa debug1: Trying private key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_ed25519 debug2: we did not send a packet, disable method debug1: Next authentication method: password root@10.18.207.253's password:
最先吸引我注意力就是ssh回去查找id_dsa这个文件作为私钥,而本机只有id_rsa,我猜测是因为服务器是因为设置不同,需要使用 dsa 作为加密算法.那么就想办法让我使用的PC机产生 id_dsa 文件.
# ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa. Your public key has been saved in /home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa.pub. The key fingerprint is: d3:26:96:59:12:f6:f2:e6:f8:73:bc:e4:b1:4b:5a:31 HISENSE\jiangxun1@jiangxun1c1 The key's randomart image is: +--[ DSA 1024]----+ | o | | . o | | o o | | O | | S = E | | . B o | | . ..= | | ..*oo | | oo=o | +-----------------+
产生 RSA 的密钥后,问题依旧.这个时候我就有些没有头绪了,猜想也许需要重启启动一下服务器上的 sshd 才能起作用吧.
[root@jhx /]# systemctl restart sshd [root@jhx /]#
重启后依旧不能免密码登录,而且 sshd 重启后在终端里一点输出都没有,都不知 ssh 是否已经重新启动,更重要的是服务器的 sshd 在收到免密码登录请求时,是否报错?
我对 Linux 的日志输出的设置不了解,又一时没有想到什么的关键词去搜索,就直接本机 grep 搜索了,确实很 low,不过好在很快就找到了 ssh 的日志放在了 /var/log/secure 文件里.ssh 重启后可以看到下面的几行输出,证明 sshd 是正常重启了.
May 28 12:54:30 jhx sshd[27117]: Received signal 15; terminating. May 28 12:54:30 jhx sshd[28262]: Server listening on 0.0.0.0 port 22. May 28 12:54:30 jhx sshd[28262]: Server listening on :: port 22.
并且找到了之前免密登录时的报错信息:
May 28 11:41:34 jhx sshd[27313]: Authentication refused: bad ownership or modes for directory /root May 28 11:41:34 jhx sshd[27313]: Authentication refused: bad ownership or modes for directory /root
报错信息非常明显,就是 /root 目录的权限和所有权设置不正确.接下来我就到google上去搜这条报错信息了,找到了这篇文章,文章中的主要解决方案就是修改权限,但是改过之后问题照旧. SSH Authentication Refused: Bad Ownership or Modes for Directory
现在回过头来想,真的应该好好在本机上分析一下原因,而不是不停的在搜索引擎上不停的找现成的解决方案. 当然最终我还是因为对 google 的依赖,一条道走到黑了.在网上瞎晃了半个小时以后,我终于在服务器上发现了 /root 目录的问题所在./root 目录的所有者居然不是 root.
# ll / drwxr-xr-x. 15 1054761 1049089 4096 May 28 11:36 root
修改之后,终于可以成功的免密码登录了.
chown root:root /root
以前我就遇到过ssh免密总是无法成功的情况,一直没有找到解决办法.这次终于算是解决了.不过复盘自己分析问题的过程,还是可以发现自己过于依赖搜索引擎和stackoverflow,总是想找到完全符合自己问题的答案.更严重的问题是我自己还没有意识到这其实是不正确思考方式.缺乏自己分析和解决问题的意愿,以后遇到问题一定要先主动分析,看看自己能不能凭借自己的力量解决,实在是没有思路再去搜索引擎上寻求帮助,把搜索引擎当做场外的援助,而千万不能让搜索引擎替代自己思考.
最后搜索了一下 CentOS7 的日志输出设置,这篇文章讲的挺清楚的: SSHD is not logging in /var/log/secure
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句