understanding "ip addr change" and "ip addr replace" commands
(I realize this is an old question, but Google brought me here because I was trying to figure out exactly what change
and replace
do and how they are different).
I believe that both replace
and change
are used for modifying an existing address. Consider:
ip addr add 192.168.1.10/32 dev dummy0
This gets me:
32: dummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default
link/ether 5a:ec:58:4f:d1:35 brd ff:ff:ff:ff:ff:ff
inet 192.168.111.10/32 scope global dummy0
valid_lft forever preferred_lft forever
inet6 fe80::58ec:58ff:fe4f:d135/64 scope link
valid_lft forever preferred_lft forever
If I run the same command again, I get an error:
# ip addr add 192.168.111.10/32 dev dummy0
RTNETLINK answers: File exists
If I want to modify the flags on that address, I can use either change
or replace
. Here, I use ip addr change
to modify the preferred_lft
and valid_lft
settings on that address:
# ip addr change 192.168.111.10/32 dev dummy0 preferred_lft 300 valid_lft 300
# ip addr show dummy0
32: dummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default
link/ether 5a:ec:58:4f:d1:35 brd ff:ff:ff:ff:ff:ff
inet 192.168.111.10/32 scope global dynamic dummy0
valid_lft 298sec preferred_lft 298sec
inet6 fe80::58ec:58ff:fe4f:d135/64 scope link
valid_lft forever preferred_lft forever
The behavior of ip addr replace
is identical. In fact, if you look at the code, they result in almost identical actions:
if (matches(*argv, "change") == 0 ||
strcmp(*argv, "chg") == 0)
return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1);
if (matches(*argv, "replace") == 0)
return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
It looks like the intention here is that change
will only modify an existing address, while replace
will either modify an existing address or create a new one if the specified address does not exist. In practice, it seems as if both change
and replace
will add the address if it does not already exist.
If you actually want to add a new address and remove an old one, you will need to do that in two steps, using ip addr del
followed by ip addr add
(or the other way around, of course).