Auto‑Show CPU, Memory, and Disk Usage on Linux Login with a Single Script
This article provides a ready‑to‑use Bash script that, when placed in a system‑wide or user profile directory, automatically displays hostname, uptime, load average, IP address, CPU and memory usage, and disk mount statistics each time you log into a Linux server, along with color‑coded formatting and customization tips.
The author shares a Bash script that automatically presents key system metrics—CPU, memory, disk usage, IP address, load average, and uptime—immediately after logging into a Linux server, giving administrators an instant overview without manually running commands.
Effect Demonstration
Upon login the script prints a colored table showing the collected information, providing a clear sense of system health.
Script Implementation
Create the file /etc/profile.d/sysinfo.sh (or an equivalent user profile file) and insert the following content:
#!/bin/bash
# Colors
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
CYAN="\033[1;36m"
RESET="\033[0m"
# Basic info
HOSTNAME=$(hostname)
UPTIME=$(uptime -p | sed 's/up //')
LOADAVG=$(uptime | awk -F'load average:' '{print $2}' | sed 's/^ //')
# Memory
read MEM_TOTAL MEM_USED <<<$(free -m | awk '/Mem:/ {print $2, $3}')
MEM_PCT=$((MEM_USED * 100 / MEM_TOTAL))
# IP
IP_ADDR=$(hostname -I | awk '{print $1}')
# CPU usage (top method)
CPU_IDLE=$(top -bn2 | grep "Cpu(s)" | tail -n1 | awk -F',' '{print $4}' | grep -o '[0-9.]*')
CPU_USAGE=$(awk "BEGIN {printf \"%.0f\", 100 - $CPU_IDLE}")
# Output system information
echo -e "
${GREEN}Congratulations on logging in, here is the system info!${RESET}"
echo -e "${YELLOW}---------------------------------------------${RESET}"
printf "| %-8s | %-30s |
" "Resource" "Usage"
printf "|----------|--------------------------------|
"
printf "| %-8s | %-30s |
" "IP" "$IP_ADDR"
printf "| %-8s | %-30s |
" "CPU" "$CPU_USAGE%"
printf "| %-8s | %-30s |
" "Memory" "${MEM_USED}MB / ${MEM_TOTAL}MB (${MEM_PCT}%)"
printf "| %-8s | %-30s |
" "Load" "$LOADAVG"
printf "| %-8s | %-30s |
" "Uptime" "$UPTIME"
echo -e "${YELLOW}---------------------------------------------${RESET}"
echo -e "${CYAN}Disk Mount Information${RESET}"
echo -e "${YELLOW}-------------------------------------------------${RESET}"
printf "| %-10s | %-10s | %-10s | %-6s |
" "Mount" "Used" "Total" "Usage"
printf "|------------|------------|------------|--------|
"
df -h -x tmpfs -x devtmpfs | awk 'NR>1 {printf "| %-10s | %-10s | %-10s | %-6s |
", $6, $3, $2, $5}'
echo -e "${YELLOW}-------------------------------------------------${RESET}"
echo -e "${GREEN}Proceed with caution, avoid being the scapegoat!${RESET}
"Running the Script
Make the script executable: chmod +x /etc/profile.d/sysinfo.sh After this step, every new SSH, TTY, or shell session will display the formatted resource list automatically.
Placement Recommendations
System‑wide effect: /etc/profile.d/sysinfo.sh Current‑user only: ~/.bash_profile or ~/.bashrc Non‑bash shells (e.g., zsh): ~/.zshrc or /etc/zsh/zshrc Keep scripts in /etc/profile.d/ for centralized management.
Additional Notes
Scripts under /etc/profile.d/ must be executable and end with .sh.
Do not place logic that affects all users in personal ~/.bashrc or ~/.bash_profile files.
The script can be extended to display deployed application paths, ports, or other custom information, helping newcomers quickly understand the system’s services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
