22

I edited the default /etc/update-motd.d/00-header, adding some color to make it easier to read:

printf "Welcome to \e[1;34m%s\e[0m \e[2m(%s %s %s)\e[0m\n" "$DISTRIB_DESCRIPTION" "$(uname -o)" "$(uname -r)" "$(uname -m)"

I can run that line of code just fine in a terminal, but when I edit 00-header with this information, I get the escape codes printed out in plaintext:

Welcome to \e[1;36mUbuntu 13.10\e[0m \e[2m(GNU/Linux 3.11.0-23-generic i686)\e[0m

How can I add a splash of color to the message of the day?

Christopher
  • 15,611
  • 7
  • 51
  • 64
IQAndreas
  • 10,145
  • 21
  • 59
  • 79
  • How did you edit the file, especially the non-printable chars? – Volker Siegel Jun 25 '14 at 04:39
  • In `pico` I replaced the existing line of code with the first line o code that I included in my example. The problem is the MOTD parser then treated the `\e` and subsequent characters as plain-text instead of as special characters. – IQAndreas Jun 25 '14 at 04:57
  • I see from the copyright headeer of the linked file you are on Ubuntu indeed, so I add the respective tag, as it's relevant for answering (regarding shell used, see my answer). – Volker Siegel Jun 25 '14 at 05:49

3 Answers3

13

Assuming you are on Ubuntu - which uses dash to run system scripts:

That file, /etc/update-motd.d/00-header, is executed by /bin/dash, (not /bin/bash,) which is pretty minimalistic (and fast) -
it seems to not support the "\e" in this place - use "\033" instead.

It is different in when to expand escape codes.

Volker Siegel
  • 16,983
  • 5
  • 52
  • 79
10

On Debian/Ubuntu the motd is configured in /etc/pam.d/sshd:

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

which means that upon successful login the system will run something like:

cat /run/motd.dynamic
if [[ -f /etc/motd ]]; then cat /etc/motd; fi

where /etc/motd is the static part (only printed, not sourced).

Debian 9 / Ubuntu 16.04:

For generating the dynamic part run-parts is used for /etc/update-motd.d directory:

/usr/bin/env -i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin run-parts --lsbsysinit /etc/update-motd.d

For slightly more readable code you can use tput setaf {color number}. Note that to preserve colors we have to set TERM variable.

#!/bin/sh
export TERM=xterm-256color

read one five fifteen rest < /proc/loadavg
echo "$(tput setaf 2)
Kernel: `uname -v | awk -v OFS=' ' '{print $4, $5}'`
$(tput setaf 4)Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)
$(tput setaf 5)
 ______________
< Hello World! >
 --------------
        \\   ^__^
         \\  (oo)\\_______
            (__)\\       )\\\/\\
                ||----w |
                ||     ||

$(tput sgr0)"

save the file as e.g. /etc/update-motd.d/10-uname

and make sure it's executable:

chmod +x /etc/update-motd.d/10-uname

Basic colors are numbered:

  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White

The code above generate: bash colored motd

Depending on your taste you can produce more colorful output using lolcat or headings from figlet. Generated output uses standard bash syntax:

^[(B^[[m
^[[32m
Kernel: 4.9.65-3+deb9u2 (2018-01-04)
^[[34mLoad Averages......: 0.04, 0.05, 0.05 (1, 5, 15 min)
^[[35m
 ______________
< Hello World! >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

^[(B^[[m

Debian 8 / Ubuntu 14.04

The dynamic part is updated using /etc/init.d/motd start that executes following:

uname -snrvm > /var/run/motd.dynamic
Tombart
  • 2,630
  • 5
  • 26
  • 39
0

Insert an ANSI <ESC> escape character before the color code. If you are using Vim, it would be pressing Ctrl+K ESC or Ctrl+v+[ in edit mode.

^[

Then write the color code you want. For example, let it be red foreground [31m

^[[31mHello! Glad to see you again^[[0m

The starting ^[ should be an indicator that there is an escape character, and [31m the real characters typed by hand.

Alex
  • 1
  • 2