How To Update The Message Of The Day (MOTD) On Ubuntu 18.04

How To Update The Message Of The Day (MOTD) On Ubuntu 18.04

I like to have a banner display at login to my servers when I use Secure Shell (SSH) to access them. Once upon a time, this was done by putting a static message in a static file called /etc/motd.

Since 2008, in Ubuntu, this system has changed allowing administrators to easily call dynamic content and have it added to the motd.

There's a directory called /etc/update-motd.d/ that holds shell scripts that are executed pretty much in alphabetical order with the resulting output displayed to the logging in user.

/etc/update-motd.d Contents

The leading two digit number (00 to 99) will determine where the file is alphabetically, and thereby where it will be in the order of items displayed. Lower numbers like 00 or 10 will be displayed first but will scroll up as subsequent files are processed and their output is displayed.

Processing these files results in seeing something similar to the following upon a successful SSH login:

Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-45-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sun Feb  3 05:17:10 UTC 2019

  System load:  0.0               Processes:           94
  Usage of /:   6.1% of 48.29GB   Users logged in:     1
  Memory usage: 27%               IP address for eth0: 10.0.0.1
  Swap usage:   0%

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

2 packages can be updated.
0 updates are security updates.

Last login: Sun Feb  3 04:12:26 2019 from 10.1.1.1

There's a lot of useful information there, but no banner saying only authorized connections are permitted.

Each section of the output comes from an individual file in /etc/update-motd.d

To add my warning to logging in users, I've added a file called 96-access-warning. I want the login warning displayed near the end of the motd script output so it is visible, but I want a few things like Last login date and time displayed after.

There was a file 95-hwe-eol, and a file 97-overlayroot, so I made one in between called 96-access-warning.

sudo vim 96-access-warning while in the /etc/update-motd.d/ directory.

The file had the following content:

#!/bin/sh
echo
echo "-------------------------------------------------------------------------------
    ___         __  __               _                __
   /   | __  __/ /_/ /_  ____  _____(_)___  ___  ____/ /
  / /| |/ / / / __/ __ \/ __ \/ ___/ /_  / / _ \/ __  /
 / ___ / /_/ / /_/ / / / /_/ / /  / / / /_/  __/ /_/ /
/_/ _||\__,_/\__/_/ /_/\____/_/  /_/ /___/\___/\__,_/
   /   | _____________  __________
  / /| |/ ___/ ___/ _ \/ ___/ ___/
 / ___ / /__/ /__/  __(__  |__  )
/_/__||\___/\__||\___/_||_/____/
  / __ \____  / /_  __/ /
 / / / / __ \/ / / / / /
/ /_/ / / / / / /_/ /_/
\____/_/ /_/_/\__, (_)
             /____/

ALERT! You are logging into a secured device! Your IP Address, Login Time, and
Username have been noted and have been sent to the server administrator!

 This service is restricted to authorized users only. All activities on this
system are logged.

 Unauthorized access will be fully investigated and reported to the
appropriate law enforcement agencies.

--------------------------------------------------------------------------------"

Next, you have to set the file to be executable.

sudo chmod +x 96-access-warning

Now, users logging in will see output similar to the following:

Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-45-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sun Feb  3 05:34:53 UTC 2019

  System load:  0.0               Processes:           96
  Usage of /:   5.9% of 24.06GB   Users logged in:     1
  Memory usage: 16%               IP address for eth0: 10.0.0.2
  Swap usage:   0%

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


-------------------------------------------------------------------------------
    ___         __  __               _                __
   /   | __  __/ /_/ /_  ____  _____(_)___  ___  ____/ /
  / /| |/ / / / __/ __ \/ __ \/ ___/ /_  / / _ \/ __  /
 / ___ / /_/ / /_/ / / / /_/ / /  / / / /_/  __/ /_/ /
/_/ _||\__,_/\__/_/ /_/\____/_/  /_/ /___/\___/\__,_/
   /   | _____________  __________
  / /| |/ ___/ ___/ _ \/ ___/ ___/
 / ___ / /__/ /__/  __(__  |__  )
/_/__||\___/\__||\___/_||_/____/
  / __ \____  / /_  __/ /
 / / / / __ \/ / / / / /
/ /_/ / / / / / /_/ /_/
\____/_/ /_/_/\__, (_)
             /____/

ALERT! You are logging into a secured device! Your IP Address, Login Time, and
Username have been noted and have been sent to the server administrator!

 This service is restricted to authorized users only. All activities on this
system are logged.

 Unauthorized access will be fully investigated and reported to the
appropriate law enforcement agencies.

--------------------------------------------------------------------------------

Last login: Sun Feb  3 05:27:45 2019 from 10.1.1.1 

There you have it! Want something else displayed at login? Add an appropriate shell script to /etc/update-motd.d with a number that will place it in the order you'd like it displayed.

If you like the text based artwork for Authorized Access Only!, you can generate your own quickly for free. It's called ascii art, and there are many sites that will do this for you. I used patorjk.com's text to ascii art generator.

I've created a super-small github repo for this.

Enjoy!

/* Adding copy button to code snippet in Ghost https://forum.ghost.org/t/how-do-i-add-a-copy-button-to-a-code-snippet/34586 */