How to configure multiple IP addresses on Linux

On Linux, you can assign IP addresses in two ways: temporary and permanent. Temporary IP addresses work only in the current login session. Linux stores them in RAM and removes them when you log out from the current login session or restart the system. Permanent IP addresses work until you manually remove them. Linux stores them in configuration files.

Two devices can communicate when they belong to the same IP subnet. They need a router to speak if they belong to different IP subnets. Let us take an example. The following image shows a network. In this network, PC1 and PC2 belong to subnet 10.0.0.0/8, and PC3 belongs to subnet 20.0.0.0/8. In this situation, PC1 and PC2 can communicate directly but can't communicate with PC3. If they want to communicate with PC3, they need a router.

Network with a router

Routers are expensive. Moreover, managing them is also complicated. You should add them only when you have a complex network or your network needs them. If you have a simple network with only some IP subnets, you can assign multiple IP addresses to computers belonging to different IP subnets.

Let's take another example.

The following image shows a network. This network has two IP subnets: 10.0.0.0/8 and 20.0.0.0/8. PC1 and PC2 belong to subnet 10.0.0.0/8, PC4 and PC5 belong to subnet 20.0.0.0/8, and PC3 belongs to both subnets. In this case, you can assign two IP addresses to PC3, one from each subnet.

Network without router

Assigning multiple temporary IP addresses on Linux

To assign multiple temporary IP addresses, we use the following command.

#ip addr add [IP ADDRESS/Subnet Mask] dev [interface]

The following command adds the IP address 20.0.0.10/8 to the interface ens160.

 #ip addr add 20.0.0.10/8 dev ens160

The following image shows the above command with the output.

Adding multiple ip addresses with the ip addr command

Temporary IP addresses add flexibility to the network. You can use them as required without changing the configuration files. For example, suppose you want to connect to a different IP network to share or receive some files. In that case, you can configure a temporary IP address from that IP subnet on your computer.

When you assign a temporary IP address from that IP subnet, your computer becomes a member of that IP subnet. If two computers belong to the same IP subnet, they can exchange data without using a router. Temporary IP addresses allow you to exchange data with any connected IP subnet without a router.

Verifying temporary ip address

Assigning multiple permanent IP addresses on Linux

If you connect your computer with different IP subnets regularly, you can permanently configure multiple IP addresses. Linux stores an interface's IP configuration and other network settings in a configuration file. For the naming convention, it uses the interface name with the ifcfg prefix. For example, if the interface name is ens160, the file name will be ifcfg-ens160. Linux stores all configuration files in the /etc/sysconfig/network-scripts/ directory.

Listing all configurations

It uses IPADDR and PREFIX directives to save IP addresses.

We can configure multiple IP addresses by adding ascending numbers to these directives. The following table lists four example IP configurations.

First IP configuration Second IP configuration Third IP configuration Fourth IP configuration Fifth IP configuration
IPADDR IPADDR IPADDR1 IPADDR2 IPADDR3 IPADDR4
PREFIX PREFIX PREFIX1 PREFIX2 PREFIX3 PREFIX4
IP 10.0.0.1 20.0.0.1 30.0.0.1 40.0.0.1 50.0.0.1
prefix 8 8 8 8 8

You can update this file using any text editor. After updating this file, you need to restart the interface.

Use the following commands to restart the interface and its connections.

#nmcli device down [interface]
#nmcli device up [interface]
#nmcli con reload

The following image shows the above exercise.

Updating the configuration file

If you want to avoid editing configuration files directly, you can use the nmtui utility. The following steps explain how this utility can configure and manage multiple IP addresses.

  • Use the nmtui command to start the utility
  • Select the Edit a connection option and press the Enter key
  • Select the interface from the left pane and the 'Edit' option from the right pane and press the Enter key
  • On the IPv4 configuration option, select the Manual method, select the Add option, and press the Enter key
  • Set the new IP address
  • To remove an IP address, use the Remove option
  • You can also update/change an existing IP address
  • After making changes, select OK and press the Enter key
  • On the previous screen, select the Back option and press the Enter key
  • On the main screen, use the Quit option to quit the utility

The following image shows the above steps.

Using the nmtui command

Restart the interface and its connections to apply the changes.

Verifying multiple ip addresses

Practice LAB

To practice this topic, you need two Linux systems. Let us say the first system is Server1, and the second is Server2.

On Server1, configure four IP addresses from different IP subnets on the same interface. For example, you can configure 10.0.0.1/8, 20.0.0.10/8, 30.0.0.10/8, and 40.0.0.10/8 on the interface ens160.

Verifying multiple ip addresses

On Server2, configure one IP address from one IP subnet at a time and use the ping command on Server1 to test the connectivity. If you get the reply, update the IP address on Server2. Set an IP address from the next IP subnet and test connectivity from Server1 again.

Testing connectivity

If you have RHCE practice LAB, you can use Server1 and Server2 to complete the above exercise. You can check out the following tutorial to learn how to set up the RHCE Practice Lab.

RHCE Practice Lab Setup in VMWare and VirtualBox

Conclusion

Linux supports multiple IP configurations on an interface. This feature allows us to connect the system with different subnets without additional interfaces. We can configure many IP configurations on an interface and use each IP configuration to connect with a different subnet.

ComputerNetworkingNotes Linux Tutorials How to configure multiple IP addresses on Linux

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com