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