If you have ever used Ubuntu Server prior to its newest release of 16.04, then you should be familiar with the dynamic MOTD. Previously, this is what is presented to the user in terminal when a log on occurs.
This feature is very nice as it will give you a quick look at useful information. Unfortunately, Ubuntu Server 16.04 does not include the dynamic MOTD by default in this release. First you will need to install some prerequisites and build the directory structure. To do so, run the following commands in terminal.
# install lsb-release apt-get install lsb-release # install figlet to enable ASCII art apt-get install figlet # install update-motd software apt-get install update-motd # delete default directory rm -r /etc/update-motd.d/ # create new directory mkdir /etc/update-motd.d/ # create dynamic files touch /etc/update-motd.d/00-header ; touch /etc/update-motd.d/10-sysinfo ; touch /etc/update-motd.d/90-footer # make files executable chmod +x /etc/update-motd.d/* # remove MOTD file rm /etc/motd.dynamic
Once the above commands have been run, you can move on to populating the files you created. Below you can copy and paste.
Header (00-header
)
#!/bin/sh # # 00-header - create the header of the MOTD # Copyright (c) 2013 Nick Charlton # Copyright (c) 2009-2010 Canonical Ltd. # # Authors: Nick Charlton <hello@nickcharlton.net> # Dustin Kirkland <kirkland@canonical.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. [ -r /etc/lsb-release ] && . /etc/lsb-release if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then # Fall back to using the very slow lsb_release utility DISTRIB_DESCRIPTION=$(lsb_release -s -d) fi figlet $(hostname) printf "\n" printf "Welcome to %s (%s).\n" "$DISTRIB_DESCRIPTION" "$(uname -r)" printf "\n"
System Information (10-sysinfo
)
#!/bin/bash # # 10-sysinfo - generate the system information # Copyright (c) 2013 Nick Charlton # # Authors: Nick Charlton <hello@nickcharlton.net> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. date=`date` load=`cat /proc/loadavg | awk '{print $1}'` root_usage=`df -h / | awk '/\// {print $(NF-1)}'` memory_usage=`free -m | awk '/Mem:/ { printf("%3.1f%%", $3/$2*100) }'` swap_usage=`free -m | awk '/Swap:/ { printf("%3.1f%%", $3/$2*100) }'` users=`users | wc -w` time=`uptime | grep -ohe 'up .*' | sed 's/,/\ hours/g' | awk '{ printf $2" "$3 }'` processes=`ps aux | wc -l` ip=`ifconfig $(route | grep default | awk '{ print $8 }') | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'` echo "System information as of: $date" echo printf "System Load:\t%s\tIP Address:\t%s\n" $load $ip printf "Memory Usage:\t%s\tSystem Uptime:\t%s\n" $memory_usage "$time" printf "Usage On /:\t%s\tSwap Usage:\t%s\n" $root_usage $swap_usage printf "Local Users:\t%s\tProcesses:\t%s\n" $users $processes echo /usr/lib/update-notifier/update-motd-reboot-required /usr/lib/update-notifier/apt-check --human-readable echo
Footer (90-footer
)
#!/bin/sh # # 99-footer - write the admin's footer to the MOTD # Copyright (c) 2013 Nick Charlton # Copyright (c) 2009-2010 Canonical Ltd. # # Authors: Nick Charlton <hello@nickcharlton.net> # Dustin Kirkland <kirkland@canonical.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. [ -f /etc/motd.tail ] && cat /etc/motd.tail || true
Once these files are populated with the above, you should now be able to logout and then log back in either using putty or on the console and you will be presented with something that looks like below.
Didn’t work for me on Ubuntu 16.04 server until I did this:
Edit /etc/ssh/sshd_config
UsePAM yes
PrintMotd no
Then restart SSH…
sudo service ssh restart
Thanks for the update Aaron
Hi,
Aaron this helped me to. Changing /etc/ssh/sshd_config as you wrote did the trick and I am running Ubuntu 18.04.4 LTS
very nice guide, worked flawless for me on a 16.04-server, thank you very much for it. ๐
but one thing stays, and is shown TWO times below the last line (local user…) and it is this bunch of text:
“The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.”
i just dont know where it comes from. the only clue is that iยดm using a new user only for ssh-access, maybe theres something somewhere, but i just dont know where to look at… any hints?
Please take a look at
/etc/motd.tail
. Just clear the contents of the file and you should be good to go.hi. thanks for the fast reply. just had a look at the system, there is no such file :-/ tried to search with “find”, no result! -> ?!?!
Take a look at
/etc/update-motd
folder and ensure there are no other files in there besides those listed in the article.i checked the files; its a fresh install following your instructions above. the dynamic-file contains only what your example will show. and more should not be printed at login. i found lots of similar postings, maybe pointing to a bug. dunno. basically its working correct the way you are describing it above. o.O
I was having a trouble, but found a solution
https://www.reddit.com/r/linuxquestions/comments/6qt6b3/trouble_with_updatemotd_ubuntu_minimal_1604/
Please update HTML code
Bruno, thanks for the heads up. The article has been corrected
Is a reboot required? I’m still getting nothing but the last login line.
A reboot is not required. Please ensure you have run every command in the first code block.
For some reason I’m not getting anything. I feel like an idiot lol
Mike, please ensure that you have run every command in the first code block.
Everything but the comments haha
I do get failure saying there is no motd.dynamic to remove. However that shouldn’t be an issue. This isn’t a total loss on my current server, I just wanted to have it working so I can add the motd to future server when we get them off of 14.
Maybe try one of the previous tutorials and see if that gets you going. See https://oitibs.com/?s=motd
I’ve set this up on an LXD container and get the following error:
:~# run-parts /etc/update-motd.d/
/etc/update-motd.d//00-header: 24: /etc/update-motd.d//00-header: Syntax error: “&” unexpected
run-parts: /etc/update-motd.d//00-header exited with return code 2
System information as of: Fri Sep 16 11:32:29 UTC 2016
System Load: 0.04 IP Address: 10.1.1.130
Memory Usage: 3.3% System Uptime: 2 min
Usage On /: 1% Swap Usage: 0.0%
Local Users: 1 Processes: 23
10 packages can be updated.
0 updates are security updates.
/etc/update-motd.d//90-footer: 24: /etc/update-motd.d//90-footer: Syntax error: “&” unexpected
run-parts: /etc/update-motd.d//90-footer exited with return code 2
It’s a publishing bug I think. Replace the multiple & in the header and footer with just &.
damn. looks like the comment system took the HTML encoded entity for “& a m p ;” and decoded it to just &
All set. Thanks for catching that ๐
Great write-up! I have a few suggestions. Since these scripts run upon each ssh connection it is important they run fast. “route” command by default performs DNS lookup/address resolution. This may be, and often is, slow and my be measured in full seconds. To avoid the annoying lag “-n” switch can be used. When doing that “grep” should be tweaked as well to search for “0.0.0.0” instead of default. So the final snippet should look like “route -n | grep ‘^0.0.0.0’ -m 1” where “-m 1” tells grep that there is no need to continue after the first matching line is found.
Also both “route” and “ifconfig” are in /sbin directory which means that some environments (e.g. when executed by cron) which don’t have “/sbin” included in $PATH will not work. To make sure the scripts work they need to reference “/sbin/route” and “/sbin/ifconfig”.
Thanks Yuri
the free -m command displays differently on Ubuntu 16.04 and up than previous versions, which is why it always displays 0.0% memory and swap now.
So on line 23 of 10-sysinfo it should be this I guess?:
memory_usage=`free -m | awk ‘/Mem:/ { printf(“%3.1f%%”, $3/$2*100) }’`
Thanks for the update.
But now uptime doesn’t seem to work.
I am running a fresh install of 16.04.1 and everything is working as expected. Note, I have updated the sysinfo file to report the correct memory usage.
thanks I guess you slipped it in without me noticing….. ouch.
Thank you again.
I had been meaning to update the article anyhow, your first comment this morning just reminded me to do so. Thanks
thanks for the guide! something seems to be wrong there tho. memory and swap always showing 0%