Centos 7 - Add secondary IPs Print

  • 122

In Centos 7 there is no more use for the ifconfig command. instead it is the 'ip' command.

$ ip a | grep 'inet '
    inet scope host lo
    inet brd scope global dynamic eth0
    inet brd scope global dynamic eth1

This syntax is more inline with most routers/switches, where you can grep for inet and inet6 for your IPv4 and IPv6 IP addresses.

$ ip a | grep 'inet6 '
    inet6 ::1/128 scope host
    inet6 fe80::a00:27ff:fe19:cd16/64 scope link
    inet6 fe80::a00:27ff:fefd:6f54/64 scope link

Using Network Manager

Check if your interface you want to add an alias to, uses the Network Manager.

$ grep 'NM_CONTROLLED' /etc/sysconfig/network-scripts/ifcfg-ens160

If that's a yes, you can proceed with the next configurations using the Network Manager tool.

You may be used to adding a new network-scripts file in /etc/sysconfig/network-scripts/, but you'll find that doesn't work in RHEL / CentOS 7 as you'd expect if the Network Manager is being used. Here's what a config would look like in CentOS 6:

$ cat ifcfg-ens160:0

After a network reload, the primary IP address will be removed from the server and only the IP address from the alias interface will be present. That's not good. That's the Network Manager misinterpreting your configuration files, overwriting the values from your main interface with the one from your alias.

The simplest/cleanest way to add a new IP address to an existing interface in CentOS 7 is to use the nmtui tool (Text User Interface for controlling NetworkManager).

$ nmtui


Once nmtui is open, go to the Edit a network connection and select the interface you want to add an alias on.


Click Edit and tab your way through to Add to add extra IP addresses.


Save the configs and the extra IP will be added.

If you check the text-configs that have been created in /etc/sysconfig/network-scripts/, you can see how nmtuihas added the alias.

$ cat /etc/sysconfig/network-scripts/ifcfg-ens192
# Alias on the interface

If you want, you can modify the text file, but I find using nmtui to be much easier.

Manually Configuring An Interface Alias

Only use this if your interface is not controlled by Network Manager.

$ grep 'NM_CONTROLLED' /etc/sysconfig/network-scripts/ifcfg-ens160

If Network Manager isn't used, you can use the old style aliases you're used to from CentOS 5/6.

$ cat ifcfg-ens160:0

Bring up your alias interace and you're good to go.

$ ifup ens160:0

Don't use this if Network Manager is in control.

Adding a temporary IP address

Want to add an IP address just for a little while? You can add one using the ip command. It only lasts until you reboot your server or restart the network service, after that -- the IP is gone from the interface.

$ ip a add dev eth0

Perfect for temporary IPs!

Post originally found at: https://ma.ttias.be/how-to-add-secondary-ip-alias-on-network-interface-in-rhel-centos-7

Was this answer helpful?

« Back