The Message of the Day (MOTD) is usually a static file that prints immediately after a successful login. By default, Ubuntu 14.04 will display a dynamically generated message created via an assembly of the contents inside the directory /etc/update-motd.d. The output of this assembly can be found in /run/motd.dynamic and this is the file that PAM calls upon successful login.

/etc/update-motd.d is assembled using the run-parts utility which runs all executable files within a directory in lexical order. Begin every file with the proper hashbang and you’re good to go.

First, let’s back up our original directory. Once we’ve covered our backs, we’ll clear it out so that we can use our own scripts.

sudo cp -r /etc/update-motd.d /etc/update-motd.oops
cd /etc/update-motd.d
sudo rm -rf *

Now we can put whatever we want in here, we just have to make sure our scripts start with #! /bin/sh and their permissions are set to executable.

sudo touch 00-hello && sudo touch 10-updates
sudo chmod 755 00-hello 10-updates

For the sake of this example, we’ll put the following script checking for available updates inside 10-updates.

#! /bin/sh
if [ -x /usr/lib/update-notifier/update-motd-updates-available ];
then
sh /usr/lib/update-notifier/update-motd-updates-available
fi

Ideally, we want /etc/update-motd.d to be assembled upon login. To do this we need to make a small change to the PAM configuration inside /etc/pam.d/sshd. Find the following line and remove noupdate.

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

Alternatively, we can set it to update and changes to /run/motd.dynamic will be triggered by a login but appear on the next successful attempt. The PAM configuration for desktop logins is in /etc/pam.d/login.

You might notice timestamped information about the last login appear in your MOTD despite not being anywhere in your scripts. This is actually generated by SSH and can be configured inside /etc/ssh/sshd_config from the line PrintLastLog.

Test your files using the run-parts command on /etc/update-motd.d. If there are any errors /run/motd.dynamic will simply not be updated.

About Canadian Web Hosting

Since 1998, Canadian Web Hosting has been providing on-demand hosting solutions that include Shared HostingVirtual Private Servers (VPS), Cloud HostingDedicated Servers, and Infrastructure as a Service (IaaS) for Canadian companies of all sizes. Canadian Web Hosting is SSAE 16 Type II SOC 2 certified, ensuring that their processes and business practices are thoroughly audited against industry standards. Canadian Web Hosting guarantees a 100% network uptime, and a total money-back guarantee that backs everything they do. Customers can get help by calling 1-888-821-7888 to get 24/7 support. For more information, visit them at www.canadianwebhosting.com, or get the latest news by following them on Twitter at @cawebhosting.