在CentOS系统中,自定义消息可根据场景(启动提示、登录提示、实时广播等)选择不同方法,以下是常见且实用的实现方式:
/etc/motd(Message of the Day)是用户登录时显示的标准启动消息文件。直接编辑该文件即可修改启动提示内容:
sudo nano /etc/motd========================================
Welcome to CentOS Server!
System Status: Running (Last Updated: $(date))
Contact Admin: admin@example.com
========================================
Ctrl+O→Enter→Ctrl+X)。若需要动态生成消息(如实时日期、系统负载、磁盘空间等),可通过修改/etc/update-motd.d/目录下的脚本实现:
cd /etc/update-motd.d/99-custom-message):sudo nano 99-custom-message#!/bin/bash
echo "=== System Daily Check ==="
echo "Date: $(date)"
echo "Uptime: $(uptime -p)"
echo "Disk Usage: $(df -h / | awk 'NR==2 {print $5}')"
echo "Last Login: $(lastlog -u $(whoami) | awk 'NR==2 {print $4" "$5" "$6" "$7}')"
sudo chmod +x 99-custom-messagesudo rm /var/run/motd若需在SSH登录时显示自定义消息(如安全警告),可通过pam_motd模块配置:
sudo nano /etc/pam.d/sshdsession optional pam_motd.so motd=/run/motd.dynamic noupdate
/etc/update-motd.d/目录下的脚本生成动态消息。若需强制显示静态消息,可直接修改/etc/motd文件。若需立即向所有登录用户发送紧急通知(如系统维护、宕机预警),可使用wall(Write All)命令:
sudo nano custom_message.txt,内容示例:【系统紧急通知】
服务器将于今晚22:00-23:00进行系统升级,期间服务将短暂中断,请提前保存工作!
sudo wall -f custom_message.txt若需将系统消息发送至邮箱、微信、钉钉等外部渠道,可结合邮件服务或第三方API实现:
mailx):echo "系统磁盘空间不足!当前使用率:$(df -h / | awk 'NR==2 {print $5}')" | mail -s "CentOS系统警告" admin@example.com
curl调用API):curl 'https://api.weixin.qq.com/cgi-bin/message/send?access_token=YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-d '{"touser":"@all","msgtype":"text","text":{"content":"服务器CPU负载过高!当前值:$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk "{print 100 - \$1}")%"}}}'
/etc/crontab或自定义脚本中,可实现定时或触发式通知。sudo cp /etc/motd /etc/motd.bak)。chmod +x),否则无法运行。wall命令需root权限,避免误操作影响系统正常使用。