Linux – How to change netmask with iproute2

iproute2linuxlinux-networking

With ifconfig we can change the netmask with the folowing:

ifconfig eth0 netmask <new netmask>

How can we do this with the command ip?

I know ifconfig is deprecated and this mostly why I would like to do it the iproute2 way.

My objective is to extend our current network from a /24 to a /21 on-the-fly without disturbing services running on our servers. As a workaround currently I simply add a new route to the /21 and eventually a planned network restart will officialize the change. It works but I was wondering if there is a better way.

Thanks,

Best Answer

How about:

ip address add 1.2.3.4/21 dev <device>
ip address del 1.2.3.4/24 dev <device>

(Where 1.2.3.4 is your current IP). ip address show Will list all of the assigned addresses.