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 only if they belong to the same IP subnet. If they belong to different IP subnets, they need a router to communicate. 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 they can't communicate with PC3. If they want to communicate with PC3, they need a router.

network with router

Routers are expensive. Moreover, managing them is also complicated. You should add them to the network only when you have a complex network or your network needs them. If you have a simple network having only some IP subnets, you can assign multiple IP addresses on computers that belong 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. PC3 belongs to both subnets. In this case, you can assign two IP addresses on 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 per the requirement without making any changes in 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 some different IP subnets regularly, you can configure multiple IP addresses permanently. Linux stores IP configuration and other network settings of an interface 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, then 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.

By adding ascending numbers with these directives, we can configure multiple IP addresses. 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 use any text editor to update this file. 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.

directly updating configuration file

If you don't want to edit configuration files directly, you can use the nmtui utility. The following steps explain how to use this utility to 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.

adding ip addresses by using nmtui command

You need to restart the interface and its connections to apply the changes.

verifying multiple ip addresses

Practice LAB

You need two Linux systems to practice this topic. 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 address

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. To learn how to set up RHCE Practice Lab, you can check the following tutorial.

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. With this feature, 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