How to Create a Custom Linux MOTD Screen

Creating a custom Message of the Day (Linux MOTD) screen on Linux involves modifying certain files and configurations. We’ll use the /etc/update-motd.d/ directory to add scripts that fetch and display relevant information.

Source: Unix.StackExchange.com

Creating a Custom Linux MOTD: Enhanced Guide

Understanding MOTD:

The Message of the Day (MOTD) is a brief message displayed to users when they log into a Linux system. It can contain welcome messages, system information, announcements, or even artistic ASCII banners.

Prerequisites:

  • A Linux system (examples use Debian/Ubuntu).
  • Access to a terminal.
  • Permissions to run commands with sudo.
  • A command-line text editor like nano.

Step 1: Access the Terminal and Check MOTD is installed

Open a terminal on your Linux system.

Bash
sudo apt update
sudo apt install update-motd

While most Linux systems have MOTD enabled by default, this command ensures it’s installed and up-to-date. The update-motd package is crucial for generating dynamic MOTDs.


Step 2: Backup Existing Linux MOTD and Update Scripts (Optional)

Backup the existing MOTD file and update scripts:


The Message of the Day (MOTD) in Linux is typically stored in the “/etc/motd” file. In some versions of Ubuntu and Debian, it’s located in “/etc/update-motd.d”.

Bash
sudo cp -r /etc/update-motd.d "/etc/update-motd.d_backup_$(date +%F)"


Step 3: Create Custom Linux MOTD Scripts

Scripts in /etc/update-motd.d are executed in alphanumeric order. The number prefix on your script determines its position in the MOTD. We will create a script named 10-custom-info to have it appear early.

Bash
sudo nano /etc/update-motd.d/10-custom-info

Add the following content to the script:

Bash
#!/bin/bash

# Gather System Information
WHO=$(who | awk '{print $1}' | sort | uniq)  
CPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
DISK=$(df -h / | awk 'NR==2 {print $5}')
RAM=$(free -m | awk 'NR==2 {printf "%.1f%%", $3/$2*100}')

# Display Welcome Message
echo "Welcome to [Your System Name]!"
echo "============================="

# Display Dynamic Information (Customizable)
echo "Logged in users: $WHO"
echo "CPU Usage: $CPU%"
echo "Disk Usage (/): $DISK"
echo "RAM Usage: $RAM"

This script:

  • Gathers user logins (who), CPU usage (top), disk usage (df), and RAM usage (free).
  • Displays a welcome message.
  • Presents the gathered information in a readable format.
  • Customize: Feel free to modify the welcome message, add more system details (e.g., uptime, IP address), or incorporate ASCII art.

Save the script and make it executable:

Bash
sudo chmod +x /etc/update-motd.d/10-custom-info

Need inspiration? Check out this MOTD Creator.


Step 4: Understanding How It Works

You do not need to edit the /etc/motd file itself. The pam_motd system service automatically runs all executable scripts within the /etc/update-motd.d/ directory upon user login. It concatenates the output of these scripts to form the final message.


Step 5: Test Your New MOTD

To see the result, you can either log out and log back in, or open a new SSH session to the server.

To test the output of your scripts without logging out, you can manually run them using the run-parts command:

sudo run-parts /etc/update-motd.d/

This will print the full, generated MOTD to your current terminal.


Alternative: Using neofetch for a Graphical MOTD

For a more visually appealing and detailed summary with less manual scripting, you can use a tool like neofetch.

  • Install neofetch:
# For Debian/Ubuntu 
sudo apt install neofetch 
# For Fedora/CentOS/RHEL
sudo dnf install neofetch
  • Create a MOTD script for neofetch: Disable other MOTD scripts if you wish, or simply give this one a high-priority number like 99-neofetch.
sudo nano /etc/update-motd.d/99-neofetch
  • Add the following content:
#!/bin/bash
#
# /etc/update-motd.d/99-neofetch
#
neofetch
  • Make it executable:
sudo chmod +x /etc/update-motd.d/99-neofetch

Now, upon login, the colorful neofetch output will be displayed as part of your MOTD.


Try this Killer MOTD screen

#!/bin/bash

# ==========================================
# Stunning MOTD with Advanced Information
# ==========================================

# User Information
USER=$(whoami)
FULLNAME=$(getent passwd "$USER" | cut -d: -f5 | cut -d, -f1)
LAST_LOGIN=$(last -n 1 $USER | head -n 1 | awk '{ print $5, $6, $7 }')
SHELL=$(getent passwd "$USER" | cut -d: -f7)

# System Information
HOSTNAME=$(hostname)
UPTIME=$(uptime -p)
LOAD=$(uptime | awk '{print $8,$9,$10}' | sed 's/,//g')
IP_ADDRESS=$(ip addr show | grep -oP 'inet \K[\d.]+' | head -n 1)

# Disk Usage
DISK_ROOT=$(df -h / | tail -1 | awk '{print $5}')
DISK_OVERVIEW=$(df -h | awk '{print $1, $5}' | tail -n +2)

# Recent Logins
RECENT_LOGINS=$(last -n 5 | awk '{print $1, $3, $5, $6, $7}')

# Recent Commands
LAST_COMMANDS=$(history | tail -n 5 | awk '{print $2, $3, $4, $5, $6, $7}')

# Last Shutdown/Reboot
LAST_SHUTDOWN=$(last -x shutdown | head -n 1 | awk '{ print $5, $6, $7, $8 }')

# ==========================================
# MOTD Display (with Formatting)
# ==========================================

echo "  _   _      _ _        __        __         _     "
echo " | | | | ___| | | ___   \ \      / /__  _ __| | __ "
echo " | |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ / "
echo " |  _  |  __/ | | (_) |   \ V  V / (_) | |  |   <  "
echo " |_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\_\ "
echo ""

echo -e "\e[1;34mWelcome back, $FULLNAME!\e[0m"
echo -e "\e[1;34mYou last logged in on $LAST_LOGIN from $IP_ADDRESS.\e[0m"
echo ""

echo -e "\e[1;32mSystem Information:\e[0m"
echo "   Hostname: $HOSTNAME"
echo "   Uptime: $UPTIME"
echo "   Load Average: $LOAD"
echo ""

echo -e "\e[1;33mDisk Usage:\e[0m"
echo "   Root (/): $DISK_ROOT"
for line in $DISK_OVERVIEW; do echo "   $line"; done
echo ""

echo -e "\e[1;35mRecent Logins:\e[0m"
for line in $RECENT_LOGINS; do echo "   $line"; done
echo ""

echo -e "\e[1;36mYour Recent Commands:\e[0m"
for line in $LAST_COMMANDS; do echo "   $line"; done
echo ""

if [ ! -z "$LAST_SHUTDOWN" ]; then
    echo -e "\e[1;31mLast Shutdown:\e[0m $LAST_SHUTDOWN"
    echo ""
fi

Alternatives to MOTD

One of my favorite alternatives to MOTD is a cool tool called neofetch

Installation:

Neofetch is usually available in most package managers:

  • Debian/Ubuntu/Mint: sudo apt install neofetch
  • Fedora/CentOS/RHEL: sudo dnf install neofetch
  • Arch Linux/Manjaro: sudo pacman -S neofetch
  • macOS (Homebrew): brew install neofetch

Usage:

  • Basic: Just type neofetch in your terminal, and it will display the default information.
  • Customization: Neofetch is highly customizable:
    • Configuration File: Create a ~/.config/neofetch/config.conf file to change settings like logo, colors, info displayed, etc.
    • Command-Line Options: Use flags like --ascii_distro to change the ASCII logo or --source image to use a custom image.

Example Configuration (in config.conf)

# Use image instead of ASCII logo
image_source="wall" # Use your wallpaper
# image_source="ascii" # Use custom ASCII art
# image_source="'/path/to/your/image.png'"

# Information to display
print_info="distro title kernel uptime packages shell resolution de wm theme icons terminal cpu gpu memory"

Q&A

Q1: What is Linux MOTD and why is it important?

A1: The Linux MOTD, or Message of the Day, is a customizable screen that users see when they log into a Linux system. It serves as a welcoming message and can convey important information, updates, or announcements. Customizing the MOTD allows system administrators to provide users with relevant details at login.

Q2: How can I create a custom MOTD in Linux?

A2: Creating a custom MOTD involves editing the MOTD file or using scripts in the /etc/update-motd.d/ directory. To make the login experience unique, you can add personalized messages, ASCII art, and even dynamic system information. Check out our detailed guide for step-by-step instructions.

Q3: Can I include dynamic system information in my Linux MOTD?

A3: Absolutely! You can enhance your MOTD by adding dynamic details like who is currently logged on, CPU usage, disk space, and RAM statistics. Utilizing scripts in the /etc/update-motd.d/ directory allows you to fetch and display real-time information for users.

Thats it, hope you have fun writing your own custom MOTD. Like our Linux Procedures? Check out more here.

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate ยป