如何為現有介面設定額外的IP?

如何為現有介面設定額外的IP?

我有一個接口eth0,我想給它一個額外的虛擬IP。我透過以下方式實現這一目標:

ifconfig eth0:0 ip.address.goes.here netmask subnet.address.goes.here

這工作正常,但是,當我重新啟動時,它就會丟失。

我嘗試編輯/etc/network/interfaces添加以下內容:

auto eth0:0 iface eth0:0 inet static
    address ip.address.goes.here
    netmask subnet.address.goes.here

但是,重新啟動後,eth0 的靜態 IP 可以正常加載,但是 eth0:0 虛擬 IP 根本沒有加載。

那麼,如何永久加入eth0:0虛擬IP呢?

答案1

eth0:0你應該這樣做,而不是做那件事:

  • /etc/network/interfaces像平常一樣配置您的(一個)靜態 IP 位址:

    # The primary network interface
    auto eth0
    iface eth0 inet static
    address 192.168.0.201
    network 192.168.0.0
    netmask 255.255.255.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
    
  • 新增另一個IP在上面的後面加入這個介面:

    up /sbin/ip addr add 192.168.0.203/24 dev eth0
    down /sbin/ip addr del 192.168.0.203/24 dev eth0
    
  • 完整的文件應該喜歡

現在,如果您透過執行檢查配置了哪些 IP 位址ip addr show,兩者都會顯示:

2: eth0: mtu 1500 qdisc pfifo_fast 狀態 UP qlen 1000
    連結/以太 08:00:27:1d:fa:0b brd ff:ff:ff:ff:ff:ff
    內網192.168.0.201/24brd 192.168.0.255 範圍 全域 eth0
    內網192.168.0.203/24範圍 全域輔助 eth0

感謝萊肯斯坦為我指明了正確的方向。網路上的每個網站都只是談論eth0:0輔助 IP 位址。似乎是正確的方法。

答案2

如果您想以「傳統」方式做事,相關部分/etc/network/interfaces應如下所示:

auto eth0:0
iface eth0:0 inet static
    address ip.address.goes.here
    netmask subnet.address.goes.here

而不是這個,你犯了一個錯誤:

auto eth0:0 iface eth0:0 inet static
    address ip.address.goes.here
    netmask subnet.address.goes.here

相關內容