Friday, July 5, 2024
HomeNetworkingAdd Secondary IP Address To CentOS / RHEL Network Interface

Add Secondary IP Address To CentOS / RHEL Network Interface

This guide will explain how to add a secondary IP address to a network interface on RHEL / CentOS 7/8 server. There are different ways of adding a secondary IP address to a RHEL / CentOS network interface. This guide will discuss manually creating a network configuration file and using Network Manager CLI tool – nmcli.

secondary ip address centos rhel

Add Secondary IP address Manually

The first method involve creating a sub-interface configuration file and populating network information into it. In my RHEL 8 server, my network interface has the following details.

# ip addr
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
     inet6 ::1/128 scope host 
        valid_lft forever preferred_lft forever
 2: enp1s0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
     link/ether 52:54:00:8f:8c:86 brd ff:ff:ff:ff:ff:ff
     inet 192.168.122.198/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0
        valid_lft 1891sec preferred_lft 1891sec
     inet6 fe80::4d61:1d19:14a4:a8dc/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever

The network interface I’ll assign a secondary IPv4 address is enp1s0. This interface configuration file is located inside the /etc/sysconfig/network-scripts directory.

cat /etc/sysconfig/network-scripts/ifcfg-enp1s0

Let’s create a sub-interface configuration file.

sudo yum -y install vim
sudo vim /etc/sysconfig/network-scripts/ifcfg-enp1s0:1

Don’t ignore :1 at the end. Then add IP configuration data.

DEVICE=enp1s0:1
Type=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.122.11
PREFIX=24

Reboot your server to confirm secondary IP address.

sudo reboot

After system restart, check IP information for the interface.

# ip addr
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
     inet6 ::1/128 scope host 
        valid_lft forever preferred_lft forever
 2: enp1s0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
     link/ether 52:54:00:8f:8c:86 brd ff:ff:ff:ff:ff:ff
     inet 192.168.122.197/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0
        valid_lft 3503sec preferred_lft 3503sec
     inet 192.168.122.11/24 brd 192.168.122.255 scope global secondary noprefixroute enp1s0:1
        valid_lft forever preferred_lft forever
     inet6 fe80::4d61:1d19:14a4:a8dc/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever

You can see we have two Ip addresses assigned to the interface – 192.168.122.197/24 and the secondary IP we assigned 192.168.122.11/24.

Confirm that you can access the server via secondary IP address.

$ ssh 192.168.122.11 
 The authenticity of host '192.168.122.11 (192.168.122.11)' can't be established.
 ECDSA key fingerprint is SHA256:5RuWUmEmb9WoXB6QSivSfvDj5DeaJrxT7N+GVTl/TA8.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added '192.168.122.11' (ECDSA) to the list of known hosts.
 Activate the web console with: systemctl enable --now cockpit.socket
 Last login: Sat Mar 23 11:03:35 2019 from 192.168.122.1
 [jmutai@rhel8 ~]$ ip ad | grep enp1s0
 2: enp1s0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
     inet 192.168.122.197/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0
     inet 192.168.122.11/24 brd 192.168.122.255 scope global secondary noprefixroute enp1s0:1

Add Secondary IP address using nmcli

The second method for those using NetworkManager service it to use nmcli command line tool. We will modify the same interface as shown earlier.

Check existing network connection profiles.

$ nmcli connection show 
NAME    UUID                                  TYPE      DEVICE 
enp1s0  498869bb-0d88-4a4c-a83a-c491d1040b0b  ethernet  enp1s0 

Mark the interface you want to add a secondary IP address then proceed to modify its configuration.

sudo nmcli con mod enp1s0 +ipv4.addresses "192.168.122.11/24"

The +ipv4.addresses option is used to assign a secondary IP address.

Reboot your server to confirm secondary IP address.

sudo reboot

Confirm secondary IP assignment.

$ ip ad
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
     inet6 ::1/128 scope host 
        valid_lft forever preferred_lft forever
 2: enp1s0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
     link/ether 52:54:00:8f:8c:86 brd ff:ff:ff:ff:ff:ff
     inet 192.168.122.197/24 brd 192.168.122.255 scope global dynamic noprefixroute enp1s0
        valid_lft 3546sec preferred_lft 3546sec
     inet 192.168.122.11/24 brd 192.168.122.255 scope global secondary noprefixroute enp1s0
        valid_lft forever preferred_lft forever
     inet6 fe80::4d61:1d19:14a4:a8dc/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever

You have successfully added a secondary IP address to an interface on RHEL / CentOS 8 Linux server. You can read more related articles on below links.

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments