CentOS 7/RHEL7: How To Add Multiple IP Addresses To One Network Interface
How Do I create virtual network interface and add multiple IP addresses to one single network interface under CentOS 7 or RHEL 7 linux? I would like to add another tow ip addresses to network interface “enp0s3”, How to create virtual network interface on one single NIC? this post will show you the ways to add multple ip addresses to one network interface.
You need to create two virtual network interface based on phisical network interface(such as: enp0s3), below I will guide you to create two virtual network interface:enp0s3:0 and enp0s3:1 and set those ip address as: 10.0.2.16 and 10.0.2.17.
CentOS 7 Add Multiple IP Addresses To One Network Interface
Step1# check the current network interface information to get its netowrk device name, issue the following commmand:
ifconfig
or
ip addr
outputs:
[root@devops /]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::a00:27ff:feea:92ea prefixlen 64 scopeid 0x20<link>
ether 08:00:27:ea:92:ea txqueuelen 1000 (Ethernet)
RX packets 203764 bytes 254462850 (242.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 53762 bytes 3283454 (3.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 156 bytes 12452 (12.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 156 bytes 12452 (12.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@devops /]# ip addr
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:ea:92:ea brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
valid_lft 75944sec preferred_lft 75944sec
inet6 fe80::a00:27ff:feea:92ea/64 scope link
valid_lft forever preferred_lft forever
From the above output, we got the network device name as “enp0s3”.
Step2# create two confiuration files of virtual network interface based on configuration file of network interface “enp0s3” by runing the following command:
cp /etc/sysconfig/network-scripts/ifcfg-enp0s3 /etc/sysconfig/network-scripts/ifcfg-enp0s3:0 cp /etc/sysconfig/network-scripts/ifcfg-enp0s3 /etc/sysconfig/network-scripts/ifcfg-enp0s3:1
Step3# you need to edit those above two configuration files of virtual network interface:/etc/sysconfig/network-scripts/ifcfg-enp0s3:0 and /etc/sysconfig/network-scripts/ifcfg-enp0s3:1. using the vim command to edit configuration files and make the following changes for “BOOTPROTO“,”NAME“,”IPADDR“, “NETMASK“,”GATEWAY” configuration file:
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3:0 Device=enp0s3:0 TYPE=Ethernet BOOTPROTO=Static #=====>Set BOOTPROTO From dhcp to static DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=enp0s3:0 IPADDR=10.0.2.16 #=====>set ip address of virtual network interface NETMASK=255.255.255.0 #====>Set network mask GATEWAY=10.0.2.1 #set default gateway address UUID=8d307f87-e025-46cc-87d8-4c4235375768 ONBOOT=yes
For second virtual network interface(enp0s3:1),type:
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3:1 Device=enp0s3:1 TYPE=Ethernet BOOTPROTO=Static DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=enp0s3:1 IPADDR=10.0.2.17 NETMASK=255.255.255.0 GATEWAY=10.0.2.1 UUID=8d307f87-e025-46cc-87d8-4c4235375768 ONBOOT=no
save and close the above files.
Step3# restart network service by running the follwoing command:
systemctl restart network.service
OR
systemctl restart network
Step4# check that if virtual network interfaces are added currectly, issue the following command:
ifconfig
outputs:
[root@devops network-scripts]# ifconfig enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255 inet6 fe80::a00:27ff:feea:92ea prefixlen 64 scopeid 0x20<link> ether 08:00:27:ea:92:ea txqueuelen 1000 (Ethernet) RX packets 203776 bytes 254463660 (242.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 53802 bytes 3287096 (3.1 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp0s3:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.16 netmask 255.255.255.0 broadcast 10.0.2.255 ether 08:00:27:ea:92:ea txqueuelen 1000 (Ethernet) enp0s3:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.2.17 netmask 255.255.255.0 broadcast 10.0.2.255 ether 08:00:27:ea:92:ea txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 156 bytes 12452 (12.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 156 bytes 12452 (12.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
You will see that they are use the same MAC address among “enp0s3“, “enp0s3:0” and “enp0s3:1” network interface.