OpenSSH配置文件必须由root拥有且权限为600,否则易被提权或植入后门;修复命令为sudo chown root:root /etc/ssh/sshd_config && sudo chmod 600 /etc/ssh/sshd_config,并建议启用auditd监控、禁用非必要功能及设置chattr +i锁定。

OpenSSH配置文件权限不当或被恶意篡改,是服务器提权和后门植入的常见入口。核心原则就一条:配置文件必须由root拥有、仅root可写,且不能被组或其他用户读取或修改。
检查sshd_config权限与归属
执行以下命令快速验证:
ls -l /etc/ssh/sshd_config
正常输出应类似:
-rw------- 1 root root 4212 Apr 5 10:23 /etc/ssh/sshd_config
使用ydata-profiling(前身为pandas-profiling)生成全面的数据质量报告,包含相关性分析、缺失值模式和基数检测。导出交互式HTML仪表板和JSON摘要。
关键点:
• 权限必须是 600(即 -rw-------),禁止 group/others 有 r/w/x 权限
• 所有者必须是 root:root,不能是普通用户或 wheel 等特权组
• 若出现 -rw-r--r-- 或 -rw-rw-rw-,立即修复
自动修复权限与归属
用一条命令完成修正:
sudo chown root:root /etc/ssh/sshd_config && sudo chmod 600 /etc/ssh/sshd_config
补充建议:
• 同样处理其他敏感 SSH 配置文件(如 /etc/ssh/sshd_config.d/*.conf)
• 若使用 systemd,可加防护:运行 sudo systemctl edit sshd,添加如下内容防止覆盖:
[Service]
ProtectSystem=strict
ProtectHome=true
监控配置文件变更(实战级防护)
仅靠定期检查不够,需实时感知篡改。推荐两种轻量方案:
-
用 auditd 持续审计:运行
sudo auditctl -w /etc/ssh/sshd_config -p wa -k sshd_config_change
后续查日志:sudo ausearch -k sshd_config_change | aureport -f -i -
用 inotifywait 做简易告警(适合无 auditd 环境):
安装 inotify-tools 后,运行:
inotifywait -m -e modify,attrib,move_self /etc/ssh/sshd_config | while read e; do logger "ALERT: sshd_config was modified"; echo "$(date): $e" >> /var/log/sshd_config_audit.log; done &
预防性加固:禁用非必要功能 + 配置锁定
权限只是基础,还需从配置源头降低风险:
- 确认 PermitRootLogin no 和 AllowUsers(显式白名单)已启用
- 禁用 IgnoreRhosts、RhostsRSAAuthentication 等遗留认证方式
- 设置 StrictModes yes(默认开启,但建议显式声明)
- 对生产环境,可将 sshd_config 设为不可修改:
sudo chattr +i /etc/ssh/sshd_config(需先停 sshd;修改时先 chattr -i)

















