温馨提示×

如何查看Ubuntu SSH状态

小樊
45
2026-07-26 22:11:38
栏目: 智能运维

在 Ubuntu 上查看 SSH 服务状态,常用以下几种方法(适用于大多数 Ubuntu 版本,如 20.04 / 22.04 / 24.04):


✅ 1. 使用 systemctl(最常用)

sudo systemctl status ssh

常见输出说明:

  • active (running) ✅ → SSH 正在运行
  • inactive (dead) ❌ → SSH 未运行
  • enabled → 开机自启
  • disabled → 未设置开机自启

✅ 2. 查看 SSH 是否正在监听端口

ss -lntp | grep 22

sudo netstat -lntp | grep 22

如果看到类似:

LISTEN 0 128 0.0.0.0:22

说明 SSH 正在监听 22 端口。


✅ 3. 查看 SSH 进程是否存在

ps aux | grep sshd

如果看到 sshd 进程,说明 SSH 服务已启动。


✅ 4. 查看防火墙是否放行 SSH

sudo ufw status

如果看到:

22/tcp ALLOW Anywhere

说明防火墙允许 SSH。

若未放行,可添加:

sudo ufw allow ssh

✅ 5. 检查 SSH 服务是否已安装

dpkg -l | grep openssh-server

如果未安装:

sudo apt update
sudo apt install openssh-server

✅ 6. 启动 / 停止 / 重启 SSH

sudo systemctl start ssh     # 启动
sudo systemctl stop ssh      # 停止
sudo systemctl restart ssh   # 重启
sudo systemctl enable ssh    # 设置开机自启

✅ 快速判断 SSH 是否可用

systemctl is-active ssh

输出:

  • active
  • inactive

如果你是在 WSL / 云服务器 / 本地虚拟机 环境中,我也可以帮你针对具体场景排查 SSH 问题。

0