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

一、配置文件基础
1.1 配置文件位置和结构

1 # 主配置文件
2 /etc/ssh/sshd_config
3
4 # 配置文件目录(支持模块化配置)
5 /etc/ssh/sshd_config.d/
6
7 # 查看当前生效配置
8 sudo sshd -T
9
10 # 测试配置文件语法
11 sudo sshd -t

1.2 配置文件语法规则

1 # 注释行以 # 开头
2 # This is a comment
3
4 # 配置格式:参数名 参数值
5 Port 22
6 PermitRootLogin no
7
8 # 多个值用空格或逗号分隔
9 Ciphers aes128-ctr,aes192-ctr,aes256-ctr
10
11 # 条件配置块
12 Match User admin
13 AllowTcpForwarding yes
14 X11Forwarding yes

二、网络和连接配置
2.1 基础网络参数

1 # 监听端口(默认22)
2 Port 22
3 # 可以指定多个端口
4 Port 2222
5
6 # 监听地址(默认所有接口)
7 ListenAddress 0.0.0.0
8 ListenAddress ::
9 # 指定特定接口
10 ListenAddress 192.168.1.100
11
12 # 地址族(IPv4/IPv6)
13 AddressFamily any # 同时支持IPv4和IPv6
14 AddressFamily inet # 仅IPv4
15 AddressFamily inet6 # 仅IPv6
16
17 # 协议版本(强烈建议仅使用2)
18 Protocol 2

2.2 连接管理参数

1 # 最大连接数
2 MaxStartups 10:30:100 # 格式:start:rate:full
3 # 10个未认证连接后开始随机拒绝
4 # 拒绝率从30%逐渐增加到100%
5
6 # 最大会话数(每个连接)
7 MaxSessions 10
8
9 # 认证超时时间(秒)
10 LoginGraceTime 120
11
12 # 客户端存活检测
13 ClientAliveInterval 300 # 每300秒发送存活消息
14 ClientAliveCountMax 2 # 最多2次无响应后断开
15
16 # TCP保持连接
17 TCPKeepAlive yes

三、认证配置
3.1 基础认证设置

1 # 是否允许密码认证
2 PasswordAuthentication yes
3
4 # 是否允许空密码
5 PermitEmptyPasswords no
6
7 # 公钥认证
8 PubkeyAuthentication yes
9
10 # 公钥文件位置
11 AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
12
13 # 严格模式(检查文件权限)
14 StrictModes yes
15
16 # 最大认证尝试次数
17 MaxAuthTries 6
18
19 # 认证方法顺序
20 AuthenticationMethods publickey
21 # 或要求多重认证
22 AuthenticationMethods publickey,password

3.2 高级认证选项

1 # 质询响应认证
2 ChallengeResponseAuthentication no
3
4 # PAM认证
5 UsePAM yes
6
7 # Kerberos认证
8 KerberosAuthentication no
9 KerberosOrLocalPasswd yes
10 KerberosTicketCleanup yes
11
12 # GSSAPI认证
13 GSSAPIAuthentication no
14 GSSAPICleanupCredentials yes
15
16 # 主机密钥认证
17 HostbasedAuthentication no
18 IgnoreUserKnownHosts no
19 IgnoreRhosts yes

四、用户访问控制
4.1 用户和组限制

1 # 允许登录的用户
2 AllowUsers alice bob charlie
3 AllowUsers admin@192.168.1.*
4
5 # 拒绝登录的用户
6 DenyUsers guest nobody
7 DenyUsers @10.0.0.
8
9 # 允许登录的组
10 AllowGroups ssh-users admin
11
12 # 拒绝登录的组
13 DenyGroups guests
14
15 # Root用户登录控制
16 PermitRootLogin no # 完全禁止
17 PermitRootLogin yes # 允许(不推荐)
18 PermitRootLogin prohibit-password # 仅允许密钥认证
19 PermitRootLogin forced-commands-only # 仅允许强制命令

4.2 条件访问控制

1 # 基于用户的条件配置
2 Match User admin
3 AllowTcpForwarding yes
4 X11Forwarding yes
5 PermitTTY yes
6
7 # 基于组的条件配置
8 Match Group developers
9 AllowTcpForwarding no
10 X11Forwarding no
11 ForceCommand /usr/bin/restricted-shell
12
13 # 基于地址的条件配置
14 Match Address 192.168.1.0/24
15 PasswordAuthentication yes
16 PermitRootLogin yes
17
18 # 基于主机的条件配置
19 Match Host “*.internal.company.com”
20 GSSAPIAuthentication yes
21 AllowTcpForwarding yes

五、安全强化配置
5.1 加密算法配置

1 # 支持的密钥交换算法
2 KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group16-sha512
3
4 # 支持的加密算法
5 Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
6
7 # 支持的MAC算法
8 MACs umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com
9
10 # 主机密钥算法
11 HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256
12
13 # 公钥接受算法
14 PubkeyAcceptedKeyTypes ssh-ed25519,rsa-sha2-512,rsa-sha2-256

5.2 主机密钥配置

1 # 主机密钥文件位置
2 HostKey /etc/ssh/ssh_host_rsa_key
3 HostKey /etc/ssh/ssh_host_ecdsa_key
4 HostKey /etc/ssh/ssh_host_ed25519_key
5
6 # 主机密钥长度(RSA)
7 ServerKeyBits 2048
8
9 # 密钥重新生成间隔(SSH-1,已废弃)
10 KeyRegenerationInterval 1h

六、功能控制配置
6.1 转发和隧道

1 # 允许TCP转发
2 AllowTcpForwarding yes# 允许所有
3 AllowTcpForwarding no # 禁止所有
4 AllowTcpForwarding local# 仅本地转发
5 AllowTcpForwarding remote # 仅远程转发
6
7 # 网关端口
8 GatewayPorts no # 仅绑定本地
9 GatewayPorts yes# 绑定所有接口
10 GatewayPorts clientspecified # 客户端指定
11
12 # X11转发
13 X11Forwarding no
14 X11DisplayOffset 10
15 X11UseLocalhost yes
16
17 # 隧道设备
18 PermitTunnel no # 禁止所有隧道
19 PermitTunnel point-to-point # 允许点对点
20 PermitTunnel ethernet # 允许以太网
21 PermitTunnel yes# 允许所有

6.2 会话和环境

1 # 允许TTY分配
2 PermitTTY yes
3
4 # 用户环境变量
5 PermitUserEnvironment no
6
7 # 接受的环境变量
8 AcceptEnv LANG LC_* EDITOR
9
10 # 强制执行命令
11 ForceCommand /usr/bin/restricted-shell
12
13 # 压缩
14 Compression delayed # 认证后压缩
15 Compression yes # 立即压缩
16 Compression no # 不压缩

七、日志和监控配置
7.1 日志配置

1 # 系统日志设施
2 SyslogFacility AUTH
3
4 # 日志级别
5 LogLevel INFO # 标准信息
6 LogLevel VERBOSE # 详细信息
7 LogLevel DEBUG # 调试信息
8 LogLevel DEBUG1 # 更详细调试
9 LogLevel DEBUG2 # 最详细调试
10 LogLevel DEBUG3 # 极详细调试
11
12 # 记录失败登录尝试
13 LogLevel VERBOSE
AI写代码

7.2 横幅和消息

1 # 登录前横幅
2 Banner /etc/ssh/banner.txt
3
4 # 登录后消息
5 PrintMotd yes
6
7 # 最后登录信息
8 PrintLastLog yes
9
10 # 版本信息
11 DebianBanner no # Debian特有选项
AI写代码

八、性能优化配置
8.1 缓冲区和超时

1 # 接收缓冲区大小
2 ReceiveBufferSize 65536
3
4 # 发送缓冲区大小
5 SendBufferSize 65536
6
7 # 认证超时
8 LoginGraceTime 120
9
10 # 空闲超时
11 ClientAliveInterval 300
12 ClientAliveCountMax 2
AI写代码

8.2 DNS和反向解析

1 # 使用DNS
2 UseDNS no # 禁用反向DNS查找(提升性能)
3
4 # IP服务质量
5 IPQoS af21 cs1 # 交互流量和批量流量的QoS标记
AI写代码

.九、实用配置示例
9.1 高安全性配置

1 # /etc/ssh/sshd_config - 高安全性配置
2 Port 2222
3 Protocol 2
4 ListenAddress 0.0.0.0
5
6 # 认证配置
7 PermitRootLogin no
8 PasswordAuthentication no
9 PubkeyAuthentication yes
10 AuthorizedKeysFile .ssh/authorized_keys
11 PermitEmptyPasswords no
12 MaxAuthTries 3
13 LoginGraceTime 60
14
15 # 用户限制
16 AllowUsers admin developer
17 DenyUsers guest nobody
18
19 # 功能限制
20 AllowTcpForwarding no
21 X11Forwarding no
22 PermitTunnel no
23 GatewayPorts no
24
25 # 会话配置
26 ClientAliveInterval 300
27 ClientAliveCountMax 2
28 MaxSessions 2
29 MaxStartups 2
30
31 # 加密强化
32 KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group16-sha512
33 Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
34 MACs umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com
35
36 # 日志配置
37 SyslogFacility AUTH
38 LogLevel VERBOSE
39 UseDNS no
AI写代码

9.2 开发环境配置

1 # /etc/ssh/sshd_config - 开发环境配置
2 Port 22
3 Protocol 2
4
5 # 认证配置(相对宽松)
6 PermitRootLogin prohibit-password
7 PasswordAuthentication yes
8 PubkeyAuthentication yes
9 MaxAuthTries 6
10
11 # 功能支持
12 AllowTcpForwarding yes
13 X11Forwarding yes
14 GatewayPorts clientspecified
15
16 # 会话配置
17 ClientAliveInterval 600
18 ClientAliveCountMax 3
19 MaxSessions 10
20
21 # 日志配置
22 LogLevel INFO
23 UseDNS no
24
25 # 开发便利性
26 PrintMotd yes
27 PrintLastLog yes
AI写代码

9.3 跳板机配置

1 # /etc/ssh/sshd_config - 跳板机配置
2 Port 22
3 Protocol 2
4
5 # 严格认证
6 PermitRootLogin no
7 PasswordAuthentication no
8 PubkeyAuthentication yes
9 MaxAuthTries 3
10
11 # 转发配置
12 AllowTcpForwarding yes
13 GatewayPorts no
14 X11Forwarding no
15
16 # 会话限制
17 MaxSessions 5
18 ClientAliveInterval 300
19 ClientAliveCountMax 2
20
21 # 条件配置
22 Match Group jump-users
23 AllowTcpForwarding yes
24 PermitTTY no
25 ForceCommand /usr/local/bin/jump-menu
26
27 Match Group admin
28 AllowTcpForwarding yes
29 PermitTTY yes
30 X11Forwarding yes
31
32 # 日志和监控
33 LogLevel VERBOSE
34 SyslogFacility AUTH
AI写代码

十、配置验证和调试
10.1 配置测试命令

1 # 测试配置文件语法
2 sudo sshd -t
3
4 # 查看当前生效配置
5 sudo sshd -T
6
7 # 查看特定用户的配置
8 sudo sshd -T -C user=alice,host=192.168.1.100
9
10 # 调试模式启动(前台运行)
11 sudo sshd -D -d
12
13 # 详细调试信息
14 sudo sshd -D -ddd
AI写代码

10.2 常见配置错误

1 # 错误1:权限问题
2 # 配置文件权限必须是644或更严格
3 sudochmod 644 /etc/ssh/sshd_config
4
5 # 错误2:语法错误
6 # 使用sshd -t检查语法
7
8 # 错误3:端口冲突
9 # 检查端口是否被占用
10 sudo netstat -tlnp | grep :22
11
12 # 错误4:防火墙阻止
13 # 检查防火墙规则
14 sudo ufw status
15 sudo iptables -L
16
17 # 错误5:SELinux策略
18 # 检查SELinux上下文
19 sudo setsebool -P ssh_sysadm_login on
AI写代码

十一、配置管理最佳实践
11.1 配置文件管理

1 # 备份原始配置
2 sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
3
4 # 使用版本控制
5 cd /etc/ssh
6 sudo git init
7 sudo git add sshd_config
8 sudo git commit -m “Initial SSH configuration”
9
10 # 模块化配置(OpenSSH 7.5+)
11 # 在 /etc/ssh/sshd_config.d/ 目录下创建配置片段
12 sudo echo “MaxAuthTries 3” > /etc/ssh/sshd_config.d/security.conf
AI写代码

11.2 配置变更流程

1 #!/bin/bash
2 # ssh_config_update.sh - 安全的SSH配置更新脚本
3
4 set -e
5
6 CONFIG_FILE=“/etc/ssh/sshd_config”
7 BACKUP_FILE=“/etc/ssh/sshd_config.backup.$(date +%Y%m%d_%H%M%S)”
8
9 # 1. 备份当前配置
10 echo “备份当前配置到 BACKUPFILE"11sudocp"BACKUP_FILE" 11 sudo cp "BACKUPFILE"11sudocp"CONFIG_FILE” “KaTeX parse error: Expected 'EOF', got '#' at position 23: …FILE" 12 13 #̲ 2. 应用新配置 14 e…CONFIG_FILE”
16
17 # 3. 测试配置语法
18 echo “测试配置语法…”
19 if sudo sshd -t; then
20 echo “配置语法正确”
21 else
22 echo “配置语法错误,恢复备份”
23 sudo cp “BACKUPFILE""BACKUP_FILE" "BACKUPFILE""CONFIG_FILE”
24 exit 1
25 fi
26
27 # 4. 重新加载配置
28 echo “重新加载SSH服务…”
29 sudo systemctl reload sshd
30
31 # 5. 验证服务状态
32 if sudo systemctl is-active --quiet sshd; then
33 echo “SSH服务运行正常”
34 echo “配置更新成功”
35 else
36 echo “SSH服务异常,恢复备份”
37 sudo cp “BACKUPFILE""BACKUP_FILE" "BACKUPFILE""CONFIG_FILE”
38 sudo systemctl restart sshd
39 exit 1
40 fi
AI写代码

十二、安全检查清单
12.1 基础安全检查

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

使用强加密算法
配置客户端存活检测
实施多重认证
设置会话限制
配置条件访问控制
启用入侵检测
定期轮换主机密钥
监控异常访问
总结
SSH配置文件是系统安全的重要组成部分,正确理解和配置这些参数对于:

提升安全性 - 通过限制访问、强化认证、使用强加密
优化性能 - 调整缓冲区、超时参数、禁用不必要功能
便于管理 - 使用条件配置、模块化管理、自动化部署
满足合规 - 记录详细日志、实施访问控制、定期审计
SSH配置的核心原则是"最小权限"和"纵深防御"。在保证功能需求的前提下,尽可能限制不必要的功能和权限,并通过多层安全措施构建坚固的防护体系。
————————————————
版权声明:本文为CSDN博主「程序员霸哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/logic1001/article/details/156592956

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐