How to assign an additional/alias IP addresses to a network card (NIC)?

Latest response

Hello folks,
I'm looking for best practice to assign and manage an additional/alias IP addresses to a network card (NIC) in RHEL 7 and RHEL 8.

Most of my network configurations for a host with a single NIC looks something like this:

# cat /etc/sysconfig/network-scripts/ifcfg-ens192
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_PRIVACY=no
NAME=ens192
DEVICE=ens192
ONBOOT=yes
DNS1=192.168.0.1
DNS2=192.168.0.2
PEERDNS=no
DOMAIN="foo.example.com example.com"
IPADDR=192.168.3.56
PREFIX=24
GATEWAY=192.168.3.1

Today when I have to add an additional IP address to this NIC I would create a new file called ifcfg-ens192:1 with the follwoing content:

# cat /etc/sysconfig/network-scripts/ifcfg-ens192\:1
DEVICE=ens192:1
ONPARENT=on
IPADDR=192.168.3.8
PREFIX=24

Afterwards I restart the network service via systemctl restart network to activate the new (virtual) interface. The new interface is active imediatly. How could I prevent this? The new IP should stay inactive until I enable it via ifup ens192\:1.

Besides that, what do you suggest is the recommend way for RHEL 7 and RHEL 8 to bring up and down (virtual) interfaces? I would like to know how to control which IP is UP or DOWN and I'm not sure wether to choose ifup|ifdown, ip or nmcli.

When the additional/virtual interface is down, it is not shown in the output of ifconfig -a and ip a. How could I get a view of all virtual interfaces in status DOWN?

I'm looking forward reading your suggestions.

Best regards,
Joerg

Responses

Those sort of interfaces with a colon like ens192:1 are called "alias interfaces" and have fallen out of favor. There are a few obscure situations where they don't work, it is recommended to move away from them.

The replacement is simply adding IP addresses as secondary IPs on the original interface.

In ifcfg files with the network initscript, this is done with IPADDRn and similar arguments where n is a number below 255, for example:

IPADDR1=10.0.0.1
PREFIX1=24
IPADDR2=172.16.0.1
PREFIX2=28

or you can use NETMASKn as below:

IPADDR1=10.0.0.1
NETMASK1=255.255.255.0
IPADDR2=172.16.0.1
NETMASK2=255.255.255.240

If you're using NetworkManager, then these can be added easily in the GUI or nmtui, or using nmcli like:

nmcli con mod ethX ipv4.addresses "10.0.0.1/24,172.16.0.1/28"

On RHEL7 and RHEL8 I think you're better to stick with either NetworkManager or the network initscripts. Don't use both at once, so don't mix up the commands like run ifup/ifdown while running NM. Use the proper NM equivalent like nmcli con up ethX and so on.

If you want to change config files under NetworkManager instead of editing the connection properties, then run nmcli con reload to read the new config files. Don't restart the NetworkManager service, it's not designed to be restarted after boot.

Also the old net-tools package has been deprecated for a very long time, instead of ifconfig consider using the equivalent ip commands like ip addr or ip route.

Hello Jamie,
Thanks for your response.

I undestand how to assign a secondary or third IP address to an interface. What I did not understand yet is how to control whether the additional IP address is up or down?

Would the additional IP addresses be active on boot? If yes, how to prevent that? How to enable/disable the additional addresses on demand? Could you give me an example?

Thank you in advance
Joerg

Hello to everyone,
I still got a little stuck here. So I hope someone could give some examples in how to deal with the following topics, so I could hopefully understand them.

I undestand how to assign a secondary or third IP address to an interface. What I did not understand yet is how to control whether the additional IP address is up or down?

Would the additional IP addresses be active on boot? If yes, how to prevent that? How to enable/disable the additional addresses on demand? Could you give me an example?

Is anybody out there who could give me a helping hand here?

Best regards,
Joerg

Hi perhaps I can get help here. I have 2 redhat 7.6 servers. I just added IP aliases to both. Both new IPs are the same vlan however although they both appear active only one is responding from outside the server . this is the working one:

cat ifcfg-bond0:1 DEVICE=bond0:1 NAME=bond0:1 TYPE=Bond BONDING_MASTER=yes IPADDR=159.244.136.40 NETMASK=255.255.255.0 ONBOOT=yes BOOTPROTO=none BONDING_OPTS="mode=4 miimon=100 lacp_rate=1" NM_CONTROLLED="no"

this is the one that is not working: cat ifcfg-bond0:1 DEVICE=bond0:1 NAME=bond0:1 TYPE=Bond BONDING_MASTER=yes IPADDR=145.55.17.50 NETMASK=255.255.255.0 ONBOOT=yes BOOTPROTO=none BONDING_OPTS="mode=4 miimon=100 lacp_rate=1" NM_CONTROLLED="no"

hi perhaps this option in the interface config file is what you are looking for:

ONBOOT=yes

Hi Julian,
As far as I know this option is for the interface itself and it's primary ip address, not for the secondary ip address.

Regards,
Joerg

Virtual interfaces are controlled with the ONPARENT setting. Not sure ONPARENT=on is valid. I believe the valid options are yes|no. So to stop it from coming up automatically, you need to set it to no. (ONPARENT=no) if you want it up automatically, set it to yes. (ONPARENT=yes) Article related to RHEL 5. But it applies to 6 and 7 (don't know about 8), if you are using the network scripts: https://access.redhat.com/solutions/169503

Hi,

When using a virutal interface I could simply use ifup/ifdown commands to start and stop it. But as Jamie posted in his comment above so called virtual or alias interfaces have fallen out of favor.

The new world order seems to be adding a secondary ip address on the original interface. But I'm still not sure if this second or maybe even third ip address comes up and down with the first address or if it is possible to control them independent from each other.

Example: When I have an interface with two ip addresses, bringing down the interface takes both ip addresses offline.

What do I have to do to enable/disable only the second or first ip address on an interface?

Best regards, Joerg

I have wondered this myself :P My guess is:

If you don't want an address on the interface at boot, comment it out of the file.

If you wish to put an address down later, you could do so with NetworkManager or ip addr.

"The new world order seems to be adding a secondary ip address on the original interface" <--- I don't think this is correct. The new world order is to use nmcli, but if you are going to be managing your network scripts manually, and you want this behaviour (to manually manage when they come up), then you'd need to either manage them on a separate file, with the ONPARENT option, or, like Jamie says, commenting the lines with the additional IPs on the main configuration file. I've been looking through the documentation and can't find anything about how to manage them with nmcli, except that you just add a list of IPs.. so I get you need to use -g to retrieve all the configured IPs and then reconfigure without the one you don't want.. The advantage of using nmcli is that it's more scriptable than using the files.. but it certainly behaves differently, so you need to be aware of the differences..