关闭 printmotd 并清空 /etc/motd 是阻断 ssh 登录时泄露内核版本等敏感信息最直接有效的方式,还需停用 motd-news、landscape-sysinfo 等自动更新服务,并禁用 /etc/update-motd.d/ 下非必要脚本执行权限。

PrintMotd 本身不直接显示内核版本,但它会输出 /etc/motd 文件内容——而该文件在 Ubuntu/Debian 等系统中常被自动更新脚本写入含内核版本的提示(如 “Ubuntu 22.04.4 LTS\nKernel: 6.8.0-59-generic”)。关闭 PrintMotd 是阻断这条泄露路径最直接有效的方式。
确认并禁用 PrintMotd
编辑 SSH 主配置:
sudo nano /etc/ssh/sshd_config
查找或新增以下行(确保未被注释):
- PrintMotd no —— 明确禁止 SSH 登录后读取并显示 /etc/motd
- 若存在 PrintLastLog yes,也建议改为 PrintLastLog no,避免“Last login”行间接暴露主机名或部署环境
同步清理 motd 源文件
即使 PrintMotd 已关,/etc/motd 文件本身仍可能被其他服务(如 systemd-motd、landscape-sysinfo)动态写入。清空它可彻底消除残留风险:
sudo truncate -s 0 /etc/motd
对 Ubuntu/Debian,还可停用自动更新服务:
- sudo systemctl disable --now motd-news.service(Ubuntu 20.04+)
- sudo systemctl disable --now landscape-sysinfo.service(若启用)
注意 motd.d 的潜在影响
部分系统使用 /etc/update-motd.d/ 目录下脚本动态生成 motd。即使 PrintMotd 关闭,这些脚本若被其他机制调用(如 cron 或 PAM),仍可能产生日志或临时输出。
稳妥做法是:
- 检查该目录是否存在可执行脚本:ls -l /etc/update-motd.d/
- 对非必要脚本移除执行权限:sudo chmod -x /etc/update-motd.d/*
- 或重命名(如加 .disabled 后缀)以保留备份
验证是否生效
完成修改后重载服务:
sudo systemctl reload sshd
新开一个 SSH 连接,观察登录后是否还出现包含内核版本、发行版信息或系统摘要的横幅。若仅剩 shell 提示符(如 user@host:~$),说明已收敛成功。











