How to add secondary IP address on RHEL/CentOS 8

Sometimes, you might have to assign a secondary IP address to a single Network Interface Card (NIC) on RHEL 8 and CentOS 8 systems.

There are numerous reasons for this and some of them, such as application requirement or installation of SSL certificate.

There are two ways to add a secondary IP address to the RHEL 7 and CentOS 7 network interface.

  • Using Network Scripts files (ifcfg-*)
  • Using nmcli command

In this guide, we will explain how to add additional or multiple IP addresses in RHEL 7/8 and CentOS 7/8. The same procedure works on Rocky Linux and Almalinux.

Method-1: Adding Secondary IP address Manually

Before proceeding to configure a secondary IP, let’s see the current configuration using the IP command.

# ip a show enp0s3

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000      
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff      
    inet 192.168.1.4/24 brd 192.168.1.255 scope global enp0s3
       valid_lft forever preferred_lft forever      
    inet6 fe80::a00:27ff:fe97:132e/64 scope link         
       valid_lft forever preferred_lft forever

Also, let’s see the contents of our ‘ifcfg-enp0s3’ file:

# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3

NAME="enp0s3" 
DEVICE="enp0s3" 
ONBOOT="yes" 
NETBOOT="yes" 
UUID="3grf34s0-9045-4312-e12t-567v90lk0e9t" 
IPV6INIT="yes" 
BOOTPROTO="none" 
TYPE="Ethernet" 
PROXY_METHOD="none" 
BROWSER_ONLY="no" 
DEFROUTE="yes" 
IPV4_FAILURE_FATAL="no" 
IPV6_AUTOCONF="yes" 
IPV6_DEFROUTE="yes" 
IPV6_FAILURE_FATAL="no" 
IPADDR=192.168.1.4 
PREFIX=24 
GATEWAY=192.168.1.1 
DNS1=8.8.8.8

As the above output shows that we had already configured the static IP address. Now, we will modify this file to accommodate secondary IP address as well.

To do so, modify the "IPADDR" variable to “IPADDR0” for 1st IP and “IPADDR1” for 2nd IP as shown below:

IPADDR0=192.168.1.50
IPADDR1=192.168.1.51

This can be further configured to accommodate more IPs by adding the variables “IPADDR2”, “IPADDR3”, and so on.

After modification, the file should be as shown below:

# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3

NAME="enp0s3" 
DEVICE="enp0s3" 
ONBOOT="yes" 
NETBOOT="yes" 
UUID="3grf34s0-9045-4312-e12t-567v90lk0e9t" 
IPV6INIT="yes" 
BOOTPROTO="none" 
TYPE="Ethernet" 
PROXY_METHOD="none" 
BROWSER_ONLY="no" 
DEFROUTE="yes" 
IPV4_FAILURE_FATAL="no" 
IPV6_AUTOCONF="yes" 
IPV6_DEFROUTE="yes" 
IPV6_FAILURE_FATAL="no" 
IPADDR0=192.168.1.50 
IPADDR1=192.168.1.51
PREFIX=24
GATEWAY=192.168.1.1 
DNS1=8.8.8.8

Once configured, run the following commands and bring down and bring up the interface.

# ifdown enp0s3 && ifup enp0s3

Now, run the ip command to verify if both the IP addresses have been successfully assigned to ‘enp0s3’ interface or not:

# ip a show enp0s3

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000      
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff      
    inet 192.168.1.50/24 brd 192.168.1.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff      
    inet 192.168.1.51/24 brd 192.168.1.255 scope global secondary noprefixroute enp0s3
       valid_lft forever preferred_lft forever      
    inet6 fe80::a00:27ff:fe97:132e/64 scope link         
       valid_lft forever preferred_lft forever

Method-1(a): Configure a Virtual Network Interface

Similarly you can configure a secondary IP address using virtual network interface.

To do so, create a virtual interface. Note that you need to create a new interface file with 'enp0s3:1'.

# touch /etc/sysconfig/network-scripts/ifcfg-enp0s3:1

Add the following configuration for secondary IP.

# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3:1

DEVICE=enp0s3:1
Type=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.1.51
PREFIX=24

Once you configured, run the below command to shut down the interface and bring it back:

# ifdown enp0s3 && ifup enp0s3

Check if the new configurations are populated by using ip command:

# ip a show enp0s3

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000      
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff      
    inet 192.168.1.50/24 brd 192.168.1.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff      
    inet 192.168.1.51/24 brd 192.168.1.255 scope global secondary noprefixroute enp0s3:1
       valid_lft forever preferred_lft forever      
    inet6 fe80::a00:27ff:fe97:132e/64 scope link         
       valid_lft forever preferred_lft forever

Method-2: Adding Secondary IP address using nmcli

We can add a secondary ip to the same interface using nmcli command as shown below:

Run the below nmcli command to configure a secondary ip address. Use the '+ipv4.addresses' option for adding a secondary IP address.

# nmcli con mod enp1s0 +ipv4.addresses "192.168.1.51/24"

To save these changes and to reload the interface, run:

# nmcli con up enp0s3

Verify the configured IPs.

# ip a show enp0s3

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000      
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff      
    inet 192.168.1.50/24 brd 192.168.1.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    link/ether 08:00:27:97:13:2e brd ff:ff:ff:ff:ff:ff      
    inet 192.168.1.51/24 brd 192.168.1.255 scope global secondary noprefixroute enp0s3
       valid_lft forever preferred_lft forever      
    inet6 fe80::a00:27ff:fe97:132e/64 scope link         
       valid_lft forever preferred_lft forever

Closing Notes

In this guide, we’ve shown you how to configure additional or seconday IP in RHEL 7/8 and CentOS 7/8 using two different methods.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to add secondary IP address on RHEL/CentOS 8”

Leave a Reply

Your email address will not be published. Required fields are marked *