文章目录
安装 OpenSSH 服务器
根据 Linux 发行版选择命令
Ubuntu/Debian:
sudo apt update
sudo apt install openssh-server
-
CentOS/RHEL:
sudo yum install openssh-server
-
Arch Linux:
sudo pacman -S openssh
启动并启用 SSH 服务
Ubuntu/Debian/CentOS
启动服务:
sudo systemctl start ssh # Ubuntu/Debian 服务名是 `ssh`(非 `sshd`)
sudo systemctl start sshd # CentOS/RHEL 服务名是 `sshd`
- 设置开机自启:
sudo systemctl enable ssh # Ubuntu/Debian sudo systemctl enable sshd # CentOS/RHEL
验证服务状态
sudo systemctl status ssh # Ubuntu/Debian
sudo systemctl status sshd # CentOS/RHEL
- 正常状态应显示
active (running)
。
确认 SSH 服务配置
检查默认端口(22
)是否在监听:
sudo ss -ltnp | grep ssh
-
输出应有类似
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1234,fd=3))
的行。 -
若需修改配置(如端口或允许Root登录):
sudo nano /etc/ssh/sshd_config # 编辑配置文件 sudo systemctl restart ssh # 重启服务使配置生效
至此,就差不多了,可以尝试是否,以下只是针对特殊情况,进行进一步检测。不属于必须操作!
防火墙放行 SSH 端口
Ubuntu/Debian(使用 ufw
)
sudo ufw allow 22
sudo ufw enable # 启用防火墙(若未启用)
sudo ufw reload
CentOS/RHEL(使用 firewalld
)
sudo firewall-cmd --permanent --add-port=22/tcp
sudo firewall-cmd --reload
附:常见问题
Q1:Ubuntu/Debian 中服务名称是 ssh
还是 sshd
?
服务名是 ssh
,配置文件是 /etc/ssh/sshd_config
(命名易混淆,但服务名是 ssh
)。
Q2:安装后仍提示 Connection refused
?
- 可能是防火墙未正确放行端口,或 SSH 服务未启动。使用以下命令排查:
sudo systemctl restart ssh # 重启服务 sudo netstat -tulpn | grep ssh # 确认端口监听状态