告别枯燥!一键统一部署全平台 Linux 酷炫 SSH 登录提示 (MOTD) —— 附彩蛋
博客正文:
对于经常和服务器打交道的运维与开发人员来说,每天打开无数次 SSH 终端是家常便饭。但大多数情况下,迎接我们的总是枯燥的系统内核版本、长篇大论的 Debian 免责声明,以及毫无美感的 Last login 提示。
作为追求极致体验的开发者,我们完全可以给自己的服务器(或者交付给客户的机器)加上专属的品牌标识,顺便祈祷一下“永不宕机”。
今天分享一个全自适应的 Shell 自动化脚本,一键搞定带有彩色 ASCII 艺术字的 SSH 登录欢迎语(MOTD),并且完美兼容 Debian/Ubuntu 和 CentOS/RHEL 体系。
效果展示
配置完成后,当你或你的客户通过 SSH 登录服务器时,将会看到如下清爽且酷炫的界面:
核心痛点:系统差异化
在 Linux 中修改登录提示,通常有两种机制:
- 纯静态展示:修改
/etc/motd文件。缺点是不能使用 ANSI 颜色代码,只能显示纯文本,无法实现彩色高亮。 动态执行脚本:
- Debian / Ubuntu 系:原生支持通过 PAM 模块调用
/etc/update-motd.d/目录下的可执行脚本。 - CentOS / RHEL 系:默认不支持
update-motd.d,标准做法是将.sh脚本放入/etc/profile.d/目录下,在用户加载环境变量时触发。
- Debian / Ubuntu 系:原生支持通过 PAM 模块调用
这就导致了如果要批量运维不同发行版的机器,很容易出现“在这个系统生效,在那个系统不显示”的尴尬局面。
终极解决方案:一键全兼容自适应脚本
为了实现绝对的自动化和幂等性(重复执行不报错),我编写了下面这个自适应部署脚本。它不仅包含了高亮的品牌信息和“佛祖保佑” ASCII 画,还会自动识别底层系统环境并路由文件,最后贴心地关掉原生的 Last login 提示。
直接在任意 Linux 终端执行以下命令(你可以将代码中的 Global IEPL 替换为你自己的品牌名称):
第一版
#!/bin/bash
# 1. 清空原有的静态文件
> /etc/motd
# 2. 写入全新排版的动态脚本
cat << 'PAYLOAD_EOF' > /tmp/globaliepl_motd.sh
#!/bin/sh
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
WHITE='\033[1;37m'
RESET='\033[0m'
BOLD='\033[1m'
printf "${CYAN}${BOLD}"
cat << "ASCII_EOF"
____ _ _ _ ___ _____ ____ _
/ ___| | ___ | |__ __ _| | |_ _| ____| _ \| |
| | _| |/ _ \| '_ \ / _` | | | || _| | |_) | |
| |_| | | (_) | |_) | (_| | | | || |___| __/| |___
\____|_|\___/|_.__/ \__,_|_| |___|_____|_| |_____|
ASCII_EOF
printf "${RESET}"
printf "${BLUE}======================================================================${RESET}\n"
printf " ${BOLD}${GREEN}欢迎登录 Global IEPL 核心网络服务器${RESET}\n"
printf " ${YELLOW}官方网站:${RESET} https://www.globaliepl.com/\n"
printf " ${YELLOW}服务宗旨:${RESET} 为您提供稳定、高速的全球网络互联体验\n"
printf "${BLUE}======================================================================${RESET}\n\n"
printf "${WHITE}${BOLD}"
cat << "CROSS_EOF"
//////////////////////////////////////////////////////////////////////
// //
// _][_ //
// _[__]_ //
// | | //
// | | //
// _______| |_______ //
// |_______ _______| //
// | | //
// | | //
// | | //
// | | //
// | | //
// | | //
// __|__|__ //
// |________| //
// //
// 愿主保佑 永不宕机 永无 BUG //
//////////////////////////////////////////////////////////////////////
CROSS_EOF
printf "${RESET}\n\n"
PAYLOAD_EOF
# 3. 智能判断系统环境并路由文件
if [ -d "/etc/update-motd.d" ]; then
mv /tmp/globaliepl_motd.sh /etc/update-motd.d/10-uname
chmod +x /etc/update-motd.d/10-uname
chmod -x /etc/update-motd.d/10-help-text 2>/dev/null
chmod -x /etc/update-motd.d/50-motd-news 2>/dev/null
else
mv /tmp/globaliepl_motd.sh /etc/profile.d/99-globaliepl-motd.sh
chmod +x /etc/profile.d/99-globaliepl-motd.sh
fi
# 4. 关闭 SSH 的 LastLog 提示
sed -i 's/^#*PrintLastLog .*/PrintLastLog no/' /etc/ssh/sshd_config
# 5. 重启 SSH 服务
if systemctl is-active --quiet sshd; then
systemctl restart sshd
elif systemctl is-active --quiet ssh; then
systemctl restart ssh
fi
echo -e "\033[0;32m排版优化完成!请新开 SSH 窗口查看效果。\033[0m"第二版
#!/bin/bash
echo -e "\033[0;34m[1/5] 开始清理系统默认的静态 MOTD...\033[0m"
> /etc/motd
echo -e "\033[0;34m[2/5] 正在生成专属动态登录脚本...\033[0m"
# 使用 'PAYLOAD_EOF' 带有单引号,确保内部颜色变量不会被提前解析
cat << 'PAYLOAD_EOF' > /tmp/custom_motd.sh
#!/bin/sh
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
BOLD='\033[1m'
# 打印蓝色的品牌艺术字 (可通过 ASCII 生成工具自定义)
printf "${CYAN}${BOLD}"
cat << "EOF"
____ _ _ _ ___ _____ ____ _
/ ___| | ___ | |__ __ _| | |_ _| ____| _ \| |
| | _| |/ _ \| '_ \ / _` | | | || _| | |_) | |
| |_| | | (_) | |_) | (_| | | | || |___| __/| |___
\____|_|\___/|_.__/ \__,_|_| |___|_____|_| |_____|
EOF
printf "${RESET}"
# 打印品牌信息
printf "${BLUE}==========================================================${RESET}\n"
printf " ${BOLD}${GREEN}欢迎登录 Global IEPL 核心网络服务器${RESET}\n"
printf " ${YELLOW}官方网站:${RESET} https://www.globaliepl.com/\n"
printf " ${YELLOW}服务宗旨:${RESET} 为您提供稳定、高速的全球网络互联体验\n"
printf "${BLUE}==========================================================${RESET}\n\n"
# 打印佛祖保佑 ASCII 彩蛋
cat << "EOF"
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \| |// `. //
// / \||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
////////////////////////////////////////////////////////////////////
EOF
echo ""
PAYLOAD_EOF
echo -e "\033[0;34m[3/5] 正在识别操作系统并应用配置...\033[0m"
if [ -d "/etc/update-motd.d" ]; then
# Debian / Ubuntu 体系
mv /tmp/custom_motd.sh /etc/update-motd.d/10-uname
chmod +x /etc/update-motd.d/10-uname
# 静默禁用 Debian 烦人的默认新闻和帮助提示
[ -f /etc/update-motd.d/10-help-text ] && chmod -x /etc/update-motd.d/10-help-text
[ -f /etc/update-motd.d/50-motd-news ] && chmod -x /etc/update-motd.d/50-motd-news
else
# CentOS / RHEL / AlmaLinux 体系
mv /tmp/custom_motd.sh /etc/profile.d/99-custom-motd.sh
chmod +x /etc/profile.d/99-custom-motd.sh
fi
echo -e "\033[0;34m[4/5] 正在配置 SSH 服务 (关闭 LastLog)...\033[0m"
if grep -q "^#*PrintLastLog" /etc/ssh/sshd_config; then
sed -i 's/^#*PrintLastLog .*/PrintLastLog no/' /etc/ssh/sshd_config
else
echo "PrintLastLog no" >> /etc/ssh/sshd_config
fi
echo -e "\033[0;34m[5/5] 正在重启 SSH 服务使配置生效...\033[0m"
if systemctl is-active --quiet sshd; then
systemctl restart sshd
elif systemctl is-active --quiet ssh; then
systemctl restart ssh
fi
echo -e "\033[0;32m部署完成!请新开一个 SSH 终端查看效果。\033[0m"博客标签:
#Linux #运维自动化 #Shell脚本 #SSH定制 #CentOS #Debian #WHMCS开机脚本
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »