Customizing Your Linux Welcome Message using motd (Message of the Day)

The Message of the Day (MOTD) in Linux is a great way to present system information and communicate important notices every time users log in. The standard /etc/motd file can be edited to include static text, but on modern systems, dynamic motd scripts provide powerful ways to display system status, security warnings, and other useful information.

This guide will show you not only how to modify the static /etc/motd file but also how to use dynamic motd scripts for advanced configurations.

Why Use the motd?

The /etc/motd file serves several purposes:

  • Unified Communication: Share updates or instructions with all users.
  • Security Reminders: Include warnings or best practices for better security.
  • System Identity: Differentiate between multiple systems with a unique message.
  • Compliance: Document that users have seen a specific message when logging in.

Step-by-Step Guide to Customizing the motd

1. Editing the Static motd File

Customizing the static /etc/motd is the simplest way to add a custom welcome message:

  • Open the motd file using a text editor:

      sudo nano /etc/motd
    
  • Add your custom message. For example:

      Welcome to MyServer!
      Unauthorized access is prohibited.
      Contact [email protected] for support.
    
  • Save and exit the file.

  • Test the Change: Log out and back in to see the new message.

Advanced motd Configuration Using Dynamic Scripts

Modern Linux systems like Ubuntu use dynamic motd scripts located in /etc/update-motd.d/. These scripts execute when a user logs in, allowing you to display dynamic content such as system load, uptime, and more.

2. Creating a Custom motd Script

To create a custom script, follow these steps:

  1. Navigate to the /etc/update-motd.d/ directory:

     cd /etc/update-motd.d/
    
  2. Create a new script. Use a number prefix to determine the order (e.g., 99-custom):

     sudo nano 99-custom
    
  3. Add the following content to display a custom message:

     #!/bin/bash
     echo "Welcome to MyServer - $(hostname)"
     echo "Current Load: $(uptime)"
     echo "Memory Usage: $(free -h | grep Mem | awk '{print $3 " / " $2}')"
    
  4. Save and exit the file.

  5. Make the script executable:

     sudo chmod +x 99-custom
    
  6. Test the script by logging out and back in.

This script will display your server’s hostname, current system load, and memory usage every time a user logs in.

3. Displaying Disk Usage with motd

To include disk usage statistics, add the following to a custom script:

#!/bin/bash
echo "Disk Usage:"
df -h | grep -E '^/dev/'

This will display disk usage for all mounted drives when a user logs in.

4. Adding a Custom Header to the motd

To create a header for your motd, use ASCII art. You can generate this online using tools like patorjk.com’s ASCII Art Generator.

  • Add the following code snippet to your script:
#!/bin/bash
echo "==========================="
echo "   Welcome to MyServer!"
echo "==========================="

This makes your login message stand out and provides a visually appealing header for users.

5. Adding a System Updates Notification

To inform users about pending updates:

#!/bin/bash
updates=$(apt list --upgradable 2>/dev/null | grep -c upgradable)
if [ "$updates" -gt 0 ]; then
    echo "You have $updates package(s) that can be updated."
else
    echo "Your system is up to date."
fi

This script checks for available package updates and displays the count.

6. Conditional motd Based on User Login

If you want to show specific information only to certain users (e.g., admins), use a conditional statement:

#!/bin/bash
if [ "$USER" = "admin" ]; then
    echo "Admin Alert: Please review security logs."
else
    echo "Welcome, $USER!"
fi

This script will display a custom alert if the user logging in is named admin.

Disabling Dynamic motd Scripts

If you want to prevent dynamic scripts from running and prefer to use a static motd, you can disable individual scripts:

  1. Navigate to /etc/update-motd.d/.

  2. Remove execute permissions from the unwanted script(s):

     sudo chmod -x 10-help-text
    

This will stop the selected script from running, allowing you to control which messages are shown.

Further Notes

  • Dynamic vs Static motd: While the static /etc/motd is straightforward for quick messages, dynamic scripts provide far more flexibility, enabling system stats, alerts, and updates.
  • Script Order: The order in which scripts run is controlled by the prefix number (00, 10, 99, etc.). Adjust these numbers to control display order.
  • Testing Changes: Always test your motd configurations by logging out and back in to ensure they display as expected.

By leveraging both static and dynamic motd configurations, you can create a highly customized welcome message that displays vital information, alerts, and more, making your Linux system more user-friendly and informative.

Would you like to explore more advanced motd scripting techniques or integrate other system stats?