Multiple static IP Addresses on Linux

I ran into some difficulty recently with two programs that wanted to bind to the same port. The ports were not configurable, but the address that they tried to bind to was. If I had had multiple network cards installed, I could have bound one app to one card’s IP, and the other to the remaining IP…but I didn’t…

It turns out, in Linux it’s possible for a network device to have multiple IP addresses! I can assign several IPs and then bind them independently.

Temporarily adding an IP address to a device

Initially to test this out we can just add an IP address to a device. This will not persist through a reboot but it is sufficient to prove that our apps will run one-per-IP.

First, check the available network devices with ifconfig or ip:

$ ifconfig
lo: ...
wlp59s0: ...
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
...
2: wlp59s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue 
...

I’m going to add an IP address to wlp59s0. It already has an IP, as we can see in the output of ip addr:

$ ip addr show dev wlp59s0
2: wlp59s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.14/24 brd 192.168.0.255 scope global dynamic 

This device has been assigned 192.168.0.14 by the router. I want it to also have 192.168.0.15.

Note: you might want to check the client leases in your router, or at least run $ arp -a to see what else is active in your subnet, so that you don’t pick an IP that is already assigned to another device.

To add the IP address, we can use the ip command again:

$ sudo ip address add dev wlp59s0 192.168.0.15/24

That’s it!

Permanently assigning IPs to a device

To make our testing changes permanent we can assign static IP. In GDE we can do this through the Networking system settings pane, but we can also do it by manually by writing a network config file.

NetworkManager

By default in Fedora, networking is managed by NetworkManager and manually configured via drop-ins in /etc/sysconfig/network-scripts/. I will editing the drop-in named ifcfg-wlp59s0 - if a static IP address was already configured on this interface then the file may already exist - otherwise it should be created with this content:

# /etc/sysconfig/network-scripts/ifcfg-wlp59s0
IPADDR0="192.168.0.14"
IPADDR1="192.168.0.15"

systemd-networkd

On some systems (especially servers), networking may be managed by systemd-networkd. This is less common on laptops or for WiFi devices because it is relatively static.

We can edit an interface’s networkd config file to add a static IP address like so:

# /etc/systemd/network/wlp59s0.conf
[Match]
Name=wlp59s0

[Network]
...
Address=192.168.0.14/24 # Note that this CIDR indicates the subnet range for this LAN
Address=192.168.0.15/24
...

Reboot to check persistence

Now, we can reboot and check $ ip addr and we’ll see that our device has both of the static addresses that we’ve configured! :)