![如何使用ip命令更改网络掩码](https://linux22.com/image/178961/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8ip%E5%91%BD%E4%BB%A4%E6%9B%B4%E6%94%B9%E7%BD%91%E7%BB%9C%E6%8E%A9%E7%A0%81.png)
我尝试使用 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