Linux Commands

How to bind multiple addresses to an interface on Linux

This tutorial explains how to bind or assign multiple IP addresses to a network interface on Linux.

Adding multiple addresses to a single network card is pretty simple, just as adding a unique IP address. After reading this tutorial, you’ll know how to implement this in Debian and its based Linux distributions such as Ubuntu.

To begin, list your network devices to see their current IP address by running the command below.

sudo ip addr

As you can see, there is a loopback interface and a network card named enp2s0 without an IP address assigned.

Now let’s begin adding IP addresses. This first method explained doesn’t leave persistent IP addresses; after reboot, they’ll be removed. But after those instructions, I added instructions to add multiple IP addresses to the network card permanently.

Adding multiple IP addresses to the network card (no persistent method):

The example below shows how to assign the IP address 10.0.1.100 to the network card enp2s0.

sudo ip addr add 10.0.1.100/24 dev enp2s0

As you can see in the image above, the IP address was added properly.

Adding a second or third IP address does not require a different command than adding a single IP. Therefore, I run the following command to add the second IP address 192.168.0.100 to the same network card.

sudo ip addr add 192.168.0.100/24 dev enp2s0

And as you can see, the second IP address was added correctly. You can add more IP addresses running the same command, replacing the IP addresses with these you want to assign to your card.

Binding multiple IP addresses to the network card (persistent):

To add multiple persistent IP addresses to your network card, you need to edit the /etc/network/interfaces configuration file.

sudo nano /etc/network/interfaces

To add an IP address, the syntax is pretty simple, as depicted below.

iface <NetworkDevice> inet static
  address <IP>/<NetMask>

To add multiple IP addresses, just type the syntax, adding more IP addresses as shown in the following example. In the image below, I assign 3 IP addresses to the network card named enp2s0.

iface enp2s0 inet static

  address 10.0.0.100/24

iface enp2s0 inet static

  address 192.168.0.100/24

iface enp2s0 inet static

  address 172.12.43.4/16

Exit nano saving changes (Ctrl+X) and run the command below to apply the changes you just made.

sudo ifup enp2s0

Then check your IP addresses by running

ip addr

As you can see, the IP addresses were added properly.

Removing IP addresses from your network card:

To remove permanent IP addresses on the /etc/network/interfaces file, just comment or remove the addresses you added and restart the network device.

You can also remove temporary IP addresses using similar syntax as when adding; just replace add with del as shown in the image below, in which the IP 10.0.0.100/24 is deleted.

sudo ip addr del 10.0.0.100/24 dev enp2s0

As you can see, the IP address was removed.

Assign IP using DHCP:

I decided to add instructions to get an IP address dynamically through DHCP to make this tutorial complete.

To get a dynamic IP at demand, you can execute the command below.

sudo dhclient enp2s0

For a permanent configuration to get a dynamic IP at boot, edit the configuration file /etc/network/interfaces adding iface inet dhcp. You can add the line by running the following command, remember to replace enp2s0 for your network card.

echo "iface enp2s0 inet dhcp" >>/etc/network/interfaces

Once edited, set your network device down to restart it by running the following command.

sudo ifdown <NetworkDevice>

And then, set it up by executing the command below as depicted in the screenshot.

sudo ifup <NetworkDevice>

As you can see in the image, the network card tries to fetch a dynamic IP address, failing because it’s unplugged from the router.

DHCP vs Static:

Static IP addresses are a basic need to keep accessible services or network rules applied to specific devices. DHCP is a basic need, at least for guest clients without a static address configured.

Usually, if you connect with your Linux device to a public network and don’t receive an IP address, you can solve this by running dhclient ; if you don’t, the gateway may have the dhcp service disabled.

Except for the guests, keeping a network without fixed IP addresses is senseless, meaning you need to update your host’s table every time addresses change. Dynamic IP addresses are the most common way to get public internet addresses.

Adding DNS:

As an additional tip, I also decided to add DNS to your resolv.conf, in case your system doesn’t resolve hosts properly. You can add Google public DNS by running the following command.

echo "nameserver 8.8.8.8" > /etc/resolv.conf

Conclusion:

As you can see, binding multiple IP addresses to an interface on Linux is pretty easy. The process is simple and can be executed by all user levels. Assigning multiple network addresses to a single network card is a formidable method to cut resources and configuration time.

Note this tutorial only focused on up to date commands. This tutorial didn’t explain obsolete commands like ifconfig, which you can read in this ifconfig tutorial.

Thank you for reading this tutorial explaining how to bind multiple IP addresses to an interface on Linux. Keep following us for more Linux tips and tutorials.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.