Centos 7 Single NIC Binding IP Segment Method - Tech

  1. Portal Home
  2. Tech

Centos 7 Single NIC Binding IP Segment Method

There are several different ways to enable IP addresses on CentOS / Redhat 7.x. In this guide, we will provide you with instructions and examples for configuring a small number of additional IP addresses, entire CIDR network prefixes (blocks/ranges), and ways to remove them.

Determine the name of the network device
Before we configure or remove any other IP addresses, we are going to determine the device name of our primary network interface, which can be discovered by running the following command in our terminal.

[root@localhost ]# ip link | awk 'NR%2==1' | awk '{print $2,$8,$9}' | tr -d ':'

The output shall be similar to the following.

lo state UNKNOWN
enp3s0 state UP
enp4s0 state DOWN

    The first device in the above example is named lo, which means it is your return interface. The second line of output is usually your primary network interface, in this example. Its state is currently the only UP (active/online) state, so we can assume this is the correct interface to configure as another DOWN (disconnected/inactive). Another interface (e.g. enp4s0) should be configured for UP, and then you will need to decide on which interface to configure additional IP space.

Find Network Configuration Files
Using the output from ip link's earlier commands, we can now find the appropriate network configuration files network-scripts in our directory. interface configuration files begin with the device name of the ifcfg-interface. So let's see how everything enp3s0 currently configured on our interface looks (replace it with the name of your interface).

[root@localhost ]# cat /etc/sysconfig/network-scripts/ifcfg-enp3s0

Your output should look similar to the following.

TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp3s0
UUID=a007fd6d-4cc5-45b6-9a38-991a8e820eaf
DEVICE=enp3s0
ONBOOT=yes
IPADDR=10.0.0.2
PREFIX=29
GATEWAY=10.0.0.1
DNS1=8.8.8.8
DNS2=8.8.4.4
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

In the above example, the currently configured unique IP address is 10.0.0.2. It is part of the / 29 prefix (PREFIX=29), which can also be configured for IPs 10.0.0.3, 10.0.0.4, 10.0.0.5, and 10.0.0.6.

If you intend to configure multiple IP addresses on a network interface, you may need to use a scope file to do so. The scope file allows you to configure the entire CIDR network prefix (/ 29, / 24, etc.) so you do not have to configure each IP address individually. Continue to the Scope File section or skip to the section for manually added IP addresses if you only need to enable a small number of IP addresses.

scoping paper
First, we update the configuration file for our primary network interface (replace it with the name of your interface) with.

[root@localhost ]# vi /etc/sysconfig/network-scripts/ifcfg-enp3s0

Now, attach the following text to the end of the file and save it: NM_CONTROLLED=NO

For enabling scope files, Redhat / CentOS 7.x requires this configuration change. It simply allows us to utilize the scope file by making the interface no longer under the control of the Network Manager system. After making the above changes and saving the file, we can proceed to create our scope file by.

[root@localhost ]# vi /etc/sysconfig/network-scripts/ifcfg-enp3s0-range

Add the following text lines to the new configuration file (ifcfg-enp3s0-range). Replace the settings for each parameter with your own unique network configuration (IP, prefix, etc.).

IPADDR_START=192.168.1.2 - The first available IP address in the range.

IPADDR_END=192.168.1.254 - The last available IP address in the range.

PREFIX=24 - The CIDR prefix for the block/range of IPs. (See the CIDR prefix table below to determine the correct prefix number for your IP range.)

CLONENUM_START=0 - The interface alias starting number, which will automatically add an IP address for each additional IP address bound to the interface by creating an alias for it (e.g. enp3s0:0, enp3s0:1, etc.). If you have multiple range files, you will need to ensure that this number is set to the next available number (e.g. if you have configured / 24 as described here).

It should look similar to the following scoping document.

IPADDR_START=192.168.1.2
IPADDR_END=192.168.1.254
PREFIX=24
CLONENUM_START=0
/ 32	192.168.1.90/32		1
/ 31	192.168.1.90/31		2
/ 30	192.168.1.92/30		4
/ 29	192.168.1.200/29	8
/ 28	192.168.1.16/28		16
/ 27	192.168.1.64/27		32
/ 26	192.168.1.192/26	64
/ 25	192.168.1.128/25	128
/ 24	192.168.1.0/24		256

In the example scope file above, we configure the / 24 prefix, where the first IP address (192.168.1.0) is a Network address, the second is a Gateway address (192.168.1.1), and the last IP address is a Broadcast address (192.168.1.255). These three IP addresses are "unavailable" in the sense that they need to be configured in this way for the rest of the IP addresses to work properly. Alternatively, you can configure a different Gateway if you need to use different defaults. In that case, you can also simply append GATEWAY=YOURGATEWAYADDRESS to the file. The current default Gateway address can be determined by running the following command.

[root@localhost ~]# ip route | grep default
default via 192.168.1.1 dev enp3s0

We double check that we entered the correct information and that there are no errors in our scope file. This is important because the next step may result in your server being inaccessible. After confirming the order of everything, restart your web service for the changes to take effect.

[root@localhost ]# systemctl restart network

Delete IP address
Now that we know how to add additional IP addresses, let's quickly learn how to remove them. To delete a single IP address, you can issue the following command.

[root@localhost ]# ip addr del [ipaddress] dev [network_device_name]

Example.

[root@localhost ]# ip addr del 192.168.1.72/32 dev enp3s0

If you want to delete the entire range, you can refresh the IP using the following command.

[root@localhost ]# ip -s a f to [CIDRPrefix]

Example.

[root@localhost ]# ip -s a f to 192.168.1.0/24