了解“ ip addr change”和“ ip addr replace”命令


10

目前,我有以下与eth0接口关联的IPv4地址:

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:~ # 

我想将10.11.12.3/24地址替换或更改为10.11.12.6/24。这该怎么做?如果执行ip addr change 10.11.12.6/24 dev eth0ip addr replace 10.11.12.6/24 dev eth0则将10.11.12.6添加到eth0中。另外,在更改/替换的情况下,语法是否应为change/replace from 10.11.12.3/24 to 10.11.12.6/24?我知道我可以使用ip addr del命令,但是如何使用ip addr changeor 来实现ip addr replace呢?

Answers:


19

(我意识到这是一个老问题,但谷歌带我来这里是因为我想弄清楚到底是什么changereplace做,他们是如何不同)。

我相信replacechange都用于修改现有地址。考虑:

ip addr add 192.168.1.10/32 dev 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 dummy0
       valid_lft forever preferred_lft forever
    inet6 fe80::58ec:58ff:fe4f:d135/64 scope link 
       valid_lft forever preferred_lft forever

如果再次运行相同的命令,则会出现错误:

# ip addr add 192.168.111.10/32 dev dummy0 
RTNETLINK answers: File exists

如果要修改该地址上的标志,可以使用changereplace。在这里,我ip addr change用来修改该地址上的preferred_lftvalid_lft设置:

# 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

的行为ip addr replace相同。实际上,如果您看一下代码,它们将导致几乎相同的动作:

    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);

看起来这里的意图是change仅修改现有地址,而replace修改现有地址或在指定地址不存在的情况下创建一个新地址。在实践中,似乎两个地址都将添加,如果地址不存在changereplace则会添加该地址。

如果您确实想添加一个新地址并删除一个旧地址,则需要分两步进行操作,使用ip addr del后跟ip addr add(当然也可以采用另一种方法)。


感谢您的回答。它为我的生产环境节省了大量的停机时间(如果只有我早发现的话,我可能会遇到: NO P0()
Samveen
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.