Linux · Server

Add to and change Ubuntu’s MOTD


Ubuntu’s Message Of The Day or MOTD is the name given to the welcome screen users see when they login to a Ubuntu server using a remote terminal.

motd default

On the surface the output looks like it is sourced from a basic text file but it is a collection of modular shell scripts being executed. In this entry I explain how to add, edit and remove parts of the MOTD plus also apply colour text and ASCII art. It will only cover Ubuntu 14.04+.

cleaner output

As I mentioned the MOTD is comprised of a collection of shell scripts that run in a sequential order. These scripts can be found at /etc/update-motd.d/ so let’s take a look inside.

ll /etc/update-motd.d/

The filenames are named in NN-description where NN is the ascending start order with 00-header being the first script to be run.

ll first time

A look into one of the files shows they are indeed shell scripts.

cat /etc/update-motd.d/00-header

cat 00-header

You can also run the script from terminal.

cd /etc/update-motd.d/
./00-header

run 00-header

Disable MOTD scripts

Turning off one or more scripts is simple as removing the execute permissions bit from the target. Here we will turn off the script that posts the Documentation link.

cd /etc/update-motd.d/
sudo chmod -x 10-help-text

Use the run-parts command to see your changes.

run-parts /etc/update-motd.d

chmod -x 10-help-text

And if you wish to return the script to the MOTD re-add its execute bit.

sudo chmod +x 10-help-text
run-parts /etc/update-motd.d

chmod +x 10-help-text

Edit existing MOTD scripts

I don’t recommend editing the system installed scripts directly. Rather I think it’s better to disable, duplicate and edit the copy. This way you can always revert to the original script if you need to.

Here let’s change the 00-header to simplify its version output. But first we need to disable and copy the system original.

cd /etc/update-motd.d/
sudo cp 00-header 01-header-custom
sudo chmod -x 00-header

Now edit our custom header to make some modifications.

sudo nano 01-header-custom

cp 00-header 01-header-custom

Replace the line Welcome to line with something simpler.

printf "Welcome to %s (%s %s %s)\n" "$DISTRIB_DESCRIPTION" "$(uname -o)" "$(uname -r)" "$(uname -m)"
printf "Welcome to %s\n" "$DISTRIB_DESCRIPTION"

printf welcome

Save and exit.

run-parts /etc/update-motd.d

welcome to mod

If you want to revert your changes back to the default, overwrite it with the system original.

cp 00-header 01-header-custom
run-parts /etc/update-motd.d

Add new MOTD scripts.

Adding new scripts to the MOTD is a simple process. Let’s start with a hello world example. First we will create a script with the name 30-hello-world. It will start after the 00-header and 10-help-text.

cd /etc/update-motd.d/
sudo nano 30-hello-world

Add the following to the file. We use the printf command to handle text output while \n tells the shell to print a newline.

#!/bin/sh
printf "\nHello world.\n"

nano 30-hello-world

Save and exit. Now add the execute permission and test the file.

sudo chmod +x 30-hello-world
run-parts /etc/update-motd.d

hello world output

Colour your text

For colour support I use the tput command that is included in a default Ubuntu install. It not only allows you to colour text but also apply styles, centering, cursor movement and clear the screen.

For our purposes of colouring text tput supports the adjustment of both foreground and background colours using ANSI escape codes.

Set the foreground.

tput setaf x

Set the background.

tput setab x

Resets the colours to the terminal defaults.

tput sgr0

tput x colour codes.
0 black
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 white

So tput setaf 7 sets the foreground to white and then tput setab 2 would set the and the background to green. tput setaf 4 would set the foreground to blue.

Tput relies on the $TERM user environment variable to determine the terminal type. Unfortunately this variable is not given until the user is logged in which is after the MOTD has been displayed. So whenever we use tput we need to force it to use xterm using the -T argument.

sudo nano 30-hello-world

Append the following commands to the file, save and test.

echo "$(tput -T xterm setaf 1)Hello world$(tput -T xterm sgr0) in red."

nano 30-hello-world edit 2

Now if you run run-parts /etc/update-motd.d you’ll see the words “Hello world” in the colour red and then the following text “in red” in the terminal default colours.

hello world in red

Now edit the file again and append the following, save and test.

echo "$(tput -T xterm setaf 4)$(tput -T xterm setab 7)White background and blue foreground.$(tput -T xterm sgr0)"
echo "$(tput -T xterm rev)Reverse normal text.$(tput -T xterm sgr0)"

nano 30-hello-world edit 3

run-parts /etc/update-motd.d

hello world multiple output

To learn more on what you can do using the tput command I’d recommend the following guides.

Print a text file

Outputting a text file is a simple process and there are a few ways of doing it. I generally save the text file into the /etc/update-motd.d and use the cat command from within a script to display it onscreen.

sudo nano /etc/update-motd.d/tux.asc

Copy, paste and save this Tux logo that I sourced from Chris.com.

              a8888b.   
             d888888b. 
             8P"YP"Y88 
             8|o||o|88 
             8'    .88 
             8`._.' Y8. 
            d/      `8b. 
           dP   .    Y8b. 
          d8:'  "  `::88b 
         d8"         'Y88b 
        :8P    '      :888 
         8a.   :     _a88P 
       ._/"Yaa_:   .| 88P| 
  jgs  \    YP"    `| 8P  `.
  a:f  /     \.___.d|    .' 
       `--..__)8888P`._.' 

Now create a new MOTD script.

sudo nano /etc/update-motd.d/20-display-logo

Edit it and add the following script.

#!/bin/sh
printf "\n$(cat /etc/update-motd.d/tux.asc)\n"

Finally apply the execute bit and test out your changes.

sudo chmod +x /etc/update-motd.d/20-display-logo
run-parts /etc/update-motd.d

messy output

Now feel free to use what you have learnt to clean-up the modifications and produce a nicer login.

cleaner output

Perfect and that is it for this guide, I hope it was useful.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s