Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 網卡綁定多 IP 設定:ifconfig 臨時指令與永久配置檔雙軌教學

編輯:Linux 系統常見問題解答

單張網卡需綁定多個 IP(虛擬介面),常見於多站點虛擬主機、SSL 憑證綁定、容器網路、測試模擬多節點。本文說明臨時指令與永久寫入配置檔兩種方式。

方式一:ifconfig 臨時指令(重啟失效)

# 語法:ifconfig 介面:別名 IP netmask 子網路遮罩
ifconfig eth0:1 192.168.1.10 netmask 255.255.255.0
ifconfig eth0:2 192.168.1.11 netmask 255.255.255.0
ifconfig eth0:3 192.168.1.12 netmask 255.255.255.0

# 驗證
ifconfig -a
ip addr show eth0

缺點:重啟網路服務或重開機後消失。

方式二:永久配置(RedHat/CentOS/Fedora 系列)

為每個虛擬介面建立獨立設定檔:

# /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
BOOTPROTO=static
IPADDR=192.168.1.10
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet
# GATEWAY 僅在主介面 (eth0) 設定,虛擬介面不需重複

同理建立 ifcfg-eth0:2、ifcfg-eth0:3... 修改後:

systemctl restart network
# 或 service network restart

方式三:永久配置(Debian/Ubuntu 系列 - /etc/network/interfaces)


auto eth0:1
iface eth0:1 inet static
    address 192.168.1.10
    netmask 255.255.255.0

auto eth0:2
iface eth0:2 inet static
    address 192.168.1.11
    netmask 255.255.255.0

重啟:systemctl restart networking 或 ifdown eth0:1 && ifup eth0:1

方式四:Netplan (Ubuntu 17.10+)


# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.168.1.5/24      # 主 IP
        - 192.168.1.10/24     # 額外 IP
        - 192.168.1.11/24
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 114.114.114.114]

套用:sudo netplan apply

關鍵注意事項

  • 廣播位址:同網段虛擬 IP 共用主介面廣播,無需單獨設定
  • 路由表:核心自動為同網段 IP 建立本地路由,跨網段需手動 ip route add
  • ARP 衝突:同一 L2 網段內,確保 IP 唯一,避免 ARP 衝突
  • 防火牆:新 IP 需同步開放對應埠(firewall-cmd/ufw/iptables)
  • SELinux:若啟用,確保設定檔標籤正確(restorecon -R /etc/sysconfig/network-scripts)

驗證與除錯

ip addr show eth0
ping -I 192.168.1.10 192.168.1.1   # 指定來源 IP 測試連通
ss -ltn | grep :80                  # 確認服務監聽在 0.0.0.0 或具體 IP

掌握臨時與永久雙軌配置,單網卡多 IP 部署不再手忙腳亂。

Copyright © Windows教程網 All Rights Reserved