Assign multiple IP addresses to a network card on CentOS 7

Posted by lixid on Thu, 10 Feb 2022 05:43:52 +0100

Sometimes you may want to give a network card multiple addresses. What should you do? Buy another network card to assign the address? You don't have to do this in small networks. We can now assign multiple ip addresses to a network card in CentOS/RHEL 7. Want to know what to do? OK, follow me. It's not difficult.

First, let's find the IP address of the network card. In my CentOS 7 server, I only use one network card.

Run the following command with root privileges:

ip addr

Sample output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
       valid_lft forever preferred_lft forever

As you can see above, my network card name is enp0s3 and my ip address is 192.168.1.150.

As you know, the configuration file of the network card is stored in / etc / sysconfig / network scripts /. The details of each network card will be stored under different names, such as ifcfg-enp0s3.

Let's look at the details of ifcfg-enp0s3.

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

Sample output:

TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="enp0s3"
UUID="e9f9caef-cb9e-4a19-aace-767c6ee6f849"
ONBOOT="yes"
HWADDR="08:00:27:80:63:19"
IPADDR0="192.168.1.150"
PREFIX0="24"
GATEWAY0="192.168.1.1"
DNS1="192.168.1.1"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"

OK, now we will assign multiple addresses in the same subnet.

Edit the file / etc / sysconfig / network scripts / ifcfg-enp0s3:

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

Add additional IP addresses as follows.

TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="enp0s3"
UUID="933cdc9b-b383-4ddd-b219-5a72c69c9cf0"
ONBOOT="yes"
HWADDR="08:00:27:3F:AB:68"
IPADDR0="192.168.1.150"
IPADDR1="192.168.1.151"
IPADDR2="192.168.1.152"
PREFIX0="24"
GATEWAY0="192.168.1.1"
DNS1="192.168.1.1"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"

As you can see, I have added two IP addresses: IPADDR1 = "192.168.1.151" & ipaddr2 = "192.168.1.152"

Similarly, you can add more ip addresses.

Finally, save and exit the file. Restart the network service for the changes to take effect.

systemctl restart network

Now, let's check whether the ip address has been added.

ip addr

Sample output:

: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:3f:ab:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.1.151/24 brd 192.168.1.255 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.1.152/24 brd 192.168.1.255 scope global secondary enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe3f:ab68/64 scope link 
       valid_lft forever preferred_lft forever

As you can see, a single network card already has three ip addresses.

Let's ping the new IP address:

ping -c 4 192.168.1.151

Sample output:

PING 192.168.1.151 (192.168.1.151) 56(84) bytes of data.
64 bytes from 192.168.1.151: icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from 192.168.1.151: icmp_seq=2 ttl=64 time=0.075 ms
64 bytes from 192.168.1.151: icmp_seq=3 ttl=64 time=0.077 ms
64 bytes from 192.168.1.151: icmp_seq=4 ttl=64 time=0.077 ms
--- 192.168.1.151 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.048/0.069/0.077/0.013 ms
ping -c 4 192.168.1.152

Sample output:

PING 192.168.1.152 (192.168.1.152) 56(84) bytes of data.
64 bytes from 192.168.1.152: icmp_seq=1 ttl=64 time=0.034 ms
64 bytes from 192.168.1.152: icmp_seq=2 ttl=64 time=0.075 ms
64 bytes from 192.168.1.152: icmp_seq=3 ttl=64 time=0.073 ms
64 bytes from 192.168.1.152: icmp_seq=4 ttl=64 time=0.075 ms
--- 192.168.1.152 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.034/0.064/0.075/0.018 ms

If you want to use different subnets, you should change PREFIX0=24 to different subnets, such as PREFIX1=16.

For example, I want to add A class A address (* such as 10.0.0.1) to my network card.

TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="enp0s3"
UUID="933cdc9b-b383-4ddd-b219-5a72c69c9cf0"
ONBOOT="yes"
HWADDR="08:00:27:3F:AB:68"
IPADDR0="192.168.1.150"
IPADDR1="192.168.1.151"
IPADDR2="192.168.1.152"
IPADDR3="10.0.0.1"
PREFIX0="24"
PREFIX1=16
GATEWAY0="192.168.1.1"
DNS1="192.168.1.1"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"

You can see that I have added A class A address (10.0.0.1) and the prefix is 16.

Save and exit the file. Restart the network service, and then ping the new address:

ping -c 4 10.0.0.1

Sample output:

PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.073 ms
64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=0.074 ms
64 bytes from 10.0.0.1: icmp_seq=4 ttl=64 time=0.075 ms
--- 10.0.0.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.073/0.079/0.097/0.014 ms