sshd@修改sshd配置兼容低版本客户端协议

目的

修改SSH服务器配置以兼容低版本客户端,主要需要启用一些较旧但仍然安全的加密算法和认证方法。

1. 备份原配置文件

1
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup

2. 编辑SSH配置文件

1
sudo vi /etc/ssh/sshd_config

3. 添加或修改以下配置项

3.1启用兼容的密钥交换算法KexAlgorithms

KexAlgorithms diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521

3.2启用兼容的加密算法Ciphers

Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes192-cbc,aes256-cbc

3.3启用兼容的MAC算法MACs

MACs hmac-sha2-256,hmac-sha2-512,hmac-sha1

3.4启用兼容的主机密钥算法PubkeyAcceptedKeyTypes

PubkeyAcceptedKeyTypes ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ssh-ed25519

3.5其他兼容性设置

1
2
3
4
5
6
7
8
9
10
11
# 允许SSH协议版本1和2(不推荐,仅在必要时使用)
# Protocol 2,1

# 启用密码认证(如果需要)
PasswordAuthentication yes

# 允许root登录(根据需要调整)
PermitRootLogin yes

# 启用公钥认证
PubkeyAuthentication yes

4. 完整的兼容性配置示例

# SSH兼容低版本客户端配置
# 在 /etc/ssh/sshd_config 中添加或修改以下内容

# 基本设置
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# 兼容性密钥交换算法
KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1

# 兼容性加密算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc

# 兼容性MAC算法
MACs umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1

# 主机密钥算法
HostKeyAlgorithms ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519

# 公钥接受类型
PubkeyAcceptedKeyTypes ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519

# 认证设置
PubkeyAuthentication yes
PasswordAuthentication yes
ChallengeResponseAuthentication no
UsePAM yes

# 登录设置
PermitRootLogin yes
StrictModes yes
MaxAuthTries 6
MaxSessions 10

# 其他设置
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server

# 日志设置
SyslogFacility AUTH
LogLevel INFO

5. 验证配置并重启服务

5.1检查配置文件语法

1
sudo sshd -t

5.2重启SSH服务

1
2
3
4
5
# Ubuntu/Debian
sudo systemctl restart ssh

# CentOS/RHEL
sudo systemctl restart sshd

5.3查看服务状态

1
sudo systemctl status ssh

6. 安全注意事项

  • 安全风险:启用旧算法会降低安全性,建议只在必要时临时启用
  • 逐步升级:建议客户端尽快升级到支持现代加密算法的版本
  • 定期审查:定期检查并移除不再需要的兼容性设置
  • 监控日志:密切监控SSH登录日志,确保没有异常活动

7. 针对特定客户端的配置

如果只需要兼容特定的旧版客户端,可以使用Match指令:

1
2
3
4
# 针对特定IP或用户的兼容配置
Match Address 192.168.1.100
KexAlgorithms diffie-hellman-group1-sha1
Ciphers aes128-cbc,3des-cbc

这样可以在保持整体安全性的同时,为特定客户端提供兼容性支持。