For those of you not in “the know,” the /etc/motd or the MOTD stands for “Message of the Day.” The file contains text that is displayed whenever logging in via SSH to your machine. If you found this article via a search engine, you probably already know that.
To the best of my knowledge, back in Ubuntu 8.04 (Hardy Heron), the MOTD was static and involved updating the /etc/motd or /etc/motd.tail file. When switching to 9.04 (Jaunty Jackalope) I made no changes to the MOTD and therefore noticed no difference. When attempting to edit the MOTD in 10.04 (Lucid Lynx), it appears that the process has changed so that the MOTD is not read from one (or two) static text files that may or may not need periodic updating. Since 8.10 (Intrepid Ibex) it appears that the MOTD can be updated dynamically by using a number of scripts. I’m always the last to know!
Let’s jump right in and I’ll explain as we go. For some of the commands, you will probably have to precede the command with sudo. To start, you can display the contents of the /etc/motd file to see what is currently in there. It might help when we look at the scripts later that actually generate the text in this file. Using the less command will allow you to scroll through the text if the file is long, but it probably isn’t so we’ll use the cat command instead.
user@machine:~# cat /etc/motd Linux machine 184.108.40.206-rscloud #8 SMP Mon Sep 20 15:54:33 UTC 2010 x86_64 GNU/Linux Ubuntu 10.04 LTS Welcome to Ubuntu! * Documentation: https://help.ubuntu.com/
You will probably see information about the kernel, last login, Ubuntu version, etc. Now let’s look at the scripts that created the MOTD.
user@machine:~# ls -lah /etc/update-motd.d/ total 20K drwxr-xr-x 2 root root 4.0K Apr 22 2010 . drwxr-xr-x 65 root root 4.0K Jun 17 15:31 .. -rwxr-xr-x 1 root root 57 Apr 23 2010 00-header -rwxr-xr-x 1 root root 248 Apr 23 2010 10-help-text -rwxr-xr-x 1 root root 261 Apr 23 2010 99-footer
All the scripts that create the MOTD are contained in the /etc/update-motd.d/ folder. Each script (text file) is preceded by a number that indicates in what order the scripts should be run. This is why the header is first (00) and the footer is last (99). Let’s take a look at the header file and see if we can match what part of the MOTD is generated by this header script.
user@machine:~# cat /etc/update-motd.d/00-header #!/bin/sh uname -a printf "%s\n" "$(lsb_release -s -d)" user@machine:~#
If you don’t know much about scripting, the very first line starts with a shebang. The shebang is the pound sign followed by the exclamation point (#!). The shebang signifies that the file is a script and should be run by a program. What follows the shebang is the path to program that should run the script. In this case, /bin/sh will interpret and run the script. Since we now know the following lines are shell commands, we can run the next command from the 00-header file via SSH and see it’s output to figure out what it does (if you don’t already know).
user@machine:~# uname -a Linux machine 220.127.116.11-rscloud #8 SMP Mon Sep 20 15:54:33 UTC 2010 x86_64 GNU/Linux
The output from this command matches with the first line of the MOTD when we checked it earlier. If you’ve already guessed that output of the second command in the 00-header file will match the second line of the MOTD, then so what?! You think you’re some kind of big shot all of a sudden?!
user@machine:~# printf "%s\n" "$(lsb_release -s -d)" Ubuntu 10.04 LTS
Good lookin out though. It does match with the second line of the MOTD. Since there are no other lines in this 00-header file, the rest of the MOTD must be generated in one of the other files in /etc/update-motd.d/. You can go through the other files and follow the same process to determine which scripts generate what text in the MOTD. If you want to edit any of the files, use nano if you’re a beginner or any other linux text editor if you’re already familiar with one.
user@machine:~# nano /etc/update-motd.d/00-header
Common commands are listed at the bottom of nano. The carrot symbol(^) stands for the Ctrl key so to exit you would hold the Ctrl and press the x key. Then you will be prompted on whether or not you want to save the changes to the file. You can also create a new script by using a text editor, just make sure to precede the name with a two digit number and a dash. Maybe you want to include a disclaimer message to users that log in via SSH.
user@machine:~# nano /etc/update-motd.d/09-disclaimer
Then add whatever text you’d like. Make sure the first line contains the shebang followed by the path to the program to interpret the script. I’ll give you an simple example that will output the text “I added a disclaimer message here which was generated from the 09-disclaimer file.” You can copy and paste (right-click the SSH window to paste or hold the Shift key and press the Insert key) the text below into your text editor and save the file.
#!/bin/sh echo "\nI added a disclaimer message here which was generated from the 09-disclaimer file."
The echo command means to display whatever is in quotes to the screen. The \n at the beginning of the sentence means to add a new line. This new line spaces our sentence one line down from the previous header message.
Now before this newly created script will work, we need to make sure that we enable permission to execute or run the script. Without permission for the script to run, it will just be ignored and you’ll wonder why it’s not showing up.
user@machine:~# chmod 755 /etc/update-motd.d/09-disclaimer
The chmod command is used to change permissions for files and stands for change mode. We’re not going to get into an explanation of permissions as there is just too much to cover. You can check out this link for some basic info if you’re curious.
Now the MOTD is updated each time a user logs in via SSH. If you’ve made some changes to any of the files in /etc/update-motd.d/ and want to check to make sure the scripts operate correctly and the message is displayed how you wanted, you can run the following command.
user@machine:~# run-parts /etc/update-motd.d/ Linux machine 18.104.22.168-rscloud #8 SMP Mon Sep 20 15:54:33 UTC 2010 x86_64 GNU/Linux Ubuntu 10.04 LTS I added a disclaimer message here which was generated from the 09-disclaimer file. Welcome to Ubuntu! * Documentation: https://help.ubuntu.com/
If you want to see which scripts are generating which text, you can use run the above command in verbose mode by adding the -v switch to the command.
user@machine:~# run-parts -v /etc/update-motd.d/ run-parts: executing /etc/update-motd.d//00-header Linux machine 22.214.171.124-rscloud #8 SMP Mon Sep 20 15:54:33 UTC 2010 x86_64 GNU/Linux Ubuntu 10.04 LTS run-parts: executing /etc/update-motd.d//09-disclaimer I added a disclaimer message here which was generated from the 09-disclaimer file. run-parts: executing /etc/update-motd.d//10-help-text Welcome to Ubuntu! * Documentation: https://help.ubuntu.com/ run-parts: executing /etc/update-motd.d//99-footer
If you just wanted to add simple text to your MOTD, hopefully the above will help you. If you want to add more dynamic information to the MOTD using Linux commands, you might have to do some research. Any Linux command or utility that can be run from the command line and outputs text can be added to the MOTD scripts. Good luck!