A Message of the Day (MOTD) is a piece of text that is displayed when you log in to your computer. If you’ve ever SSHed into a Raspberry Pi or a VPS, you’ll have noticed some text being displayed right after you log in – a copyright notice, a disclaimer, the amount of unread mail you have, etc.

By default, a MOTD is static – the login program just prints out the contents of /etc/motd. That gets boring pretty soon, though. So I wrote a dynamic MOTD for myself – its contents change on every login. It’s created by a script I wrote called motdmaker and stuck in /usr/bin/.

Here’s what it looks like:

TTY screenshot showing my MOTD

And here’s the code for motdmaker (GitLab snippet featured in this post; updated GitLab project).

I spent a good half hour on this, so let me explain a couple of the less-immediately-obvious lines.

  1. /opt/monokai contains escape codes that set my TTY colour scheme. I snagged it from pywal – it creates a file for your preferred colour scheme, ~/.cache/wal/colors-tty.sh, and you can source this to set your TTY colours.

  2. Toilet (lol):

toilet -k -f slant -F rainbow "$(hostname)" | sed 's/^.*$/\t  &/'
`toilet` makes my hostname look like that. Poking around in `man toilet` yields

a few interesting options, so make sure to play around with it. (Pun intended!) sed is the venerable stream editor, and here it prefixes each line from toilet’s output with a tab and two spaces.

  1. Disk usage:
echo "    Disk space remaining:\t" `df -h | grep sda2 | awk '{print $4}'` " ("`df -h | grep sda2 | awk '{print $5}'`" used)"
`df` shows you your disk usage. Typically, its output (with the human-readable

flag -h) is something like:

Filesystem      Size  Used Avail Use% Mounted on
dev             3.9G     0  3.9G   0% /dev
run             3.9G  936K  3.9G   1% /run
/dev/sda2       916G  358G  511G  42% /
tmpfs           3.9G   73M  3.8G   2% /dev/shm
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           3.9G  2.6M  3.9G   1% /tmp
/dev/sda1       511M  6.3M  505M   2% /efi
tmpfs           784M   16K  784M   1% /run/user/1000
tmpfs           784M     0  784M   0% /run/user/1001

My primary disk is /dev/sda2, so I’m grepping that (extracting the line that contains “sda2”), and getting the fourth and fifth words using awk, to print the amount of free space and the percentage occupied.

  1. Network info:
echo "\n    Connected to:\t\t" `iwconfig wlp2s0 | head -n 1 | sed 's/"/ /g' | awk '{print $5}'` >> $motd

wlp2s0 is my wireless interface; this prints the SSID of the network I’m connected to. head -n 1 gets the first line. sed 's/"/ /g' gets rid of the quotation marks. awk '{print $5}' gets the fifth word.

  1. Quote:
shuf -n 1 /opt/quotes.md | sed 's/ --- /\n\t-- /g' | fmt -w 70 -u | sed 's/^.*$/    &/'

I’m particularly proud of this one. shuf -n 1 gets one random line from /opt/quotes.md. That file has a few dozen quotes, formatted like so:

A rock pile ceases to be a rock pile the moment a single man contemplates it, bearing within him the image of a cathedral. --- Antoine de Saint Exupéry
Without courage we cannot practice any other virtue with consistency. We can't be kind, true, merciful, generous, or honest. --- Maya Angelou

First, sed splits the line at the em-dash, and puts the attribution on a new line, prefixed by a tab and an en-dash. fmt formats the quote to 70 characters a line, splitting the lines at spaces, and applies uniform spacing (one space after words, two after periods). Finally, we use sed to prefix each line with four spaces.

  1. colouredline contains a line that I passed through lolcat and captured with script. It’s just more fun than a regular old linebreak. To get a coloured line of your own, run:
$ script colouredline
$ echo "---------------------------------" | lolcat
$ # make the line as long/short as you want
$ # hit Ctrl-D to exit script
$ vim colouredline # remove a couple of lines at the top & bottom
$ cat colouredline
$ # voilà!

Neat, isn’t it?

Usually, a MOTD is read from /etc/motd. However, I had problems with permissions when I tried to edit motdmaker to write to it at boot, so I’m using /opt/motd instead. I call motdmaker at the end of my /etc/profile like so:

if [[ ! $TMUX ]]; then
	/usr/bin/motdmaker
	cat /opt/motd
fi

With the not-tmux flag, my MOTD is printed when logging in at the TTY and through SSH. And it works for every user, as all the files I’m using here are in /opt/ and have their permissions set to 777 (chmod 777 /opt/file).

So to replicate this set-up, you’ll have to:

  • create /usr/bin/motdmaker and make it an executable (chmod +x /usr/bin/motdmaker)
  • play around with toilet, figlet, lolcat, and pywal
  • modify the lines for disk space & SSID to match your system setup
  • make a coloured line and store it in /opt/
  • compile a quote file for yourself in /opt/ (or just use fortune)
  • modify the permissions for all these files appropriately
  • modify your /etc/profile.

I’m enjoying my new MOTD very much! If you try this out, send me a picture of what you come up with – I’d love to see.


References: