Displaying IP Info on Console with Netplan


I had a need to display the IP configuration information on the default TTY console login screen of Ubuntu 18.04.1 LTS Bionic Beaver. Basically we needed a way to see what the IP was set to without having to login.

There were a few articles (like this from offbytwo.com) out there about ways to do this, but they no longer worked because Bionic Beaver moved to using Netplan as the default network configuration tool.

There were a couple of hurdles. I wanted to make sure the IP was displayed regardless of if the IP was static or DHCP. I opted to use the “ip” command to pull this data. Specifically the below command to limit to IPv4:

ip -4 -br addr

Next I wanted to get the current routing configuration, so I used the below command:

route -n

Now that I had the information, I just needed a way to automatically push this to /etc/issue. By default, Netplan uses networkd as the “renderer” of the configuration file. But networkd lacks an native concepts of “hooks” to execute scripts on state change like NetworkManager does.

Fortunately there is a package available called networkd-dispatcher that adds some very basic hook functionality. Installing this package creates four directories in the /etc/networkd-dispatcher directory. Each represents a different operational states of the network interface.

  • dormant.d – device has carrier (aka link) but not ready for traffic
  • no-carrier.d – device has no link
  • off.d – device powered down
  • routable.d – device has carrier (link) and a routable IP

Note: There are supposed to be two more according to the docs (carrier.d and degraded.d) but they didn’t exist when I pulled the package from ubuntu

The Process

With all this information I was able to create my desired setup with the below process:

First, Copy the default login text at /etc/issue to /etc/issue-standard (this lets us reset the login text)

sudo cp /etc/issue /etc/issue-standard

Next, save the below script somewhere on the system. I opted to place the file in the /etc/NetworkManager/dispatcher.d directory. This way if the renderer is changed to NetworkManager in the future it should still work.

#!/bin/bash

## You must create /etc/issue-standard by copying the default
cp /etc/issue-standard /etc/issue

## append brief IP4 info
ip -4 -br addr | tee -a /etc/issue

## insert blank line
echo '-----------------------------------------------------------------------------' | tee -a /etc/issue

## append routing table
route -n | tee -a /etc/issue

The name of the file isn’t critical, though you may want to preface with a number if you have multiple scripts you wish to execute in particular order. I named the file 99-display-ip

You then need to make the file executable.

chmod +x 99-display-ip

Finally, you’ll need to add symbolic links in each of the state directories under /etc/networkd-dispatcher/

ln -s /etc/NetworkManager/dispatcher.d/99-display-ip /etc/networkd-dispatcher/dormant.d/99-display-ip
ln -s /etc/NetworkManager/dispatcher.d/99-display-ip /etc/networkd-dispatcher/no-carrier.d/99-display-ip
ln -s /etc/NetworkManager/dispatcher.d/99-display-ip /etc/networkd-dispatcher/off.d/99-display-ip
ln -s /etc/NetworkManager/dispatcher.d/99-display-ip /etc/networkd-dispatcher/routable.d/99-display-ip

If all is working as expected you should be able to view the TTY login screen and see something like the below. I’m sure this could be prettied up a bit, but it met my need.

Screenshot of the console screen
Advertisements

2 thoughts on “Displaying IP Info on Console with Netplan

  1. I love this! Thanks for writing it up.

    Just one thing; you can use the ‘ip’ command to show routes too:

    ip -4 route (or ip -6 addr, ip -6 route, etc.)

    This is the way we recommend people display IPs with netplan, even if ifconfig will still work — the kernel remains the authority on what’s currently applied on interfaces.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s