<<< Back to the Linux Tips Index

27th September 2018

Using Systemd to update the console with dynamic information

Systemd is an often-hated thing. But here is one task for which I found Systemd to be quite well suited.

I have a Virtual Machine which I boot occasionally. It's on a DHCP subnet, so I don't necessarily know what IP address it will have each time.

It's a Jenkins server, as it happens; I don't often need to log in to it, but I do need to access its service via a web browser. So the only thing I care about, once it has booted up, is the DHCP address that has been assigned to it.

Many times I had booted it up, and then tediously logged in to it, just to run "ip a" in order to find its current IP address.

Instead, I have now installed this simple Systemd Unit file, and a tiny shell script which it executes, so the IP address is now shown to me above the login prompt.

The Script

The script is pretty simple; if it's passed a message, it puts it in /etc/issue, which (on RedHat-based systems, at least) is displayed on the console above the login prompt. Otherwise, it puts the machine's current IP address in there.

/usr/bin/showip.sh

#!/bin/bash
IP=${1:-`hostname -I`}
# Add "IP ADDRESS" line if not already found
grep "IP ADDRESS" /etc/issue > /dev/null
if [ "$?" -ne "0" ]; then
  echo >> /etc/issue
  echo "IP ADDRESS" >> /etc/issue
fi
sed -i s/"IP ADDRESS.*"/"IP ADDRESS : $IP"/g /etc/issue

The Systemd Unit file

The Unit file is also very simple. Every time the network comes up (particularly, at boot), the Unit file causes the script to be run (with no parameters passed to it, so the script updates /etc/issue with the current IP address). When the service is stopped (when the machine is being shut down), it replaces the old IP address with the word "UNKNOWN". So - if the next time it boots, for some reason it can't find the IP address, you get a clear message communicating that fact, instead of being told the previous IP address, which may have been assigned weeks ago and then discarded.

/lib/systemd/system/showip.service

[Unit]
Description=Add IP address to /etc/issue
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/showip.sh
RemainAfterExit=true
ExecStop=/usr/bin/showip.sh UNKNOWN

[Install]
WantedBy=multi-user.target

Conclusion

This simple service manages the task of displaying the machine's IP address to anybody, without needing to log in. Because Systemd may display the login prompt before the showip service has run, you may get an "UNKNOWN" message initially, but all you need to do is to hit <ENTER> in order to display a new login prompt, which will re-read the now-updated /etc/issue file, and display the current IP address.

And of course, there's no real security issue here, as we're only displaying this information to users who already have access to the console; you wouldn't want to advertise this extra information on a fully-hardened server, but for my purposes, this convenience trumps any security concerns about knowing the VM's IP address.


Invest in your career. Buy my Shell Scripting Tutorial today:

 

Steve Parker - Linux / DevOps Consultant
Share on Twitter Share on Facebook Share on LinkedIn Share on Identi.ca Share on StumbleUpon