Linux
OS-Mgmt
Motd

Introduction

The Message of the Day (MOTD) is a useful feature in Linux that displays a message to users upon logging into the system. This guide will show you how to create a custom MOTD script to display a personalized message and user login information.

Steps to Customize MOTD

1. Create a Custom MOTD Script

First, create a new script in the /etc/update-motd.d/ directory. This directory contains scripts that generate the MOTD.

nano /etc/update-motd.d/99-custom

2. Add Custom Content to the Script

Add the following content to the 99-custom script to display a welcome message, server URL, and recent user logins:

  • Example1
#!/bin/sh
echo "Welcome to the Server "
echo " UAT SERVER  URL : https://uat.example.in "
echo "This is the UAT Server where we deploy test projects please use this accordingly"
echo ""
echo "Recent User logins to this server:"
last -n 10
echo ""
  • Example2
# cat /etc/update-motd.d/99-custom-motd 
#!/bin/bash
 
# Collect and format system information
LAST_LOGINS=$(last -n 10 | head -n 10)
RAM_USAGE=$(free | awk '/Mem:/ {printf "%.2f%%", $3/$2 * 100.0}')
UPGRADES_AVAILABLE=$(apt list --upgradable 2>/dev/null | grep -c upgradable)
UPTIME=$(uptime -p)
DISK_SPACE=$(df -h / | awk '/\// {print $4}')
PUBLIC_IP=$(curl -s ifconfig.me)
USER_LOGGED_IN=$(whoami)
ACTIVE_SESSIONS=$(who | wc -l)
TOP_PROCESSES=$(ps -eo pid,comm,%mem --sort=-%mem | head -n 5)
SYSTEM_HEALTH=$(systemctl --failed 2>/dev/null || echo "No failed services")
LAST_REBOOT=$(who -b | awk '{print $3, $4}')
ACCOUNT_EXPIRY=$(chage -l $(whoami) | grep "Account expires" | awk -F': ' '{print $2}')
SECURITY_UPDATES=$(apt list --upgradable | grep -i security | wc -l) security updates available
OS_VERSION=$(lsb_release -d | awk -F': ' '{print $2}')
PUBLIC_IP=$(curl -s ifconfig.me)
OPEN_PORTS=$(nmap -p- $PUBLIC_IP)
 
 
#!/bin/bash
 
# Define colors and styles using ANSI escape codes
BOLD="\033[1m"
RESET="\033[0m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
YELLOW="\033[0;33m"
MAGENTA="\033[0;35m"
RED="\033[0;31m"
BLUE="\033[0;34m"
WHITE="\033[0;37m"
 
# Display MOTD
echo -e "${BOLD}${CYAN}======================================================================="
echo -e "                         SYSTEM INFORMATION                            ${RESET}"
echo -e "=======================================================================${RESET}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Public IP -----------${RESET}"
echo -e " ${PUBLIC_IP}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- User Logged In -----------${RESET}"
echo -e " ${USER_LOGGED_IN}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Uptime -----------${RESET}"
echo -e " ${UPTIME}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- RAM Usage -----------${RESET}"
echo -e " ${RAM_USAGE}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Disk Space -----------${RESET}"
echo -e " ${DISK_SPACE} free"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Active Sessions -----------${RESET}"
echo -e " ${ACTIVE_SESSIONS}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Last Reboot -----------${RESET}"
echo -e " ${LAST_REBOOT}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Last Logins -----------${RESET}"
echo -e " ${LAST_LOGINS}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Top Processes -----------${RESET}"
echo -e " ${TOP_PROCESSES}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- System Health -----------${RESET}"
echo -e " ${SYSTEM_HEALTH}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Upgrades Available -----------${RESET}"
echo -e " ${UPGRADES_AVAILABLE}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Security Updates -----------${RESET}"
echo -e " ${SECURITY_UPDATES}"
 
echo -e "${BOLD}${GREEN}------------------------------------------------------------- Ngrok Ports -----------${RESET}"
echo -e " ${OPEN_PORTS}"
 
echo -e "${BOLD}${CYAN}=======================================================================${RESET}"

3. Save and Exit

Save the file and exit the text editor (Nano: press CTRL + X, then Y, then Enter).

4. Make the Script Executable

Change the permissions of the script to make it executable:

sudo chmod +x /etc/update-motd.d/99-custom

5. Test the MOTD Script

Run the run-parts command to test the new MOTD script:

run-parts /etc/update-motd.d

6. Verify the Custom MOTD

You can view the content of your custom MOTD script with the cat command to ensure it is correct:

cat /etc/update-motd.d/99-custom

Example Output

When a user logs in, they will see the following message:

Welcome to the  UAT Server 
 UAT SERvER  URL : https://uat.example.in 
This is the UAT Server where we deploy test projects please use this accordingly

Recent User logins to this server:
user1   pts/0        192.168.1.1     Fri Jul 23 12:34   still logged in
user2   pts/1        192.168.1.2     Fri Jul 23 12:33 - 12:34  (00:01)
...

Conclusion

Customizing the MOTD in Linux is a simple yet effective way to convey important information to users upon login. By following these steps, you can create a personalized MOTD that enhances user experience and provides useful information about the system.

This guide includes detailed steps for creating and customizing the MOTD script, making it executable, and verifying its content. It also includes example output to illustrate what users will see upon login.


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!