Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Ubuntu 單張網卡綁定多個 IP 的設定方法

最近玩 sipp 需要單網卡綁多個 IP,系統是 Ubuntu 12.04,唯一一張網卡為 eth0,設定方法是編輯 /etc/network/interfaces。

修改前

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address 192.168.82.1
    netmask 255.255.0.0
    gateway 192.168.0.1

修改後

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address 192.168.82.1
    netmask 255.255.0.0
    gateway 192.168.0.1

auto eth0:0
iface eth0:0 inet static
    address 192.168.82.2
    netmask 255.255.0.0

auto eth0:1
iface eth0:1 inet static
    address 192.168.82.3
    netmask 255.255.0.0

auto eth0:2
iface eth0:2 inet static
    address 192.168.82.4
    netmask 255.255.0.0

要綁幾個就寫幾段,存檔後重啟網路:

/etc/init.d/networking restart

驗證

用 ifconfig 會看到 eth0、eth0:0、eth0:1、eth0:2 各自帶著對應的位址(同一張實體網卡、不同 MAC 欄位相同屬正常現象),lo 則維持 127.0.0.1。最後 ping 一下新加的位址:

ping 192.168.82.2
PING 192.168.82.2 (192.168.82.2) 56(84) bytes of data.
64 bytes from 192.168.82.2: icmp_req=1 ttl=64 time=0.199 ms

能通就没問題。

補充

這種「別名介面」(eth0:0)是舊式寫法,新版的 ip 指令會把它們顯示在同一個 eth0 底下,用 ip addr show eth0 可以看到完整的 secondary 位址清單。若要綁整個網段,也可以改用同一段 iface 描述並列多個 address 行,設定檔會更簡潔。

Copyright © Linux教程網 All Rights Reserved