21

At the moment I have following IPv4 addresses associated with eth0 interface:

T60:~ # ip -V
ip utility, iproute2-ss110629
T60:~ # ip addr show dev eth0
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:15:58:2a:84:3e brd ff:ff:ff:ff:ff:ff
    inet 10.11.12.2/24 scope global eth0
    inet 10.11.12.3/24 scope global secondary eth0
    inet 10.11.12.4/24 scope global secondary eth0
    inet6 fe80::215:58ff:fe2a:843e/64 scope link 
       valid_lft forever preferred_lft forever
T60:~ # 

I would like to replace or change 10.11.12.3/24 address to 10.11.12.6/24. How to do this? If I execute ip addr change 10.11.12.6/24 dev eth0 or ip addr replace 10.11.12.6/24 dev eth0 then 10.11.12.6 is just added to eth0. In addition, in case of change/replace, shouldn't the syntax be change/replace from 10.11.12.3/24 to 10.11.12.6/24? I know that I can use ip addr del command, but how to achieve this with ip addr change or ip addr replace?

1 Answer 1

36

(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.111.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).

2
  • 1
    Thank you for this answer. It saved my production environment a ton of downtime (if only I had found it earlier, I could've had NO P0s :( )
    – Samveen
    May 10, 2017 at 6:33
  • Fyi, NLM_F_CREATE and NLM_F_REPLACE are described on this page. I don't know what any of the description means (because I don't know what a "qdisc" is), but here it is: NLM_F_CREATE Create qdisc if it does not exist, otherwise -NLE_OBJ_NOTFOUND is returned. NLM_F_REPLACE If another qdisc is already attached to the same parent and their handles mismatch, replace the qdisc instead of returning -EEXIST. NLM_F_EXCL Return -NLE_EXISTS if a qdisc with matching handles exists already.
    – mpb
    Oct 10, 2022 at 5:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .