SSH 服务部署指南
本指南涵盖 OpenSSH 服务端的安装、配置密码/公钥/多因素认证,以及连接测试方法。
适用系统:Ubuntu/Debian、CentOS/RHEL 等主流 Linux 发行版。
1. 安装 SSH 服务端
Ubuntu/Debian
# 更新软件包索引
sudo apt update# 安装 OpenSSH 服务端
sudo apt install openssh-server -y# 启动服务并设置开机自启
sudo systemctl enable --now ssh
CentOS/RHEL
# 安装 OpenSSH 服务端
sudo yum install openssh-server -y# 启动服务并设置开机自启
sudo systemctl enable --now sshd
验证安装
# 检查服务状态
sudo systemctl status ssh # Ubuntu/Debian
sudo systemctl status sshd # CentOS/RHEL# 查看 SSH 端口监听
ss -tnlp | grep ssh
2. 基础配置 SSH 服务
编辑配置文件
sudo nano /etc/ssh/sshd_config
关键配置项(按需修改)
# 修改默认端口(可选,避免暴力破解)
Port 2222# 禁用 root 登录(安全建议)
PermitRootLogin no# 允许用户列表(为空表示允许所有用户)
AllowUsers alice bob# 禁用密码认证(推荐使用公钥后关闭)
PasswordAuthentication no# 启用公钥认证
PubkeyAuthentication yes# 指定公钥存储路径
AuthorizedKeysFile .ssh/authorized_keys# 配置日志级别
LogLevel VERBOSE
重启 SSH 服务生效
# Ubuntu/Debian
sudo systemctl restart ssh# CentOS/RHEL
sudo systemctl restart sshd
3. 配置认证方式
3.1 密码认证
默认已启用,如需禁用请设置 PasswordAuthentication no
。
3.2 公钥认证
客户端生成密钥对
# 在客户端机器执行
ssh-keygen -t ed25519 -C "your_email@example.com"
# 默认保存路径:~/.ssh/id_ed25519(私钥)和 ~/.ssh/id_ed25519.pub(公钥)
上传公钥到服务端
# 方法 1:使用 ssh-copy-id 自动上传
ssh-copy-id -p 22 -i ~/.ssh/id_ed25519.pub user@server_ip# 方法 2:手动追加公钥到 ~/.ssh/authorized_keys
cat ~/.ssh/id_ed25519.pub | ssh user@server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
验证权限
服务端用户目录权限必须为:
~/.ssh
→700
~/.ssh/authorized_keys
→600
# 修复权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
3.3 多因素认证(MFA)
安装 Google Authenticator
# Ubuntu/Debian
sudo apt install libpam-google-authenticator -y# CentOS/RHEL
sudo yum install google-authenticator -y
生成 TOTP 密钥
google-authenticator
# 按提示操作,保存紧急备用码,选择基于时间的令牌
配置 PAM 模块
sudo nano /etc/pam.d/sshd
添加以下行:
auth required pam_google_authenticator.so
修改 SSH 配置
sudo nano /etc/ssh/sshd_config
确保以下配置:
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
重启 SSH 服务
sudo systemctl restart ssh # Ubuntu/Debian
sudo systemctl restart sshd # CentOS/RHEL
4. 防火墙配置
开放 SSH 端口
# Ubuntu/Debian (UFW)
sudo ufw allow 22/tcp # 若修改了端口,替换为实际端口号
sudo ufw reload# CentOS/RHEL (Firewalld)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
5. 测试 SSH 连接
基本连接测试
# 使用密码认证
ssh -p 22 user@server_ip# 使用公钥认证(指定私钥路径)
ssh -p 22 -i ~/.ssh/id_ed25519 user@server_ip# 使用 MFA 认证(先公钥后 TOTP)
ssh -p 22 user@server_ip
# 连接时会提示输入验证码
调试连接问题
# 客户端开启详细日志
ssh -vvv user@server_ip# 查看服务端日志
sudo tail -f /var/log/auth.log # Ubuntu/Debian
sudo tail -f /var/log/secure # CentOS/RHEL
6. 安全加固建议
-
禁用弱加密算法
在/etc/ssh/sshd_config
中添加:Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
-
限制用户访问
AllowUsers alice bob # 仅允许特定用户 DenyUsers root # 禁止特定用户
-
启用 Fail2ban
# 安装 Fail2ban sudo apt install fail2ban -y # Ubuntu/Debian sudo yum install fail2ban -y # CentOS/RHEL
-
定期更新系统
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian sudo yum update -y # CentOS/RHEL
7. 常见问题解决
问题 1:公钥认证失败
- 检查项:
- 服务端
~/.ssh/authorized_keys
文件权限是否为600
。 - 客户端私钥权限是否为
600
。 - SSH 配置中
PubkeyAuthentication
是否为yes
。
- 服务端
问题 2:MFA 认证不生效
- 检查项:
- PAM 配置
/etc/pam.d/sshd
是否包含pam_google_authenticator.so
。 - SSH 配置中
AuthenticationMethods
是否设置为publickey,keyboard-interactive
。
- PAM 配置
问题 3:连接超时
- 检查项:
- 防火墙是否开放 SSH 端口。
- 服务端 SSH 服务是否运行。
- 网络路由是否可达(使用
ping
或traceroute
测试)。
通过本指南,您已掌握 Linux 下 SSH 服务的完整配置与管理方法。根据实际需求灵活选择认证方式,并遵循安全最佳实践,可显著提升系统的远程访问安全性。