如何使用ip命令更改网络掩码

如何使用ip命令更改网络掩码

我尝试使用 Linux ip 命令更改网络掩码,但很困惑。谷歌搜索也没有澄清这一点。如果有人可以解释一下吗?说我有界面enp0s31f6在我的 Linux 机器上

rtuser@rtuser:~$ ip addr show dev enp0s31f6
2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff

我想为接口分配IP地址:

rtuser@rtuser:~$ sudo ip addr add 192.168.20.1/24 dev enp0s31f6
rtuser@rtuser:~$ ip addr show dev enp0s31f6
2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.20.1/24 scope global enp0s31f6
       valid_lft forever preferred_lft forever

现在我意识到我的网络掩码不正确,我想更改

rtuser@rtuser:~$ sudo ip addr change 192.168.20.1/25 dev enp0s31f6
rtuser@rtuser:~$ ip addr show dev enp0s31f6
2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.20.1/24 scope global enp0s31f6
       valid_lft forever preferred_lft forever
    inet 192.168.20.1/25 scope global enp0s31f6
       valid_lft forever preferred_lft forever

我在接口上有几个相同的 IP 地址。好吧,也许改变不起作用,让我们尝试更换它

rtuser@rtuser:~$ sudo ip addr replace 192.168.20.1/26 dev enp0s31f6
rtuser@rtuser:~$ ip addr show dev enp0s31f6
2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.20.1/24 scope global enp0s31f6
       valid_lft forever preferred_lft forever
    inet 192.168.20.1/25 scope global enp0s31f6
       valid_lft forever preferred_lft forever
    inet 192.168.20.1/26 scope global enp0s31f6
       valid_lft forever preferred_lft forever

我希望更改/替换 ip 命令会更改 ip 地址的属性,但似乎它们都添加/更改/替换不断地一遍又一遍地添加相同的 ip。这是为什么?

答案1

只需使用 del 选项删除原始的错误条目即可。

ip address del 192.168.20.1/24 dev enp0s31f6

然后添加修正后的IP/CIDR:

ip address add 192.168.20.1/25 dev enp0s31f6

相关内容