How to Setup Dynamic MOTD for Debian Jessie

Introduction

Dynamically generated MOTD (Message of the Day) is actually a way to communicate important information about your server to end users, or to remind users something important.

Setting up MOTD

1. Install lsb-release

apt-get install lsb-release

2. Install figlet to enable ASCII art

apt-get install figlet

3. Create directory

mkdir /etc/update-motd.d/

4. Change to new directory

cd /etc/update-motd.d/

5. Create dynamic files

touch 00-header && touch 10-sysinfo && touch 90-footer

6. Make files executable

chmod +x /etc/update-motd.d/*

7. Remove MOTD file

rm /etc/motd

8. Symlink dynamic MOTD file

ln -s /var/run/motd /etc/motd

Created file (from number 5 procedure) should have the following content

Header (00-header)

#!/bin/sh
#
#   00-header - create the header of the MOTD
#
[ -r /etc/lsb-release ] && . /etc/lsb-release
if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
        # Fall back to using the very slow lsb_release utility
        DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi
figlet $(hostname)
printf "\n"
printf "Welcome to %s (%s).\n" "$DISTRIB_DESCRIPTION" "$(uname -r)"
printf "\n"

System Information (10-sysinfo)

#!/bin/bash
#
#  10-sysinfo - generate the system information
#
[ -r /etc/lsb-release ] && . /etc/lsb-release
if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
        # Fall back to using the very slow lsb_release utility
        DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi
figlet $(hostname)
printf "\n"
printf "Welcome to %s (%s).\n" "$DISTRIB_DESCRIPTION" "$(uname -r)"
printf "\n"
root@theSysad:/etc/update-motd.d# cat 10-sysinfo | egrep -v "(^#.*|^$)"
date=`date`
load=`cat /proc/loadavg | awk '{print $1}'`
root_usage=`df -h / | awk '/\// {print $(NF-1)}'`
memory_usage=`free -m | awk '/Mem:/ { total=$2 } /buffers\/cache/ { used=$3 } END { printf("%3.1f%%", used/total*100)}'`
swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", "exit !$2;$3/$2*100") }'`
users=`users | wc -w`
time=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }'`
processes=`ps aux | wc -l`
ip=`ifconfig $(route | grep default | awk '{ print $8 }') | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
echo "System information as of: $date"
echo
printf "System load:\t%s\tIP Address:\t%s\n" $load $ip
printf "Memory usage:\t%s\tSystem uptime:\t%s\n" $memory_usage "$time"
printf "Usage on /:\t%s\tSwap usage:\t%s\n" $root_usage $swap_usage
printf "Local Users:\t%s\tProcesses:\t%s\n" $users $processes
echo

Footer (90-footer)

#!/bin/sh
#
# 90-footer - write the admin's footer to the MOTD
#

[ -f /etc/motd.tail ] && cat /etc/motd.tail || true

Test and see the result. You should see something like:

login as: root
Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Verification code:
 _   _          ____                      _
| |_| |__   ___/ ___| _   _ ___  __ _  __| |
| __| '_ \ / _ \___ \| | | / __|/ _` |/ _` |
| |_| | | |  __/___) | |_| \__ \ (_| | (_| |
 \__|_| |_|\___|____/ \__, |___/\__,_|\__,_|
                      |___/

Welcome to Debian GNU/Linux 8.4 (jessie) (3.16.0-4-amd64).

System information as of: Fri May  6 09:37:33 SGT 2016

System load:    0.00    IP Address:     10.2.1.138
Memory usage:   55.0%   System uptime:  17:21 hours
Usage on /:     62%     Swap usage:     0.0%
Local Users:    5       Processes:      143

Linux theSysad 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-1 (2016-03-06) x86_64
Last login: Fri May  6 09:10:21 2016 from sg118ws.local
root@theSysad:~#