Introduction
Write an introduction on How to Assign Multiple IPs to a Single Network Interface in Linux
How to Assign Multiple IPs to a Single Network Interface in Linux
How to Assign Multiple IPs to a Single Network Interface in Linux
The concept of creating or configuring multiple IP addresses on a single network interface is called IP aliasing, which is very useful for setting up multiple virtual sites on a web server using one single network interface with different IP addresses on a single subnet network.
The main advantage of using this IP aliasing is that you don’t need to have a physical adapter attached to each IP. Instead, you can create multiple or many virtual interfaces (aliases) for a single physical card.
The instructions given here apply to all RHEL-based distributions such as Fedora, CentOS, Rocky, and Alma Linux. Creating multiple interfaces and assigning IP addresses to them manually is a daunting task.
Here, we’ll see how to assign IP addresses by defining a set of IP ranges. We’ll also understand how to create a virtual interface and assign a different range of IP addresses to it all at once.
In this article, we used LAN IPs, so replace those with the ones you will be using.
How to Create Multiple IP Addresses to Single Network Interface
Here, I have an interface named ‘ifcfg-eth0‘, which is the default network interface card for the ethernet device. If you’ve connected a second ethernet device, it will appear as ‘ifcfg-eth1‘, and so on for each additional device.
These network files for the devices are found in the ‘/etc/sysconfig/network-scripts/‘ directory and you can list all devices using the following ls command.
ls -l /etc/sysconfig/network-scripts/
Sample Output:
ifcfg-eth0 ifdown-isdn ifup-aliases ifup-plusb init.ipv6-global ifcfg-lo ifdown-post ifup-bnep ifup-post net.hotplug ifdown ifdown-ppp ifup-eth ifup-ppp network-functions ifdown-bnep ifdown-routes ifup-ippp ifup-routes network-functions-ipv6 ifdown-eth ifdown-sit ifup-ipv6 ifup-sit ifdown-ippp ifdown-tunnel ifup-isdn ifup-tunnel ifdown-ipv6 ifup ifup-plip ifup-wireless
Let’s assume that we want to create three additional virtual interfaces to bind three IP addresses (172.16.16.126, 172.16.16.127, and 172.16.16.128) to the NIC.
So, we need to create three additional alias files, while “ifcfg-eth0” keeps the same primary IP address. This is how we move forward to set up three aliases to bind the following IP addresses.
Adapter IP Address Type ------------------------------------------------- eth0 172.16.16.125 Primary eth0:0 172.16.16.126 Alias 1 eth0:1 172.16.16.127 Alias 2 eth0:2 172.16.16.128 Alias 3
Where ":X"
represents the device (interface) number for creating aliases for interface eth0. For each alias, you must assign a number sequentially.
For example, we copy existing parameters from the interface “ifcfg-eth0” to virtual interfaces named ifcfg-eth0:0, ifcfg-eth0:1, and ifcfg-eth0:2.
Navigate to the network directory and create the files as shown below.
cd /etc/sysconfig/network-scripts/ cp ifcfg-eth0 ifcfg-eth0:0 cp ifcfg-eth0 ifcfg-eth0:1 cp ifcfg-eth0 ifcfg-eth0:2
Open a file “ifcfg-eth0” and view the contents.
vi ifcfg-eth0
Sample Output:
DEVICE="eth0" BOOTPROTO=static ONBOOT=yes TYPE="Ethernet" IPADDR=172.16.16.125 NETMASK=255.255.255.224 GATEWAY=172.16.16.100 HWADDR=00:0C:29:28:FD:4C
Here, we only need two parameters (DEVICE and IPADDR). So, open each file with your favorite editor, rename the DEVICE name to its corresponding alias, and change the IPADDR address.
For example, open files ‘ifcfg-eth0:0‘, ‘ifcfg-eth0:1‘, and ‘ifcfg-eth0:2‘ using the VI editor and update both parameters.
Finally, it will look similar to the example below.
ifcfg-eth0:0
DEVICE="eth0:0" BOOTPROTO=static ONBOOT=yes TYPE="Ethernet" IPADDR=172.16.16.126 NETMASK=255.255.255.224 GATEWAY=172.16.16.100 HWADDR=00:0C:29:28:FD:4C
ifcfg-eth0:1
DEVICE="eth0:1" BOOTPROTO=static ONBOOT=yes TYPE="Ethernet" IPADDR=172.16.16.127 NETMASK=255.255.255.224 GATEWAY=172.16.16.100 HWADDR=00:0C:29:28:FD:4C
ifcfg-eth0:2
DEVICE="eth0:2" BOOTPROTO=static ONBOOT=yes TYPE="Ethernet" IPADDR=172.16.16.128 NETMASK=255.255.255.224 GATEWAY=172.16.16.100 HWADDR=00:0C:29:28:FD:4C
Once, you’ve made all changes, save all your changes and restart/start the network service for the changes to reflect.
systemctl restart network
To verify all the aliases (virtual interface) are up and running, you can use “ifconfig” or “ip” command.
ifconfig
Sample Output:
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.125 Bcast:172.16.16.100 Mask:255.255.255.224 inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:237 errors:0 dropped:0 overruns:0 frame:0 TX packets:198 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:25429 (24.8 KiB) TX bytes:26910 (26.2 KiB) Interrupt:18 Base address:0x2000 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.126 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.127 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000 eth0:2 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.128 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000
Now try to ping each of them from a different machine. If everything is set up correctly, you will receive a ping response from each of them.
ping 172.16.16.126 ping 172.16.16.127 ping 172.16.16.128
Sample Output:
ping 172.16.16.126 PING 172.16.16.126 (172.16.16.126) 56(84) bytes of data. 64 bytes from 172.16.16.126: icmp_seq=1 ttl=64 time=1.33 ms 64 bytes from 172.16.16.126: icmp_seq=2 ttl=64 time=0.165 ms 64 bytes from 172.16.16.126: icmp_seq=3 ttl=64 time=0.159 ms ping 172.16.16.127 PING 172.16.16.127 (172.16.16.127) 56(84) bytes of data. 64 bytes from 172.16.16.127: icmp_seq=1 ttl=64 time=1.33 ms 64 bytes from 172.16.16.127: icmp_seq=2 ttl=64 time=0.165 ms 64 bytes from 172.16.16.127: icmp_seq=3 ttl=64 time=0.159 ms ping 172.16.16.128 PING 172.16.16.128 (172.16.16.128) 56(84) bytes of data. 64 bytes from 172.16.16.128: icmp_seq=1 ttl=64 time=1.33 ms 64 bytes from 172.16.16.128: icmp_seq=2 ttl=64 time=0.165 ms 64 bytes from 172.16.16.128: icmp_seq=3 ttl=64 time=0.159 ms
It appears that everything is working smoothly. With these new IPs, you can set up virtual sites in Apache, FTP accounts, and many other things.
Assign Multiple IP Addresses Range
If you would like to create a range of multiple IP addresses to a particular interface called “ifcfg-eth0“, we use “ifcfg-eth0-range0” and copy the contents of ifcfg-eth0 on it as shown below.
cd /etc/sysconfig/network-scripts/ cp -p ifcfg-eth0 ifcfg-eth0-range0
Now open the “ifcfg-eth0-range0” file.
vi ifcfg-eth0-range0
and add “IPADDR_START” and “IPADDR_END” IP address ranges as shown below.
#DEVICE="eth0" #BOOTPROTO=none #NM_CONTROLLED="yes" #ONBOOT=yes TYPE="Ethernet" IPADDR_START=172.16.16.126 IPADDR_END=172.16.16.130 IPV6INIT=no #GATEWAY=172.16.16.100
Save it and restart/start the network service
systemctl restart network
Verify that virtual interfaces are created with IP Addresses.
ifconfig
Sample Output:
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.125 Bcast:172.16.16.100 Mask:255.255.255.224 inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1385 errors:0 dropped:0 overruns:0 frame:0 TX packets:1249 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:127317 (124.3 KiB) TX bytes:200787 (196.0 KiB) Interrupt:18 Base address:0x2000 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.126 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.127 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000 eth0:2 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.128 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000 eth0:3 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.129 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000 eth0:4 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C inet addr:172.16.16.130 Bcast:172.16.16.100 Mask:255.255.255.224 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:18 Base address:0x2000
By following these methods, you can effectively create and manage multiple IP addresses on a single network interface in Red Hat Enterprise Linux systems. This flexibility enables you to optimize network communication and connectivity according to your specific requirements.
generate an article with html tags on How to Assign Multiple IPs to a Single Network Interface in Linux