[CentOS7][ssh][publickey][troubleshoot] 通过密钥登录ssh故障排查

时间:2024-01-14 18:11:38

通常情况下,我会使用非对称加密的方式来进行ssh的登录。

做法:

  将公钥添加到 $HOME/.ssh/authorized_keys 文件中。

但是通常,会遇见各种各样的问题,导致失败。汇总如下:

0. 常用的排除手段,一般就是,双端开debug,clinet开启的方式如下,server还从来没与与遇见过需要开的情况,一般开client端就都搞定了。

/home/tong [tong@T7] [:]
> ssh -vvvvv root@t54
OpenSSH_7.5p1, OpenSSL 1.1.0e Feb
debug1: Reading configuration data /home/tong/.ssh/config
debug1: /home/tong/.ssh/config line : Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config

1. 使用的私钥加密算法已经被openssh弃用,如 之前的 ssh-dss 就已经被新版openssh启用了,如果启用,可以如下配置:

/home/tong [tong@T7] [:]
> cat ~/.ssh/config
HOST *
HostKeyAlgorithms +ssh-dss
ForwardX11 yes

  还可以通过命令和man手册,查看都支持那些算法。详细操作略。

2.  万恶的selinux搞得鬼, 禁掉禁掉,stopstop。

  具体操作略。

  然鹅,使用selinux一定有其好处以及必要性,所以还是需要花些时间,学习一下。

  无意中看到的一个参考:

    The SELinux contexts need to be right as well if you didn't disable that. The command "restorecon -R .ssh" should fix that.

3.  authorized_keys 文件的权限不对,改成644, 记不太清了,记忆的深处好像是有这么回事的。。。

4.  同样的key,root用户好用,tong用户便不好用。debug之后,发现server返回的是:

debug3: receive packet: type 

  google之

  https://superuser.com/questions/1137438/ssh-key-authentication-fails

  当前的权限是:

[root@T54 ~]# ll -a  /home/tong/  |grep '\.ssh'
drwxrwxr-x. tong tong May : .ssh
[root@T54 ~]# ll -a /root/ |grep '\.ssh'
drwxr-xr-x root root May : .ssh
[root@T54 ~]#

  改过权限之后,好用了:

[tong@T54 ~]$ ll -a |grep '\.ssh'
drwxr-xr-x. tong tong May : .ssh
[tong@T54 ~]$ ll .ssh/
total
-rw-r--r-- tong tong May : authorized_keys
[tong@T54 ~]$

-------------------------------  update @ 2017-10-24 ----------------------------

排除以上情況依然会有key不好使的时候. 这次有被我遇到.

以往, 一直在ssh的时候使用-vvvv 来排除故障, 然而这种方法其实是愚蠢的. 因为, 这会给黑客们可称之机.

查看server的 /var/log/secure 日志文件, 将发现更多的错误信息, 如下:

> sudo tail -f /var/log/secure
Sep :: new-server sshd[]: Authentication refused: bad ownership or modes for directory /home/dave/.ssh

我这次遇到错误是:

 Authentication refused: bad ownership or modes for directory /root

因为root目录的owner被改成了game组.....