In this guide we will show how you can configure networkd
to assign multiple IP addresses to one NIC, but also how to configure multiple Gateways in the same configuration.
The setup looks as following:
The device with the one NIC needs to access:
- LAN-1: 192.168.100.1/24 network – no DHCP present
- LAN-2:192.168.50.1/24 network – no DHCP present
- DHCP-LAN:the network from the router with the DHCP
For that, we need to configure one dynamic IP, coming from the DHCP and two static IPs to access LAN-1 and LAN-2. To configure this setup, we are going to use <a href="https://wiki.archlinux.org/index.php/Systemd-networkd" target="_blank" rel="nofollow noopener noreferrer">systemd-networkd</a>
. This guide will assume Ubuntu > 16.04 but similar idea should hold for other distributions.
Step 1: Enable networkd
sudo systemctl unmask systemd-networkd.service &&\
sudo systemctl enable systemd-networkd.service
Step 2: Configure NIC
In case you need to rename your NIC, check this.
Create a new .network
file /etc/systemd/network/10-nicx.network
:
[Match]
Name=eth0
[Network]
DHCP=ipv4
DNS=8.8.8.8
DNS=8.8.4.4
# LAN_1
[Address]
Label=eth0:0
Address=192.168.100.10/24
# LAN_2 - Has also a Gateway
[Address]
Label=eth0:1
Address=192.168.50.70/24
# "Static"-Gateway as backup
[Route]
Gateway=192.168.50.1
#Destination=0.0.0.0/0
Metric=1025 # The Gateway of the DHCP gets the default 1024
Restart the networkd
:
sudo systemctl restart systemd-networkd.service
and you are good to go!
Done – test
Check the routing table with route -n
:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 1024 0 0 eth0
0.0.0.0 192.168.50.1 0.0.0.0 UG 1025 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.1 0.0.0.0 255.255.255.255 UH 1024 0 0 eth0
192.168.50.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
In this case, the 192.168.1.1/24
network is coming from the router with the DHCP.
You also check with ip route get 8.8.8.8
which gateway is being used:
8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.189 uid 1000
cache
Additional/Optional Steps
Rename NIC
To rename a NIC using networkd
we are going to use the <a href="https://wiki.archlinux.org/index.php/Systemd-networkd#link_files" target="_blank" rel="nofollow noopener noreferrer">.link</a>
files. Create a new file /etc/systemd/network/10-nicx-rename.link
:
[Match]
MACAddress=00:25:90:4b:81:54
[Link]
Name=newname
Once it is renamed, re-generate your initramfs:
update-initramfs -c -k all
Reboot!
Disable udev rules, e.g. for usb-ethernet device
If you NIC is a USB-device, chances are that there are already udev
for it. Usually those rules will kick in first before systemd
has the chance to rename the device using the .link
file.
So depending on the distro you should look into this. As an example for the latest debian distros and for USB-network devices you can comment all the lines of the /lib/udev/rules.d/73-usb-net-by-mac.rules
. Don’t forget to reload the rules:
sudo udevadm control --reload-rules && sudo udevadm trigger
Disable NetworkManager
Normally you should not have issues running NetworkManager
in parallel. NetworkManager
is being used in the debian distributions to manage connections. Netplan by default uses NetworkManager
. In case you are having issues you can disable the NetworkManager
and only use networkd.
Disable Network Manager and enable systemd-networkd
sudo systemctl stop NetworkManager && \
sudo systemctl disable NetworkManager && \
sudo systemctl mask NetworkManager && \
sudo systemctl disable NetworkManager-wait-online.service
Next, start and enable the systemd-networkd service:
sudo systemctl unmask systemd-networkd.service && \
sudo systemctl enable systemd-networkd.service && \
sudo systemctl start systemd-networkd.service
If you want you can also update netplan:
# /etc/netplan/<>.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
Apply the changes by running the following command:
sudo netplan apply
Enable NetworkManager and Disable systemd networkd
First, stop the systemd-networkd service:
sudo systemctl disable systemd-networkd.service && \
sudo systemctl mask systemd-networkd.service && \
sudo systemctl stop systemd-networkd.service
Start the NetworkManager Service:
sudo systemctl unmask NetworkManager && \
sudo systemctl enable NetworkManager && \
sudo systemctl enable NetworkManager-wait-online.service && \
sudo systemctl start NetworkManager && \
sudo systemctl start NetworkManager-wait-online.service
Restore netplan if needed:
# /etc/netplan/<>.yaml
network:
version: 2
renderer: NetworkManager
Generate backend specific configuration files for NetworkManager with netplan command:
sudo netplan generate && \
<code class="language-">sudo systemctl restart NetworkManager
Now the NetworkManager is enabled, interface configurations can be done via the GUI or from the command line, using the nmcli command.