Put a Talking Cow in Your Linux Message of the Day

11517

There are a lot of cool old Linux commands that we don’t see anymore because they’re buried under our flashy graphical desktops. But they’re still there, they’re still fun, and best of all they’re configured with nice easy text files. MOTD, or Message of the Day, has evolved from a simple text file to an entire scripted framework that can display ANSI art, static text messages, and display dynamic system information.

Message of the Day

Message of the Day, or motd, displays a message on console logins. You can see this in action by pressing Ctrl+Alt+F2 to get to a console, and then login. Or open an SSH session to another Linux PC. This is how it looks on Linux Mint:

Welcome to Linux Mint 13 Maya (GNU/Linux 3.2.0-23-generic x86_64)
Welcome to Linux Mint
 * Documentation:  http://www.linuxmint.com
Last login: Thu Oct  3 09:25:53 2013

But that is boring, and I want it to look like this:

/ BOFH excuse #365:           
|                             |
| parallel processors running |
 perpendicular today         /
 -----------------------------
           ^__^
           (oo)_______
            (__)       )/
                ||----w |
                ||     ||
   _____                ____     _                __                   __
  / ___/____ ___  __   / __/____(_)__  ____  ____/ /  ____ _____  ____/ /
  __ / __ `/ / / /  / /_/ ___/ / _ / __ / __  /  / __ `/ __ / __  / 
 ___/ / /_/ / /_/ /  / __/ /  / /  __/ / / / /_/ /  / /_/ / / / / /_/ /  
/____/__,_/__, /  /_/ /_/  /_/___/_/ /_/__,_/   __,_/_/ /_/__,_/   
           /____/                                                        
              __            __
  ___  ____  / /____  _____/ /
 / _ / __ / __/ _ / ___/ / 
/  __/ / / / /_/  __/ /  /_/  
___/_/ /_/__/___/_/  (_)   

How you do these depends on your Linux distro. In the olden days the message came from /etc/motd, and most distros still use this. So all you do to change the message is edit /etc/motd, in plain text. Easy peasey and fun. But this doesn’t let you run fun commands, like cowsay and fortune. There is a way around this, which I shall return to in a moment.

MOTD on Debian and Ubuntu

Debian and Ubuntu use a dynamic scripting framework, update-motd. It uses a batch of scripts in /etc/update-motd that are executed by the pam_motd module when users login, and information from the various scripts is assembled in /var/run/motd/etc/motd is symlinked to /var/run/motd, so anything you write in it will be overwritten. So what if you want to put your own inspirational message in MOTD on Debian and Ubuntu? /etc/update-motd.d/99-footer is reserved for this, but it’s a shell script so you can’t just add a text message, but must use shell commands. Here is a simple example that outputs two lines of text separated by linebreaks:

printf "n wakka wakka wakka n n wakka n"

Which looks like this:

Welcome to Linux Mint 13 Maya (GNU/Linux 3.2.0-23-generic x86_64)
Welcome to Linux Mint
 * Documentation:  http://www.linuxmint.com
 
 wakka wakka wakka 
 
 wakka 
 
Last login: Wed Sep 11 19:53:56 2013

You can read the files in /etc/update-motd to see which scripts the other lines come from, and if you want to find all the scripts that touch MOTD try running sudo grep -ir motd /etc. Or read man update-motd.

Jazzing Up Your MOTD With Fortune and Cowsay

Wouldn’t you prefer to have a talking cow deliver a different fortune when you login?

 ___________________________________________________________
/ Q:      What's tiny and yellow and very, very, dangerous? 
 A:      A canary with the super-user password.            /
 -----------------------------------------------------------
           ^__^
           (oo)_______
            (__)       )/
                ||----w |
                ||     ||

On Debian and Ubuntu, install fortune-mod and cowsay. There are many different fortune databases you can install, which you can find with apt-cache search fortune. They’re all plain text files so you can easily edit them and add your own messages, if you feel creative. Then add this line to /etc/update-motd.d/99-footer:

exec /usr/games/fortune | /usr/games/cowsay -n

Now you’ll get a fortune wrapped in cowsay at every login. You can test it on the command-line like this:

$ /usr/games/fortune | /usr/games/cowsay -n
 ___________________________________________________
/ Q:      Why did the chicken cross the road?       
| A:      To see his friend Gregory peck.           |
|                                                   |
| Q:      Why did the chicken cross the playground? |
 A:      To get to the other slide.                /
 ---------------------------------------------------
           ^__^
           (oo)_______
            (__)       )/
                ||----w |
                ||     ||

You can even specify a specific fortune database, such as /usr/games/fortune bofh-excuses | /usr/games/cowsay -n

Do the same for figlet: just pop a line into /etc/update-motd.d/99-footer, like this:

exec figlet -f digital Welcome and be assimilated

Which looks like this:

+-+-+-+-+-+-+-+ +-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+-+-+-+
|W|e|l|c|o|m|e| |a|n|d| |b|e| |a|s|s|i|m|i|l|a|t|e|d|
+-+-+-+-+-+-+-+ +-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+-+-+-+

Other Distros

What about distros with the old-fashioned static MOTD? No worries, because you can make use of the global login profile to run scripted login messages. Create a file named/etc/profiles.d/motd.sh, and enter your commands in it like this:

/usr/games/fortune | /usr/games/cowsay -n
figlet -f slant Say friend and enter!
echo "Wipe your feet and mind your manners"

Yes, it is that easy, and your console login will look something like this:

 _____________________________________________
/ Today's weirdness is tomorrow's reason why. 
                 -- Hunter S. Thompson       /
 ---------------------------------------------
           ^__^
           (oo)_______
            (__)       )/
                ||----w |
                ||     ||
   _____                ____     _                __                   __
  / ___/____ ___  __   / __/____(_)__  ____  ____/ /  ____ _____  ____/ /
  __ / __ `/ / / /  / /_/ ___/ / _ / __ / __  /  / __ `/ __ / __  / 
 ___/ / /_/ / /_/ /  / __/ /  / /  __/ / / / /_/ /  / /_/ / / / / /_/ /  
/____/__,_/__, /  /_/ /_/  /_/___/_/ /_/__,_/   __,_/_/ /_/__,_/   
           /____/                                                        
              __            __
  ___  ____  / /____  _____/ /
 / _ / __ / __/ _ / ___/ / 
/  __/ / / / /_/  __/ /  /_/  
___/_/ /_/__/___/_/  (_)   
                           
Wipe your feet and mind your manners

You can use any shell commands and pretty it up with ANSI colors. This example displays current system information in colors:

HOSTNAME=`uname -n`
KERNEL=`uname -r`
CPU=`uname -p`
ARCH=`uname -m`
# The different colours as variables
W="33[01;37m"
B="33[01;34m"
R="33[01;31m" 
X="33[00;37m"
echo "$R#===================================================#" 
echo  "       $W Welcome $B $USER $W to $B $HOSTNAME  " 
echo  "       $R ARCH   $W= $ARCH                     " 
echo  "       $R KERNEL $W= $KERNEL                   " 
echo  "       $R CPU    $W= $CPU                      " 
echo  "$R#==================================================#"

Which displays like this:

#==================================================#
        Welcome  carla  to  studio  
        ARCH   = x86_64                     
        KERNEL = 3.8.0-31-generic                   
        CPU    = x86_64                      
#==================================================#

Another advantage to doing it this way is you can quickly test your changes by running the script directly:

$ sh /etc/profiles.d/motd.sh

Consult the Bash reference manual and the man pages of the various commands to learn more ways to customize your MOTD, and check your own distro documentation just in case they’ve made any funny tweaks to MOTD.