
大家好,我正在嘗試從一組可用 IP 中刪除特定 IP,該怎麼做?例如,假設我有以下 IP(序列相同): 1.2.3.4 1.2.3.5 1.2.3.6 我使用“ifconfig etho:x 1.2.3.x up”執行此操作,其中 x=4,5,6。現在我想單獨關閉第一個IP,即1.2.3.4,我嘗試將此作為「ifconfig eth0:4 down」。問題是,這不僅會導致所需的 IP 下降,還會導致其下方的所有 IP 下降。我正在 Linux RedHat 上工作。提前致謝...
答案1
ip addr del 1.2.3.4/24 dev eth0:4
它們eth0:n
並不是真正的接口,它們被稱為標籤(或別名)。由於您可以將多個位址分配給同一接口,因此標籤可以幫助區分 IP 的用途。這也解釋了為什麼當你設定一個標籤時,其他一切都會隨之而來;因為你實際上只是在與 eth0 交談。
答案2
大多數作業系統允許將多個 IP 位址指派給單一接口,稱為 IP 別名或邏輯網路介面。我們可以將多個 IP 位址與特定標籤/別名相關聯。別名是為了我們自己的方便。
在下面的範例中,標籤與IP 位址eth0:1
相關聯。192.168.1.1 to 192.168.1.3
且eth0:2
標籤與 相關聯192.168.1.4 to 192.168.1.6
。
# ip addr add 192.168.1.1/24 dev eth0 label eth0:1
# ip addr add 192.168.1.2/24 dev eth0 label eth0:1
# ip addr add 192.168.1.3/24 dev eth0 label eth0:1
# ip addr add 192.168.1.4/24 dev eth0 label eth0:2
# ip addr add 192.168.1.5/24 dev eth0 label eth0:2
# ip addr add 192.168.1.6/24 dev eth0 label eth0:2
$ ip addr ls dev eth0
inet 192.168.1.1/24 scope global eth0:1
inet 192.168.1.2/24 scope global secondary eth0:1
inet 192.168.1.3/24 scope global secondary eth0:1
inet 192.168.1.4/24 scope global secondary eth0:2
inet 192.168.1.5/24 scope global secondary eth0:2
inet 192.168.1.6/24 scope global secondary eth0:2
我們可以將一個特定的 IP 位址與一個介面關聯,或將多個 IP 位址與一個別名關聯。
以下命令列出了與標籤關聯的所有 IP 位址eth0:1
。
$ ip addr ls label eth0:1
inet 192.168.1.1/24 scope global eth0:1
inet 192.168.1.2/24 scope global secondary eth0:1
inet 192.168.1.3/24 scope global secondary eth0:1
我們可以刪除特定的IP位址。
$ sudo ip addr del 192.168.1.3/24 dev eth0
$ sudo ip addr ls label eth0:1
inet 192.168.1.1/24 scope global eth0:1
inet 192.168.1.2/24 scope global secondary eth0:1