×

SSH 服务配置文件详解:sshd_config 完全指南

hqy hqy 发表于2026-01-07 00:54:32 浏览13 评论0

抢沙发发表评论

SSH服务的核心配置文件 /etc/ssh/sshd_config 控制着SSH服务器的所有行为。理解这个配置文件的每个参数,对于系统安全和性能优化至关重要。本文将详细解析sshd_config的各项配置,帮助你构建安全、高效的SSH服务。

1. 配置文件基础

1.1 配置文件位置和结构

bash复制代码

# 主配置文件
/etc/ssh/sshd_config

# 配置文件目录(支持模块化配置)
/etc/ssh/sshd_config.d/

# 查看当前生效配置
sudo sshd -T

# 测试配置文件语法
sudo sshd -t

1.2 配置文件语法规则

bash复制代码

# 注释行以 # 开头
# This is a comment

# 配置格式:参数名 参数值
Port 22
PermitRootLogin no

# 多个值用空格或逗号分隔
Ciphers aes128-ctr,aes192-ctr,aes256-ctr

# 条件配置块
Match User admin
    AllowTcpForwarding yes
    X11Forwarding yes

2. 网络和连接配置

2.1 基础网络参数

bash复制代码

# 监听端口(默认22)
Port 22
# 可以指定多个端口
Port 2222

# 监听地址(默认所有接口)
ListenAddress 0.0.0.0
ListenAddress ::
# 指定特定接口
ListenAddress 192.168.1.100

# 地址族(IPv4/IPv6)
AddressFamily any          # 同时支持IPv4和IPv6
AddressFamily inet         # 仅IPv4
AddressFamily inet6        # 仅IPv6

# 协议版本(强烈建议仅使用2)
Protocol 2

2.2 连接管理参数

bash复制代码

# 最大连接数
MaxStartups 10:30:100      # 格式:start:rate:full
# 10个未认证连接后开始随机拒绝
# 拒绝率从30%逐渐增加到100%

# 最大会话数(每个连接)
MaxSessions 10

# 认证超时时间(秒)
LoginGraceTime 120

# 客户端存活检测
ClientAliveInterval 300    # 每300秒发送存活消息
ClientAliveCountMax 2      # 最多2次无响应后断开

# TCP保持连接
TCPKeepAlive yes

3. 认证配置

3.1 基础认证设置

bash复制代码

# 是否允许密码认证
PasswordAuthentication yes

# 是否允许空密码
PermitEmptyPasswords no

# 公钥认证
PubkeyAuthentication yes

# 公钥文件位置
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2

# 严格模式(检查文件权限)
StrictModes yes

# 最大认证尝试次数
MaxAuthTries 6

# 认证方法顺序
AuthenticationMethods publickey
# 或要求多重认证
AuthenticationMethods publickey,password

3.2 高级认证选项

bash复制代码

# 质询响应认证
ChallengeResponseAuthentication no

# PAM认证
UsePAM yes

# Kerberos认证
KerberosAuthentication no
KerberosOrLocalPasswd yes
KerberosTicketCleanup yes

# GSSAPI认证
GSSAPIAuthentication no
GSSAPICleanupCredentials yes

# 主机密钥认证
HostbasedAuthentication no
IgnoreUserKnownHosts no
IgnoreRhosts yes

4. 用户访问控制

4.1 用户和组限制

bash复制代码

# 允许登录的用户
AllowUsers alice bob charlie
AllowUsers admin@192.168.1.*

# 拒绝登录的用户
DenyUsers guest nobody
DenyUsers *@10.0.0.*

# 允许登录的组
AllowGroups ssh-users admin

# 拒绝登录的组
DenyGroups guests

# Root用户登录控制
PermitRootLogin no  # 完全禁止
PermitRootLogin yes # 允许(不推荐)
PermitRootLogin prohibit-password  # 仅允许密钥认证
PermitRootLogin forced-commands-only  # 仅允许强制命令

4.2 条件访问控制

bash复制代码

# 基于用户的条件配置
Match User admin
    AllowTcpForwarding yes
    X11Forwarding yes
    PermitTTY yes

# 基于组的条件配置
Match Group developers
    AllowTcpForwarding no
    X11Forwarding no
    ForceCommand /usr/bin/restricted-shell

# 基于地址的条件配置
Match Address 192.168.1.0/24
    PasswordAuthentication yes
    PermitRootLogin yes

# 基于主机的条件配置
Match Host "*.internal.company.com"
    GSSAPIAuthentication yes
    AllowTcpForwarding yes

5. 安全强化配置

5.1 加密算法配置

bash复制代码

# 支持的密钥交换算法
KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group16-sha512

# 支持的加密算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr

# 支持的MAC算法
MACs umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com

# 主机密钥算法
HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256

# 公钥接受算法
PubkeyAcceptedKeyTypes ssh-ed25519,rsa-sha2-512,rsa-sha2-256

5.2 主机密钥配置

bash复制代码

# 主机密钥文件位置
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# 主机密钥长度(RSA)
ServerKeyBits 2048

# 密钥重新生成间隔(SSH-1,已废弃)
KeyRegenerationInterval 1h

6. 功能控制配置

6.1 转发和隧道

bash复制代码

# 允许TCP转发
AllowTcpForwarding yes# 允许所有
AllowTcpForwarding no         # 禁止所有
AllowTcpForwarding local# 仅本地转发
AllowTcpForwarding remote     # 仅远程转发

# 网关端口
GatewayPorts no               # 仅绑定本地
GatewayPorts yes# 绑定所有接口
GatewayPorts clientspecified  # 客户端指定

# X11转发
X11Forwarding no
X11DisplayOffset 10
X11UseLocalhost yes

# 隧道设备
PermitTunnel no               # 禁止所有隧道
PermitTunnel point-to-point   # 允许点对点
PermitTunnel ethernet         # 允许以太网
PermitTunnel yes# 允许所有

6.2 会话和环境

bash复制代码

# 允许TTY分配
PermitTTY yes

# 用户环境变量
PermitUserEnvironment no

# 接受的环境变量
AcceptEnv LANG LC_* EDITOR

# 强制执行命令
ForceCommand /usr/bin/restricted-shell

# 压缩
Compression delayed          # 认证后压缩
Compression yes              # 立即压缩
Compression no               # 不压缩

7. 日志和监控配置

7.1 日志配置

bash复制代码

# 系统日志设施
SyslogFacility AUTH

# 日志级别
LogLevel INFO                # 标准信息
LogLevel VERBOSE             # 详细信息
LogLevel DEBUG               # 调试信息
LogLevel DEBUG1              # 更详细调试
LogLevel DEBUG2              # 最详细调试
LogLevel DEBUG3              # 极详细调试

# 记录失败登录尝试
LogLevel VERBOSE

7.2 横幅和消息

bash复制代码

# 登录前横幅
Banner /etc/ssh/banner.txt

# 登录后消息
PrintMotd yes

# 最后登录信息
PrintLastLog yes

# 版本信息
DebianBanner no              # Debian特有选项

8. 性能优化配置

8.1 缓冲区和超时

bash复制代码

# 接收缓冲区大小
ReceiveBufferSize 65536

# 发送缓冲区大小
SendBufferSize 65536

# 认证超时
LoginGraceTime 120

# 空闲超时
ClientAliveInterval 300
ClientAliveCountMax 2

8.2 DNS和反向解析

bash复制代码

# 使用DNS
UseDNS no                    # 禁用反向DNS查找(提升性能)

# IP服务质量
IPQoS af21 cs1              # 交互流量和批量流量的QoS标记

9. 实用配置示例

9.1 高安全性配置

bash复制代码

# /etc/ssh/sshd_config - 高安全性配置
Port 2222
Protocol 2
ListenAddress 0.0.0.0

# 认证配置
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PermitEmptyPasswords no
MaxAuthTries 3
LoginGraceTime 60

# 用户限制
AllowUsers admin developer
DenyUsers guest nobody

# 功能限制
AllowTcpForwarding no
X11Forwarding no
PermitTunnel no
GatewayPorts no

# 会话配置
ClientAliveInterval 300
ClientAliveCountMax 2
MaxSessions 2
MaxStartups 2

# 加密强化
KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group16-sha512
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com

# 日志配置
SyslogFacility AUTH
LogLevel VERBOSE
UseDNS no

9.2 开发环境配置

bash复制代码

# /etc/ssh/sshd_config - 开发环境配置
Port 22
Protocol 2

# 认证配置(相对宽松)
PermitRootLogin prohibit-password
PasswordAuthentication yes
PubkeyAuthentication yes
MaxAuthTries 6

# 功能支持
AllowTcpForwarding yes
X11Forwarding yes
GatewayPorts clientspecified

# 会话配置
ClientAliveInterval 600
ClientAliveCountMax 3
MaxSessions 10

# 日志配置
LogLevel INFO
UseDNS no

# 开发便利性
PrintMotd yes
PrintLastLog yes

9.3 跳板机配置

bash复制代码

# /etc/ssh/sshd_config - 跳板机配置
Port 22
Protocol 2

# 严格认证
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3

# 转发配置
AllowTcpForwarding yes
GatewayPorts no
X11Forwarding no

# 会话限制
MaxSessions 5
ClientAliveInterval 300
ClientAliveCountMax 2

# 条件配置
Match Group jump-users
    AllowTcpForwarding yes
    PermitTTY no
    ForceCommand /usr/local/bin/jump-menu

Match Group admin
    AllowTcpForwarding yes
    PermitTTY yes
    X11Forwarding yes

# 日志和监控
LogLevel VERBOSE
SyslogFacility AUTH

10. 配置验证和调试

10.1 配置测试命令

bash复制代码

# 测试配置文件语法
sudo sshd -t

# 查看当前生效配置
sudo sshd -T

# 查看特定用户的配置
sudo sshd -T -C user=alice,host=192.168.1.100

# 调试模式启动(前台运行)
sudo sshd -D -d

# 详细调试信息
sudo sshd -D -ddd

10.2 常见配置错误

bash复制代码

# 错误1:权限问题
# 配置文件权限必须是644或更严格
sudochmod 644 /etc/ssh/sshd_config

# 错误2:语法错误
# 使用sshd -t检查语法

# 错误3:端口冲突
# 检查端口是否被占用
sudo netstat -tlnp | grep :22

# 错误4:防火墙阻止
# 检查防火墙规则
sudo ufw status
sudo iptables -L

# 错误5:SELinux策略
# 检查SELinux上下文
sudo setsebool -P ssh_sysadm_login on

11. 配置管理最佳实践

11.1 配置文件管理

bash复制代码

# 备份原始配置
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

# 使用版本控制
cd /etc/ssh
sudo git init
sudo git add sshd_config
sudo git commit -m "Initial SSH configuration"

# 模块化配置(OpenSSH 7.5+)
# 在 /etc/ssh/sshd_config.d/ 目录下创建配置片段
sudo echo "MaxAuthTries 3" > /etc/ssh/sshd_config.d/security.conf

11.2 配置变更流程

bash复制代码

#!/bin/bash
# ssh_config_update.sh - 安全的SSH配置更新脚本

set -e

CONFIG_FILE="/etc/ssh/sshd_config"
BACKUP_FILE="/etc/ssh/sshd_config.backup.$(date +%Y%m%d_%H%M%S)"

# 1. 备份当前配置
echo "备份当前配置到 $BACKUP_FILE"
sudo cp "$CONFIG_FILE"$BACKUP_FILE"

# 2. 应用新配置
echo "应用新配置..."
sudo cp sshd_config.new "$CONFIG_FILE"

# 3. 测试配置语法
echo "测试配置语法..."
if sudo sshd -t; then
    echo "配置语法正确"
else
    echo "配置语法错误,恢复备份"
    sudo cp "$BACKUP_FILE"$CONFIG_FILE"
    exit 1
fi

# 4. 重新加载配置
echo "重新加载SSH服务..."
sudo systemctl reload sshd

# 5. 验证服务状态
if sudo systemctl is-active --quiet sshd; then
    echo "SSH服务运行正常"
    echo "配置更新成功"
else
    echo "SSH服务异常,恢复备份"
    sudo cp "$BACKUP_FILE"$CONFIG_FILE"
    sudo systemctl restart sshd
    exit 1
fi

12. 安全检查清单

12.1 基础安全检查

  • 禁用root直接登录
  • 使用非标准端口
  • 禁用密码认证(仅使用密钥)
  • 限制最大认证尝试次数
  • 配置用户访问控制
  • 禁用不必要的功能(X11转发等)
  • 启用详细日志记录
  • 配置防火墙规则

12.2 高级安全检查

  • 使用强加密算法
  • 配置客户端存活检测
  • 实施多重认证
  • 设置会话限制
  • 配置条件访问控制
  • 启用入侵检测
  • 定期轮换主机密钥
  • 监控异常访问

总结

SSH配置文件是系统安全的重要组成部分,正确理解和配置这些参数对于:

  1. 提升安全性 - 通过限制访问、强化认证、使用强加密

  2. 优化性能 - 调整缓冲区、超时参数、禁用不必要功能

  3. 便于管理 - 使用条件配置、模块化管理、自动化部署

  4. 满足合规 - 记录详细日志、实施访问控制、定期审计

SSH配置的核心原则是"最小权限"和"纵深防御"。在保证功能需求的前提下,尽可能限制不必要的功能和权限,并通过多层安全措施构建坚固的防护体系。


打赏

本文链接:https://kinber.cn/post/6122.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客