Adding an IP address to an existing interface

Hi, Everyone.

Please help to a problem that I am encountering in my lab environment. I am trying to follow a tutorial where the instructor added an additional IP address to an existing interface. He used nmcli to add this, here is the command that I executed:

nmcli connection add con-name home ifname eth1 type ethernet ip4 192.168.56.200 gw4 192.168.56.1

I have checked if it was added using “nmcli connection show” and it was added:

[root@server1 /etc/sysconfig/network-scripts]# nmcli connection show
NAME UUID TYPE DEVICE
ETH0 67525a21-0044-3e7f-be76-41382b7b8958 ethernet eth0
ETH1 bed5083e-565b-3aeb-9b6d-04c971804cf4 ethernet eth1
home 209c0032-05d3-4325-9d16-4602952773cb ethernet –

But under the device column for home it is empty, if I do “ip address show home” it return empty.

Please help me identify what I have missed

That command creates a new interface. I tested those in a Fedora LXC which was a bit glitchy, but I think what you are looking for is this:

nmcli con show
# grab your connection name, I'll assume it's Wired connection 1

nmcli con mod "Wired connection 1" +ipv4.address "192.168.56.200"  +ipv4.gateway "192.168.56.1"
nmcli con mod "Wired connection 1" ipv4.gateway "<current-gateway-not-56.1>"
# this is to keep your current default gateway as default
nmcli con reload "Wired connection 1"
# if reload doesn't work, do a down and up
nmcli con down "Wired connection 1"
nmcli con up "Wired connection 1"

You might need to mess with the routes and metrics after this. I prefer ip commands, as I hate nmcli when it comes to multiple interfaces and multiple IP addresses. If I need to re-add IP addresses, I just do it in a crontab @reboot, or via rc.local, or even in a process supervisor config (systemd unit file, runit run file, s6-rc oneshot service etc.)

For CentOS and any Fedora / RHEL forks, I use /etc/sysconfig/network-scripts/ifcfg-[interface] and for Debian, /etc/network/interfaces. Ubuntu, I may mess with netplan, but I haven’t really used netplan a lot, especially not for anything more than 1 interface and 1 IP or DHCP. Which is a bit sad, because netplan seems promising, because it can revert its network settings automatically if you don’t confirm the screen (if you lose network connectivity), which is a godsent when doing stuff via ssh.

Anyway, good luck on your learning.

1 Like

@Biky thank you so much for sharing that information, I will definitely try this later.

1 Like

Lets say if I have 2 NICS on the CentOS server and it is already configured to be used by ifcfg-ETH0 and ifcfg-ETH1. Can I create another interface using nmcli conn add? The new interface, can I point it to one of those NICS already used by ifcfg-ETH0 and ETH1?

You can create a new connection and point it to the same interface, if you just want to have different profiles for different places. In the example you gave, you probably needed a subnet mask. Following that, even if the device is empty, the connection is not.

Try:

nmcli con add con-name home ifname eth1 type ethernet ip4 192.168.56.200/24 gw4 192.168.56.1
nmcli con down "Wired connection 1"
nmcli con up "home"
ip a
nmcli con sho

This should leave you with 2 profiles: “wired connection 1” and “home” which you can stop and switch between.

1 Like

Thank you so much for sharing the information. I followed your instruction and it all worked for me.

1 Like