Translation(s): English - Italiano


/etc/motd in Debian

Debian has a peculiar way of handling /etc/motd. The motd is updated at every reboot, in a boot script (/etc/init.d/bootmisc.sh in squeeze and below, /etc/init.d/bootlogs in wheezy and above), which basically runs the following:

uname -snrvm > /var/run/motd
[ -f /etc/motd.tail ] && cat /etc/motd.tail >> /var/run/motd

Since /etc/motd is a symlink to /var/run/motd in Debian, this works.

How to update your /etc/motd

Since /etc/motd basically gets overwritten at every reboot, you need to instead update /etc/motd.tail and either reboot (!!) or also edit /etc/motd.tail or run the above commands. There is a bug report (437176) to provide an easier command to allow you to update only /etc/motd.tail.

How to script updates to your /etc/motd

A little less known thing about /etc/motd in Debian and Ubuntu is that pam will actually update your /etc/motd based simply on the scripts in /etc/update-motd.d. So to replicate the above scripts, you could do:

mkdir /etc/update-motd.d
cat > /etc/update-motd.d/10uname <<EOF
#! /bin/sh
uname -snrvm > /var/run/motd
EOF
cat > /etc/update-motd.d/20tail <<EOF
#! /bin/sh
[ -f /etc/motd.tail ] && cat /etc/motd.tail >> /var/run/motd
EOF
chmod a+x /etc/update-motd.d/*

Then you could add other scripts to your /etc/update-motd.d directory to add stuff to your /etc/motd. See the manpage for more information.

Note that this technique is using a patch that is shipped only with Debian and Ubuntu and not factored into upstream PAM. Furthermore, this code has had security issues in the past (CVE-2010-0832, this bug).

How to keep your /etc/motd from being overwritten

rm /etc/motd
cat > /etc/motd <<EOF
This is my message of the day!
EOF

This way, /etc/motd is not a symlink to the updated one anymore and will therefore always stay the same. The downside to this is that you will not see the latest kernel version in the motd.

Old school "don't touch my motd" approach

Similar to the above, this approach also makes sure dynamic information can be displayed...

cat > /etc/profile.d/uname <<EOF
#!/bin/sh
uname -snrvm
EOF
rm /etc/motd
cat > /etc/motd <<EOF
Known issues
============

 * the kerkuffle is known to not restart properly on reboot, to restart use the command
   service kerkuffle restart

Who's responsible for /etc/motd?

Here's a list of packages which do that work:

base-files: /usr/share/base-files/motd
base-files: /usr/share/base-files/motd.md5sums
manpages: /usr/share/man/man5/motd.5.gz
manpages: /usr/share/man/man5/motd.tail.5.gz
libpam-modules: /lib/x86_64-linux-gnu/security/pam_motd.so
initscripts: /etc/init.d/bootlogs

What others do

Ubuntu

Since Ubuntu Intrepid Ibex (2008.10), Ubuntu has had a /etc/update-motd.d/ directory, from which scripts are ran to update the /etc/motd file. This is now factored into the pam_motd module, which updates the /var/run/motd file, using this C code:

    if (do_update && (stat("/etc/update-motd.d", &st) == 0)
        && S_ISDIR(st.st_mode))
    {
       mode_t old_mask = umask(0022);
       if (!system("/usr/bin/env -i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ru
n-parts --lsbsysinit /etc/update-motd.d > /var/run/motd.new"))
           rename("/var/run/motd.new", "/var/run/motd");
       umask(old_mask);
     }

For a short while, this was instead done by the update-motd pacakge every 10 minutes.

By default Ubuntu ships with a set of scripts that add the number of packages to be update (for example).

Gentoo

Gentoo doesn't display the motd file by default, but this can be enabled in login.defs, see this wiki page.

Arch, CentOS, and others

According to this this wiki page, Arch doesn't seem to do anything special to /etc/motd other than display it on boot.

FreeBSD

In FreeBSD, the motd file is also generated through a boot script, which will preserve modifications by changing only the line that matches (loosely) the uname that gets updated on reboots. The magic lines:

T=`mktemp -t motd`
uname -v | sed -e 's,^\([^#]*\) #\(.* [1-2][0-9][0-9][0-9]\).*/\([^\]*\) $,\1 (\3) #\2,' > ${T}
awk '{if (NR == 1) {if ($1 == "FreeBSD") {next} else {print "\n"$0}} else {print}}' < /etc/motd >> ${T}

References


CategoryCommandLineInterface