Last week I was deploying a Debian 9 (Stretch) based Linux OVA from the Bitnami catalog. One of the first things to do after a deployment is to replace the DHCP assigned IP address with a static one.

Deploying the VM itself is not really blog worthy. What did bother me, is that my VM ended up with 2 IP addresses on 1 ethernet interface after configuring a static IP. The interface had a DHCP and a static assigned IP.

It took me a while to understand why my VM had 2 IP addresses and what systemd had to do with that. This post answers that question and as a bonus it seems that configuring interfaces with systemd has some nice advantages. Let me tell you about it in this post.

The initial config

At first I always start to configure the hostname and DNS related information.

  • hostname in /etc/hostname
  • DNS related info in /etc/resolv.conf or /etc/network/interfaces

Now it’s time to enter the IP information in the place I was used to, so in /etc/network/interfaces

First I looked up the assigned NIC name since we’re using Predictable Network Interface Names nowadays. When using the command below the DHCP assigned IP is listed. If not use the ip link command.

bitnami@debian:~$ ip -4 a
1: lo: <skip loopback :-) >
2: ens160: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.1.117/24 brd 192.168.1.255 scope global dynamic ens160

Now it was time to configure the interface itself by putting the proper lines in the /etc/network/interfaces

auto ens160
iface ens160 inet static
address 192.168.1.200
netmask 255.255.255.0
gateway 192.168.1.254

dns-nameservers 1.1.1.1 1.0.0.1
dns-search <your.search.domain>

The issue

After executing an ifdown / ifup command, I ended up with the original DHCP assigned address (.117) and the static configured address (.200), but expected only a single IP.

bitnami@debian:~$ ip -4 a
1: lo: <skip loopback :-) >
2: ens160: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.1.200/24 brd 192.168.1.255 scope global ens160
valid_lft forever preferred_lft forever
inet 192.168.1.117/24 brd 192.168.1.255 scope global secondary dynamic ens160

The solution

After digging into this for a while I stumbled upon an article mentioning that IP addresses can be assigned by systemd. The Bitnami OVA configures the IP address with systemd out of the box and in addition to that I configured it manually also through the interfaces files. In hindsight it’s sounds logical that 2 IP addresses are assigned to the interface.

The solution is to remove the configuration in the interfaces files and configure the IP information only with systemd. Not only IP addresses can be configured with systemd, also other network related parameters like persistent routes, NTP, DNS, MTU size and DHCP can be configured with it.

Let me show you it works. Basically create separate config file for every NIC in your VM or host within the location /etc/systemd/network. Let’s assume a VM with 2 NIC’s. NIC 1 has the default gateway and connects to the internet and NIC 2 has a connection to a backend network.

In my case a default config file was created by Bitnami to enable DHCP for every ethernet interface.

/etc/systemd/network/99-dhcp.network
[Match]
Name=en*

[Network]
DHCP=yes

The second file configures NIC 1, which is the internet facing NIC in this example.

/etc/systemd/network/192-wired.network
[Match]
Name=ens192

[Network]
DHCP=no
Address=100.100.100.10/29
Gateway=100.100.100.9
NTP=<your NTP server pool>

The third files configures NIC 2, which is the backend facing NIC in this example.

/etc/systemd/network/160-wired.network
[Match]
Name=ens160

[Network]
DHCP=no
Address=192.168.1.10/24
DNS=<your DNS Server 1>
DNS=<your DNS Server 2>
Domain=<your.search.domain>

[Route]
Gateway=192.168.1.1
Destination=192.168.1.0/22

After all modifications are performed, reload the network stack to make them active.

systemctl restart systemd-networkd

To conclude

After giving me a headache initially, from my opinion it makes sense to use systemd for configuring interfaces since pretty much all al the parameters you need can be configured with one solution.

If you have a recent Linux distro, systemd is probably already used to control service startup. So you are most likely familiar with that part already.

Thank you for reading and let me know if you have feedback.

Useful links

Bitnami WordPress OVA

ArchLinux systemd-networkd Wiki

Freedesktop.org systemd.network explained

Freedesktop.org Predictable Network Interface Names explained


0 Comments

Leave a Reply