Ubuntu dynamic MOTD on Debian 8
One thing I really do like about Ubuntu was the nice, concise little screen that you get when you SSH into a host. It was a very nice, very quick way to get a glimpse of how your machine was doing as you were logging into it, which is especially nice on servers. Debian unfortunately doesn’t have this functionality, so I went off in search of how to make it work… Luckily, I didn’t have to look far as someone had already done it for me.
Originally published by OITIBS on June 6th, 2015
Upon logging into (now only one of) my servers, I was always greeted with a screen similar to the one below. My switch to Debian however, removed that functionality. I wanted it back!
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-85-generic x86_64) * Documentation: https://help.ubuntu.com/ System information as of Tue Sep 5 00:09:13 EDT 2017 System load: 0.05 Processes: 102 Usage of /: 25.1% of 19.56GB Users logged in: 0 Memory usage: 31% IP address for eth0: 162.243.987.54 Swap usage: 0% IP address for tun0: 10.8.0.1 Graph this data and manage this system at: https://landscape.canonical.com/ No mail. Last login: Tue Sep 5 00:09:14 2017 from c-73-987.98.76.hsd1.ca.comcast.net
To get this back in Debian 8 (and possibly other flavors or versions), you’ll need to install a few prerequisites:
- First off is LSB or Linux Standard Base as a few of these pieces depend on it:
$ sudo apt install lsb-release
- Next is figlet for a welcome little bit of ASCII-Art on the new screen:
$ sudo apt install figlet
- Now, we will create the directory for the template files and then CD into it:
$ sudo mkdir /etc/update-motd.d/ && cd $_
- And next is the dynamic template files themselves:
$ sudo touch 00-header && sudo touch 10-sysinfo && sudo touch 90-footer
- And last, we’ll remove the old MOTD and set up the new symlink:
$ sudo rm /etc/motd $ sudo ln -s /var/run/motd /etc/motd
Now once that has been completed, we can get to populating the template files that we created previously. Copy and paste the content below into the files created in step 4.
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:/ { total=$2 } /buffers/cache/ { used=$3 } END { printf("%3.1f%%", used/total*100)}'` swap_usage=`free -m | awk '/Swap/ { printf("%3.1f%%", "exit !$2;$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%stIP Address:t%sn" $load $ip printf "Memory usage:t%stSystem uptime:t%sn" $memory_usage "$time" printf "Usage on /:t%stSwap usage:t%sn" $root_usage $swap_usage printf "Local Users:t%stProcesses:t%sn" $users $processes echo
Footer (90-footer)
#!/bin/sh # # 90-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 you have all of that loaded, you should be able to log out and log back in to see your new handiwork. It should look like the following:
__ _____ _ _ __ _ __ _ ___ _ __ / / _ | | | |/ _` |/ _` |/ _ '__| V / (_) | |_| | (_| | (_| | __/ | _/ ___/ __, |__,_|__, |___|_| |___/ |___/ Welcome to Debian GNU/Linux 8.9 (jessie) (3.16.0-4-amd64). System information as of: Mon Sep 11 20:39:01 PDT 2017 System load: 0.01 IP Address: 10.0.0.10 Memory usage: 10.8% System uptime: 12 days Usage on /: 61% Swap usage: 0.0% Local Users: 0 Processes: 145 You have new mail. Last login: Mon Sep 11 18:03:14 2017 from 10.0.0.137
Enjoy!