Simple linux hack: Show the IP info on the terminal login screen

GS
Your Everyday Generalist
3 min readJan 1, 2021

I have played with virtual-box VMs a lot in my career, and one thing that always bugged me was not having an easy way to figure out the IP address of my running VM when setup in the bridge networking mode. It was surprising painful to get the IP of the running VM via VBoxManage cli utility (in fact I am not sure even if there’s a way to get it easily for VMs running in the bridge networking mode). I often have to SSH into the VM and then run ip addr show to get the IP. If you are using SSH config setup from VSCode, then this is a painful step at least to me.

To make my life easier I decided to add the IPv4 information to the login screen (eg; Ubuntu 20 server) so that I know the IP from the get go.

In order to do so, there are few questions that we need to answer first

  1. how does login screen gets the text it shows on the terminal?
  2. how do I update the information at boot time if I figure out 1?
  3. what all to display?

A quick search on google hinted below for above questions:

  1. /etc/issue may be the file that can be used to display custom information at the login screen.
# From linux man page - https://man7.org/linux/man-pages/man5/issue.5.html
/etc/issue
is a text file which contains a message or system
identification to be printed before the login prompt. It may
contain various @char and \char sequences, if supported by
the getty-type program employed on the system.

2. /etc/network/if-up.d/ can be used to trigger a custom script that can get us the IP information to populate on the /etc/issue.

# From https://manpages.debian.org/testing/ifupdown/interfaces.5.en.html#HOOK_SCRIPTS/etc/network/if-up.d/Scripts in this directory are run after bringing the interface up.

3. since if-up.d gets triggered when interfaces come up, we can invoke linux commands to get IP information and populate it under the /etc/issue file.

With research out of the way, now it’s the time to do some hands on. For the test setup here, I will be using Ubuntu 20.04 LTS image under VirtualBox.

Goal: Add Host info + IPv4 address of all interfaces to the login screen.

For host info we can use lsb_release command:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal

For IPv4 address we can use ip command:

$ ip -4 -br a
lo UNKNOWN 127.0.0.1/8
enp0s3 UP 192.168.15.24/24

Cool, so we have two things we need to proceed. Let’s create a bash script that can help us display above information in a better format and persist to /etc/issue file.

#!/bin/sh
set -e
HOSTINFO=$(/usr/bin/lsb_release -a 2>&1 | grep Description | /usr/bin/cut -d ":" -f2 | /usr/bin/tr -d [:blank:])IPADDRS=$(/usr/sbin/ip -4 -br a)/usr/bin/echo -ne "Host OS: $HOSTINFO\n$IPADDRS\n" > /etc/issue

With the above custom script we need to hook it up to the network interface bring up phase during linux boot up time. As per netplan faq, it seems if your OS uses netplan (which is in my case), then as per below table mapping the scripts qualifying for if-up.d should reside under /etc/networkd-dispatcher/routable.d

Netplan suggests placing post scripts under routable.d
netplan suggests placing post-up hook scripts under routable.d instead of if-up.d

I placed the above custom script under /etc/networkd-dispatcher/routable.d/add-ip-to-login-screen. We need to ensure that the script added has executable permissions.

$ sudo chmod +x /etc/networkd-dispatcher/routable.d/add-ip-to-login-screen

Voila, we are done here. Reboot your VM and you should see the host OS and IPv4 information on your new shiny login screen.

Note: the custom script is kinda dumb as it gets triggered on every interface bring up. if you are interested in triggering it only on a specific interface, then just add an if statement like below:

[ "$IFACE" = "eth0" ] || exit 0

Thanks for reading! If you found this article helpful, please consider buying me a coffee to support my work. Thanks again!

--

--

GS
Your Everyday Generalist

Senior Software Engineer - HashiCorp, Anime lover, OSS contributor, Go enthusiast, Sporadic writer!