《远程访问安全加固策略》
第4章 SSH客户端安全配置

4、SSH客户端安全配置:StrictHostKeyChecking、连接超时设置、加密算法白名单

SSH客户端的安全配置是远程访问安全的第一道防线。通过合理配置客户端参数,可以有效防止中间人攻击、减少闲置连接风险,并确保仅使用强加密算法进行通信。本节将深入讲解三个核心配置项:StrictHostKeyChecking、连接超时设置以及加密算法白名单。

SSH客户端安全配置三大支柱 🔑 StrictHostKeyChecking 验证服务器主机密钥 防止中间人攻击 推荐值: yes ⏱️ 连接超时设置 ServerAliveInterval ServerAliveCountMax 自动断开闲置连接 🔒 加密算法白名单 Ciphers +MACs +KexAlgorithms 仅允许强加密算法 禁用弱算法

4.1 StrictHostKeyChecking:防御中间人攻击

StrictHostKeyChecking 是 SSH 客户端用于验证远程服务器主机密钥的策略。当设置为 yes 时,客户端会严格比对已知主机密钥,若密钥不匹配或未知,则拒绝连接。这能有效防止攻击者通过伪造服务器身份进行中间人攻击。

配置值 行为描述 安全等级
yes 严格验证,密钥不匹配则拒绝连接 高
accept-new 自动接受新主机密钥,但验证已有密钥 中
no 不验证主机密钥,自动接受任何密钥 低(不推荐)

推荐配置:

# 在 ~/.ssh/config 或 /etc/ssh/ssh_config 中设置
Host *
    StrictHostKeyChecking yes
    UserKnownHostsFile ~/.ssh/known_hosts
StrictHostKeyChecking 验证流程 SSH客户端 SSH服务器 ① 发起连接请求 ② 返回主机密钥指纹 ③ 比对 known_hosts 文件

4.2 连接超时设置:避免闲置连接风险

通过设置 ServerAliveInterval 和 ServerAliveCountMax,SSH客户端可以定期检测连接是否存活,并在服务器无响应时自动断开连接。这有助于释放系统资源,并防止因用户离开终端而导致的未授权访问。

  • ServerAliveInterval:客户端向服务器发送存活检测消息的间隔时间(秒)。推荐值:60。
  • ServerAliveCountMax:未收到响应前允许的最大检测次数。推荐值:3。
  • 超时总时间 = ServerAliveInterval × ServerAliveCountMax。例如 60×3=180秒(3分钟)无响应则断开。

配置示例:

# 在 ~/.ssh/config 中设置
Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    # 可选:TCPKeepAlive 作为补充
    TCPKeepAlive yes
连接超时检测机制 T=0 连接建立 T=60s 发送心跳 T=120s 发送心跳 T=180s 断开连接 3次无响应 → 自动断开闲置连接

4.3 加密算法白名单:禁用弱加密算法

SSH支持多种加密算法,但部分旧算法(如 aes128-cbc、hmac-md5)存在已知漏洞。通过配置 Ciphers、MACs 和 KexAlgorithms 白名单,可以强制客户端仅使用经过审核的强加密算法,提升通信安全性。

配置项 推荐算法(白名单) 应禁用算法
Ciphers chacha20-poly1305@openssh.com, aes256-gcm@openssh.com, aes128-gcm@openssh.com aes128-cbc, 3des-cbc, blowfish-cbc
MACs hmac-sha2-256, hmac-sha2-512 hmac-md5, hmac-sha1
KexAlgorithms curve25519-sha256, diffie-hellman-group16-sha512 diffie-hellman-group1-sha1, diffie-hellman-group14-sha1

完整配置示例:

# 在 ~/.ssh/config 或 /etc/ssh/ssh_config 中设置
Host *
    # 加密算法白名单
    Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
    MACs hmac-sha2-256,hmac-sha2-512
    KexAlgorithms curve25519-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512
加密算法白名单过滤机制 弱算法 强算法 🔍 白名单 Ciphers MACs KexAlgorithms ✅ 安全连接 仅使用白名单算法

4.4 综合配置建议

将上述三个配置项整合到 ~/.ssh/config 文件中,形成统一的SSH客户端安全策略:

# 全局SSH客户端安全配置
Host *
    # 严格主机密钥检查
    StrictHostKeyChecking yes
    UserKnownHostsFile ~/.ssh/known_hosts

    # 连接超时设置
    ServerAliveInterval 60
    ServerAliveCountMax 3
    TCPKeepAlive yes

    # 加密算法白名单
    Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
    MACs hmac-sha2-256,hmac-sha2-512
    KexAlgorithms curve25519-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512

    # 其他安全选项
    Compression no
    ForwardX11 no
    PermitLocalCommand no

💡 提示: 配置完成后,建议使用 ssh -v user@host 命令验证实际使用的算法和参数。若服务器不支持白名单中的算法,连接将失败,此时需根据服务器端配置进行调整。

SSH客户端安全配置综合架构 SSH 安全 StrictHostKeyChecking 连接超时 算法白名单 防御中间人攻击 避免闲置连接风险 确保强加密通信

专注课程资料整理