> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# 排查SSH无法连接错误
- URL: https://yinguobing.com/blog/fix-ssh-connection-error/
- Published: 2022-03-15T05:22:37.000Z
- Updated: 2022-03-15T05:22:37.000Z
- Description: 日常排错一例
- Author: 尹国冰
- Tags: 新手入门, Linux

现有一台远程服务器，安装了Ubuntu Server系统，IP地址为 `A.B.C.D` 。突然有一天无法通过ssh远程连接，具体表现为持续等待最终超时。但是通过另一台不同IP地址的客户端却可以正常访问。

排查这个错误分三步。

### 检查 hosts.deny 文件

检查 `/etc/hosts.deny` 文件，看本机IP地址是否在其中。如果在，将其删除。

### 检查 denyhosts 服务

如果上述方法不起作用，则考虑 denyhosts 服务。

首先检查 denyhosts 服务是否在运行中。

```bash
sudo systemctl status denyhosts
```

如果服务已经在运行中，则挨个检查其配置文件：

```bash
sudo ls /var/lib/denyhosts/
hosts		  hosts-root   offset		  users-hosts	 users-valid
hosts-restricted  hosts-valid  suspicious-logins  users-invalid

```

如果本机IP在配置文件中，将其删除。

删除后重启服务。

```bash
sudo systemctl restart denyhosts
```

### 检查 iptables

如果仍然无法连接，考虑 iptables。

首先输出 iptables 规则：

```bash
sudo iptables -L
```

仔细检查其输出中是否有与本机IP相关的规则，如果有，其 target 是否为 DROP。

如果存在这样的规则，将其删除。

理论上，以上步骤完成后即可以通过ssh连接了。