How to generate dynamic MOTD messages in Ubuntu

To generate a dynamic MOTD (Message Of The Day) file in Ubuntu, you can use a combination of scripts and configuration files to display useful system information such as server uptime, system load, memory usage, and more.

Here are the general steps to create a dynamic motd file:

  1. Create a motd file in the /etc/update-motd.d/ directory
  2. Make the motd file executable
  3. Update /etc/pam file to point to the motd file

We will go through each of these steps in detail below.

Create a motd file

The first step is to create a new motd file. You can name it anything you like, but it should start with a number to make it easier to determine its priority in the execution order. We will call ours 99-server-info and place it in the /etc/update-motd.d/ directory.

sudo touch /etc/update-motd.d/99-server-info

Make the motd file executable

Next, we need to make the motd file executable so that it can be run by the update-motd script.

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

Configure the motd file

Now that we have created the motd file, we need to add some content to it. We will use the following script to display the server uptime, load average, and memory usage.

#!/bin/bash
# /etc/update-motd.d/99-server-info


# Define colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

# Display server uptime
echo "Server uptime: $(uptime -p)"

# Display load average
echo "Load average: $(uptime | awk '{print $10}')"


# Display free memory
free_output=$(free -h | awk 'NR==2')
used_memory_percentage=$(echo $free_output | awk '{printf "%.0f\n", $4/$3}' | sed 's/[^0-9.%]*//g')
if [ $used_memory_percentage -gt 80 ]; then
    echo -e "Memory usage: ${RED}${used_memory_percentage}%${NC}"
elif [ $used_memory_percentage -gt 50 ]; then
    echo -e "Memory usage: ${YELLOW}${used_memory_percentage}%${NC}"
else
    echo -e "Memory usage: ${GREEN}${used_memory_percentage}%${NC}"
fi

# Display disk usage
disk_output=$(df -h | awk 'NR==2')
disk_usage=$(echo $disk_output | awk '{print $5}')
disk_usage_percentage=$(echo $disk_output | awk '{print $5}' | sed 's/[^0-9]*//g')

if [ $disk_usage_percentage -gt 80 ]; then

    echo -e "Disk usage: ${RED}${disk_usage}${NC}"

elif [ $disk_usage_percentage -gt 50 ]; then
    echo -e "Disk usage: ${YELLOW}${disk_usage}${NC}"

else
    echo -e "Disk usage: ${GREEN}${disk_usage}${NC}"
fi

The script will display the server uptime, load average, and memory usage. It will also display the disk usage in a different color depending on the percentage of disk space used.

Update the PAM configuration

Modify the /etc/pam.d/sshd file to enable the dynamic motd. Add or update the following line:

session   optional    pam_motd.so motd=/run/motd.dynamic

Now, when you connect to your Ubuntu server via SSH, you will see the dynamic motd displaying the information you configured. The information will be updated every time you log in, giving you real-time insights into your system’s performance.

Conclusion

In this tutorial, we showed you how to create a dynamic motd file in Ubuntu. We used a combination of scripts and configuration files to display useful system information such as server uptime, system load, memory usage, and more.