改了/etc/motd没生效是因为发行版动态机制覆盖:Ubuntu/Debian用update-motd脚本,CentOS/RHEL 8+用systemd-motd服务,且OpenSSH的PrintMotd可能被禁用。

/etc/motd 文件改了却没生效?不是文件写错了,而是你没绕过发行版自带的动态机制。
为什么改了 /etc/motd 却没变化
/etc/motd 却没变化直接写入 /etc/motd 在多数现代发行版上「看似正确,实则无效」。原因有三:
- Ubuntu/Debian 默认启用
update-motd机制,每次登录都重新生成内容,覆盖你手动写的/etc/motd - CentOS/RHEL 8+ 启用
systemd-motd服务,自动刷新并锁定/etc/motd - OpenSSH 的
PrintMotd配置可能被设为no,连读都不读
验证方式:运行 sudo sshd -T | grep printmotd,输出 printmotd no 就说明 SSH 根本不加载它。
Ubuntu/Debian 必须用 /etc/update-motd.d/ 脚本
/etc/update-motd.d/ 脚本这是官方推荐路径,支持实时信息(如更新数、负载),且不会被重启清空。
- 脚本按文件名数字前缀排序执行:
00-header→10-sysinfo→99-custom - 每个脚本必须可执行:
sudo chmod +x /etc/update-motd.d/99-custom - 输出必须写到
stdout,不能重定向或静默退出 - 示例创建自定义提示:
sudo tee /etc/update-motd.d/99-custom <p>注意:不要在脚本里用 <code>echo -n</code>,换行符是 motd 渲染的关键。</p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill10918" title="Linux Security Scanner"><img src="https://img.php.cn/upload/skill/001/246/273/6adaf20cf0281539.jpg" alt="Linux Security Scanner" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/xiazai/skill10918" title="Linux Security Scanner" class="overflowclass">Linux Security Scanner</a> <p class="overflowclass">Linux 安全审计工具,检查 SSH 配置、开放/监听端口、防火墙规则(ufw/iptables/nftables)、失败登录尝试、sudoers 权限...</p> </div> <a rel="nofollow" href="/xiazai/skill10918" title="Linux Security Scanner" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div><hr><h3><h3>CentOS/RHEL 8+ 要先禁用 <code>systemd-motd</code> </h3></h3><p>否则你写的任何内容都会在下次 systemd 服务启动时被覆盖。</p>
- 停用并屏蔽服务:
sudo systemctl disable --now systemd-motd - 确认已停止:
sudo systemctl is-active systemd-motd应返回inactive - 清空或重命名
/etc/update-motd.d/(防止旧脚本干扰) - 再写静态内容:
echo "Welcome to $(hostname)" | sudo tee /etc/motd
别忘了检查 sshd_config 中 PrintMotd yes 并重载服务:sudo systemctl reload sshd。
想彻底跳过所有动态逻辑?只用纯文本
适合安全审计、CI 环境或容器场景——要求稳定、不可变、无副作用。
- Ubuntu:禁用
motd-news服务:sudo systemctl disable --now motd-news - 清空
/etc/update-motd.d/目录(sudo rm -f /etc/update-motd.d/*) - 直接写入:
printf "? Env: PROD | Owner: ops-team\n" | sudo tee /etc/motd - 确保权限正确:
sudo chmod 644 /etc/motd && sudo chown root:root /etc/motd
最容易被忽略的一点:某些云镜像或容器基础镜像会把 /etc/motd 设为只读挂载,此时即使 root 也写不进——得先检查挂载属性:findmnt -T /etc/motd。










