Linux安装配置SSH服务核心三步:装服务、启服务、调配置;需按发行版执行apt/dnf/yum安装openssh-server,systemctl启动并启用sshd/ssh,编辑/etc/ssh/sshd_config调整PasswordAuthentication等参数,重启服务并放行防火墙22端口。

Linux 中安装配置 SSH 远程连接服务,核心就三步:装服务、启服务、调配置。默认多数发行版已预装客户端(ssh),但服务端(sshd)常需手动安装并启用。
安装 OpenSSH 服务端
根据系统类型执行对应命令:
-
Ubuntu/Debian 系统:
sudo apt update && sudo apt install -y openssh-server -
CentOS/RHEL/Rocky 8+:
sudo dnf install -y openssh-server(或yum install -y openssh-server在旧版)
安装完成后,配置文件 /etc/ssh/sshd_config 即自动生成,无需手动创建。
启动并设置开机自启
服务安装后不会自动运行,需主动启用:
- 立即启动:
sudo systemctl start ssh(Ubuntu/Debian)
或sudo systemctl start sshd(CentOS/RHEL) - 设为开机自启:
sudo systemctl enable ssh或sudo systemctl enable sshd - 检查状态是否正常:
sudo systemctl status ssh(看 Active: active (running))
基础安全配置(必做)
直接使用默认配置存在风险,建议至少完成以下调整:
- 编辑配置文件:
sudo nano /etc/ssh/sshd_config - 放开密码登录(如需):
找到PasswordAuthentication,改为yes(若注释掉,先删掉前面的#) - 允许 root 登录(谨慎):
将PermitRootLogin改为yes(生产环境建议用普通用户+sudo) - 保存后重启服务生效:
sudo systemctl restart ssh(或sshd)
放行防火墙端口
否则外部连接会被拦截:
-
Ubuntu(ufw):
sudo ufw allow 22/tcp && sudo ufw enable -
CentOS/RHEL(firewalld):
sudo firewall-cmd --add-port=22/tcp --permanent && sudo firewall-cmd --reload
确认端口已监听:sudo ss -tlnp | grep :22 或 sudo netstat -tlnp | grep :22


















