Ubuntu

How to Assign Multiple IP Addresses to Single NIC in Ubuntu 20.04 LTS

While working with computer systems, sometimes you might require multiple IP addresses on your physical machine. One of the solutions is to have multiple NICs installed on your system. However, it is not practical to buy a new NIC and plug it in your system when you have the option to assign multiple IP addresses to a single NIC which is referred to as IP Aliasing. The common use case of IP aliasing is the implementation of IP based virtual hosting.

In this post, we will be demonstrating you how to assign multiple IP addresses to a single NIC in Ubuntu OS. We will be demonstrating the procedure on Ubuntu 20.04 (Focal Fossa).

Assigning Multiple IP Addresses to Single NIC Temporarily

In the following section, we will see how to assign a second IP address to a NIC. The second IP address assigned by this method is temporary. Once you reboot the system, it will be removed automatically.

Here are the steps for assigning a second IP address to NIC.

1. First find the current IP address and the name of the interface in your system. To do so, issue the following command in Terminal:

$ ip addr

The following output shows our network interface is ens33 and the current IP address is 192.168.72.157/24 which is the dynamic IP address assigned by DHCP.

2. Let’s assign another IP address to the NIC. To assign an additional IP address to your NIC, issue the following command in Terminal:

$ ip adder add <ip-address> dev <interface-name>

Replace the <ip-address> with the additional IP address you want to assign to your NIC and <interface-name> with the name of your NIC. For instance, to assign the second IP address 10.1.1.5/8 to your network interface ens33, the command would be:

$ ip addr add 10.1.1.5/8 dev ens33

3. After running the above command, verify if the second IP address has been assigned to your network interface. Run the following command to do so:

$ ip addr

Here is the output of the above command which shows two IP addresses assigned to the ens33 network interface.

4. Now try to ping both IP addresses one by one. You can see both IP addresses are reachable.

Remove the Multiple IP addresses from NIC

In order to remove the additional IP addresses assigned to a NIC, run the following command:

$ sudo ip addr del <ip-address> dev <interface-name>

Replace the <ip-address> with the additional IP address you have assigned to your NIC and <interface-name> with the name of your NIC. For instance, to remove the IP address 10.1.1.5/8 assigned to our network interface ens33, the command would be:

$ sudo ip addr del 10.1.1.5/8 dev ens33

Assigning Multiple IP Addresses to Single NIC Permanently

In the previous section, we have assigned the second IP address using the “ip addr add” command which is not the permanent method. Once you reboot the system, the IP address removes automatically. In this section, we will see how to make the IP assignment permanent.

In order to assign multiple IP addresses to a single NIC permanently, you will need to configure it in the interfaces configuration file.

1. Edit the interfaces configuration file as follows:

$ sudo nano /etc/network/interfaces

Append the following lines in the file:

iface <interface-name> inet static
address <ip-address>

Replace the <interface-name> with the name of your NIC to which you want to assign the additional IP address and <ip-address> with the IP address, you want to assign. For instance, to assign the second IP address 10.1.1.5/8 to your network interface ens33, the lines would be changed to:

iface ens33 inet static

address 10.1.1.5/8

Once you have added the above lines, save, and close the file.

Now restart the networking service to apply the configuration changes you have made above:

$ sudo systemctl restart networking

Alternatively, bring down and bring up the interface to apply the changes:

$ sudo ifdown ens33
$ sudo ifup ens33

Now to verify if the additional IP address has assigned to the network interface, use the following command:

$ ip addr

As we have configured the additional IP address in the configuration file, therefore it will remain permanent even if you reboot the system.

Remove the Multiple IP addresses from NIC

You can also remove the additional IP address you have configured in the interfaces configuration file. To do so, simply edit the /etc/network/interfaces file and remove the entry for the additional IP addresses you have added. Then save and close the file and restart the networking services:

$ sudo systemctl restart networking

or bring down and bring up the interface to apply the changes

$ sudo ifdown ens33
$ sudo ifup ens33

This is how you can assign multiple IP addresses to a single NIC in Ubuntu OS. In this post, we have discussed both the temporary and permanent methods for assigning the secondary IP address to a single NIC. Following the same methods, you can allocate multiple IP addresses to a single NIC.

About the author

LinuxWays Team

LinuxWays Team

We create HowTos and Tutorials for Sys Admins. Visit us on <a href="https://linuxways.net/"LinuxWays.net