Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 登入介面個性化:/etc/bashrc、/etc/motd、~/.bashrc 打造專屬歡迎畫面

編輯:關於 Linux

每次 SSH 登入看到預設提示符是否覺得單調?透過修改 /etc/bashrc (全域互動式 shell)、/etc/motd (登入訊息)、~/.bashrc (用戶專屬)、/etc/profile.d/ (登入腳本),即可打造專屬 ASCII 藝術、系統資訊、提醒事項、顏色提示符的歡迎體驗。本文系統整理四大配置層級、實戰範例、顏色控制、動態資訊注入、安全注意事項。

四大配置層級與生效範圍

檔案/目錄生效範圍Shell 類型典型用途
/etc/motd所有用戶登入 Shell (SSH、console、su -)靜態公告、維護通知、法律聲明
/etc/profile.d/*.sh所有用戶登入 Shell環境變數、路徑、函數、動態資訊腳本
/etc/bashrc (或 /etc/bash.bashrc)所有用戶互動式非登入 Shell (bash、tmux、screen、終端機分頁)別名、提示符 (PS1)、函數、歡迎畫面
~/.bashrc單一用戶互動式非登入 Shell個人偏好、專案專用別名、本地工具路徑
~/.bash_profile / ~/.profile單一用戶登入 Shell環境變數、啟動圖形介面、載入 .bashrc

實戰範例:打造動態歡迎畫面

方案一:/etc/profile.d/welcome.sh (推薦,登入 Shell 顯示一次)

sudo vi /etc/profile.d/welcome.sh
chmod +x /etc/profile.d/welcome.sh
#!/bin/bash
# 動態歡迎畫面 - 僅登入 Shell 顯示
[[ $- != *i* ]] && return  # 非互動式直接返回

# 顏色代碼
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; CYAN='\033[0;36m'; NC='\033[0m'

# ASCII 藝術 (可用 figlet/toilet 生成)
cat << "EOF"
${CYAN}
  _____ _           _     _____ _           _
 |_   _| |__   __ _| |_  |_   _| |__   __ _| |_ ___
   | | | '_ \ / _` | __|   | | | '_ \ / _` | __/ _ \
   | | | | | | (_| | |_    | | | | | | (_| | ||  __/
   |_| |_| |_|\__,_|\__|   |_| |_| |_|\__,_|\__\___|
${NC}
EOF

# 系統資訊
HOST=$(hostname)
KERNEL=$(uname -r)
UPTIME=$(uptime -p | sed 's/up //')
LOAD=$(uptime | awk -F'load average:' '{print $2}')
MEM_FREE=$(free -h | awk '/Mem:/ {print $7 "/" $2}')
DISK_ROOT=$(df -h / | awk 'NR==2 {print $4 "/" $2 " (" $5 " used)"}')
IP=$(ip -br addr show scope global | awk '{print $3}' | head -1 | cut -d/ -f1)

echo -e "${GREEN}歡迎登入 ${YELLOW}$HOST${NC}"
echo -e "核心版本: ${CYAN}$KERNEL${NC} | 運行時間: ${CYAN}$UPTIME${NC}"
echo -e "負載: ${CYAN}$LOAD${NC} | 記憶體可用: ${CYAN}$MEM_FREE${NC}"
echo -e "根分區剩餘: ${CYAN}$DISK_ROOT${NC} | IP: ${CYAN}$IP${NC}"

# 重要提醒 (範例:磁碟 > 90% 警告)
DISK_USE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [[ $DISK_USE -gt 90 ]]; then
    echo -e "${RED}⚠ 警告: 根分區使用率 ${DISK_USE}%${NC}"
fi

# 最後登入資訊
last -1 $USER | head -1
echo "----------------------------------------"

方案二:/etc/bashrc (每開啟終端機/分頁顯示,較輕量)

# 在 /etc/bashrc 末尾加入
# 僅互動式 shell 顯示簡短問候
[[ $- == *i* ]] && echo -e "\033[1;32m歡迎, \u@\h \033[0m|\033[1;36m \d \t \033[0m| \w"
# 輸出範例:歡迎, user@host | 週一 1月15 10:30:45 | /home/user

方案三:/etc/motd (靜態公告,所有登入方式皆顯示)

sudo vi /etc/motd
===========================================================
            歡迎使用公司生產環境伺服器
===========================================================
⚠ 此系統受監控與稽核,所有操作將被記錄。
⚠ 未經授權禁止安裝軟體、修改系統配置、存取敏感資料。
📞 緊急聯繫: it-support@company.com / 內線 8888
📅 計畫維護窗口: 每週日 02:00-06:00 UTC
===========================================================

方案四:~/.bashrc (個人專屬,含顏色提示符 PS1)

# ~/.bashrc 片段
# 顏色提示符:用戶@主機:路徑 $ (綠色用戶、藍色路徑、紅色 root 警示)
if [[ $EUID -eq 0 ]]; then
    PS1='\[\033[0;31m\]\u@\h\[\033[0m\]:\[\033[0;34m\]\w\[\033[0m\]# '
else
    PS1='\[\033[0;32m\]\u@\h\[\033[0m\]:\[\033[0;34m\]\w\[\033[0m\]\$ '
fi

# 實用別名
alias ll='ls -alF --color=auto'
alias la='ls -A --color=auto'
alias l='ls -CF --color=auto'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias ports='ss -tulnp'
alias myip='curl -s ifconfig.me'

# 歡迎訊息 (僅首次登入顯示,避免 tmux/screen 重複)
if [[ -z "$WELCOME_SHOWN" ]]; then
    export WELCOME_SHOWN=1
    echo -e "\033[1;36m$(date '+%Y-%m-%d %H:%M:%S') 歡迎回來, \u!\033[0m"
fi

顏色與格式控制碼速查

代碼效果
\033[0m / \e[0m重置所有屬性
\033[1m粗體
\033[4m底線
\033[7m反轉
\033[30m-37m前景色 (黑紅綠黃藍洋紅青白)
\033[90m-97m高亮前景色
\033[40m-47m背景色
\033[100m-107m高亮背景色

在 echo -e、printf、PS1 中使用,PS1 需用 \[...\] 包裹非列印字元避免換行計算錯誤。

進階:動態資訊注入 (CPU 溫度、服務狀態、Docker 容器數)

# /etc/profile.d/sysinfo.sh 片段
CPU_TEMP=$(sensors 2>/dev/null | grep -m1 'Package id 0' | awk '{print $4}' | sed 's/+//')
DOCKER_COUNT=$(docker ps -q 2>/dev/null | wc -l)
FAILED_SVCS=$(systemctl --failed --no-legend | wc -l)

[[ -n "$CPU_TEMP" ]] && echo -e "CPU 溫度: \033[1;33m$CPU_TEMP\033[0m"
echo -e "Docker 容器: \033[1;36m$DOCKER_COUNT\033[0m"
[[ $FAILED_SVCS -gt 0 ]] && echo -e "\033[1;31m失敗服務: $FAILED_SVCS\033[0m"

安全與最佳實踐

  • 避免洩露敏感資訊:歡迎畫面勿顯示完整 IP 範圍、內部網路拓撲、帳號列表、密碼提示。
  • 效能考量:/etc/profile.d/*.sh 每次登入執行,複雜指令 (如 df、sensors) 會增加登入延遲。建議快取或簡化。
  • 非互動式 Shell 排除:腳本開頭加 [[ $- != *i* ]] && return,避免 scp、rsync、git、ansible、cron 觸發輸出干擾協議。
  • 權限控制:/etc/profile.d/、/etc/bashrc、/etc/motd 僅 root 可寫 (644);~/.bashrc 用戶自有。
  • 版本控制:將 /etc/profile.d/、/etc/bashrc 納入 Ansible/Puppet/Salt/Git 管理,確保集群一致性。

工具輔助:figlet / toilet 生成 ASCII 藝術

sudo apt/dnf install figlet toilet
figlet "Welcome"           # 標準字體
figlet -f slant "Welcome"  # 斜體
toilet -f big "Welcome"    # 大字體
toilet -f mono12 -F metal "Server"  # 金屬效果

善用四大配置層級、顏色控制、動態資訊、安全原則,即可將每次登入從「冷冰冰的提示符」轉化為「資訊豐富、品牌識別、安全提醒」的專業歡迎體驗,提升運維效率與團隊歸屬感。

Copyright © Windows教程網 All Rights Reserved