Linux SSH 连接被拒绝:诊断与修复

SSH 连不上 Linux 服务器?按顺序测试端口、检查 sshd、放行防火墙、校验配置。

Linuxnc 网络调试端口netcat 网络调试systemd故障排查服务高风险
先用 netcat 对服务器的 22 端口做一次连通性测试。

快速开始

"Connection refused" 说明客户端能到达机器,但 SSH 端口上没有服务应答。按端口测试、守护进程状态、防火墙、配置校验的顺序排查,大多数情况在这四步之内就能解决。

症状

?执行 ssh user@server 后立即报错 "ssh: connect to host ... port 22: Connection refused"(或超时,指向同样的原因)。

先测试 22 端口是否可达

"Connection refused" 通常意味着 SSH 端口上没有服务在监听,或者被防火墙拦截。用 netcat 对服务器的 22 端口做一次连接测试:连接成功说明 sshd 正常运行、端口已开放,问题在别处;如果被拒绝或超时,则指向 sshd 服务或防火墙。把目标地址换成你的服务器主机名或 IP。

回答「是不是防火墙挡住了我」最快的办法——netcat 扫描模式只建立一次 TCP 连接并打印结果。

命令
$ nc -zv example.com 443

确认 sshd 是否真的在运行

如果端口测试失败,最常见的原因就是 SSH 守护进程没有运行。在服务器上查看 sshd 服务的状态:输出会显示它是否处于活动状态、上次重启时间以及最近的日志——配置错误或崩溃通常一眼就能看出来。如果显示 inactive 或 failed,问题就找到了。

用一条命令查看 systemd 服务的加载/开机自启状态、运行状态、主进程 PID 以及最近的日志。

命令
$ systemctl status nginx

在防火墙中放行 22 端口

如果 sshd 正常运行但端口测试仍然失败,很可能是主机防火墙(如 UFW)拦截了入站 SSH。用 UFW 放行 TCP 22 端口,然后从客户端重新测试——这次应该能连上。以后如果需要撤销,这条规则也可以显式删除。放行端口会改变防火墙的暴露面,只开放确实需要对外提供服务的端口。

Linux用 ufw 放行防火墙端口高风险步骤 3

添加一条 UFW 规则,允许某个 TCP 或 UDP 端口的入站流量,例如 22 端口的 SSH。

命令
$ sudo ufw allow 22/tcp
▾ 撤销
撤销
$ sudo ufw delete allow 22/tcp

重启前先校验 sshd 配置

/etc/ssh/sshd_config 里一个笔误就可能导致 sshd 启动失败——这是 "connection refused" 的常见隐性原因。在重启之前先用 sshd -t 做干跑校验:没有任何输出说明配置合法;有错误则会带行号打印出来,方便定位修改。校验通过后再重启守护进程,SSH 即可恢复,端口测试也应能连通。

Linux用 sshd -t 在重启前校验 SSH 配置中风险步骤 4可选

对 sshd 配置做干跑检查,防止一个拼写错误把你锁在服务器外面——每次 systemctl reload sshd 前的标配动作。

命令
$ sudo sshd -t
sudo sshd -T | grep -i permitrootlogin
!修改防火墙和重启 sshd 都会影响远程访问:重启前务必校验 SSH 配置,否则一个笔误可能让你完全无法登录服务器。

如何确认已解决

重新对 22 端口执行 netcat 测试,应显示连接成功;然后用 ssh 连接,应出现登录提示或直接进入 shell。

一键全部撤销

上面每一条命令都可以撤销。按此顺序执行即可全部还原。

一键全部撤销
$ sudo ufw delete allow 22/tcp

执行前请先确认——部分步骤之间可能并非相互独立。

来源

  • nc(1) — arbitrary TCP and UDP connections and listens (OpenBSD netcat)
    Ubuntu Manpages · technical · 2026-09-17
    依据: Documents -z (zero-I/O scan mode) and -v (verbose) for testing if a port is open without sending data
  • OpenSSL — ArchWiki
    ArchWiki · community · 2026-09-17
    依据: ArchWiki network troubleshooting pages commonly recommend nc -zv for quick port reachability checks
  • Ubuntu Manpage: systemctl - Control the systemd system and service manager
    manpages.ubuntu.com · technical · 2026-09-17
    依据: "status [PATTERN...|URL...] Show terse runtime status information about one or more units ... This function is intended to generate human-readable output ... along with the most recent log data."
  • systemctl(1) — Arch Linux manual
    man.archlinux.org · technical · 2026-09-17
    依据: "status [PATTERN...|URL...] — Show terse runtime status information about one or more units, followed by most recent log data from the journal."
  • UncomplicatedFirewall - Ubuntu Wiki
    Ubuntu Wiki · vendor-doc · 2026-09-17
    依据: The Ubuntu UFW wiki describes enabling the firewall with ufw enable and adding allow rules by port and protocol.
  • ufw(8) — program for managing a netfilter firewall
    Ubuntu manpages · vendor-doc · 2026-09-17
    依据: The ufw manual documents 'ufw allow' rules with PORT/PROTO syntax such as 22/tcp and matching 'ufw delete allow' to remove them.
  • sshd(8) — Linux manual page
    man7.org · technical · 2026-09-17
    依据: sshd man page: '-t Test mode. Only check the validity of the configuration file and sanity of the keys' and '-T extended test mode printing effective config'.

最后验证: 2026-09-21

相关指南