Change Linux Primary IP Address Without Losing Connectivity

You need to change an IP address of an interface on a Linux server without loosing connectivity, so here’s one way:

First add a secondary IP address to the interface:

ip addr add 192.168.1.10/24 dev eth0
ip addr show eth0

And confirm:

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
link/ether 44:38:39:00:11:aa brd ff:ff:ff:ff:ff:ff 
inet 192.168.1.5/24 scope global eth0 
inet 192.168.1.10/24 scope global secondary eth0

Now setup the interface so it will promote the secondary IP address when we remove the first one:

echo 1 > /proc/sys/net/ipv4/conf/eth0/promote_secondaries

or

sysctl net.ipv4.conf.eth0.promote_secondaries=1

** L33T-TIP: Change eth0 to all to have it work on all interfaces.

Last step, remove the original IP address and confirm:

ip addr del 192.168.1.5/24 dev eth0
ip addr show dev eth0
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 44:38:39:00:11:aa brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 scope global eth0

And there ya go !