單張網卡需綁定多個 IP(虛擬介面),常見於多站點虛擬主機、SSL 憑證綁定、容器網路、測試模擬多節點。本文說明臨時指令與永久寫入配置檔兩種方式。
# 語法: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
缺點:重啟網路服務或重開機後消失。
為每個虛擬介面建立獨立設定檔:
# /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
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
# /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 route addfirewall-cmd/ufw/iptables)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 部署不再手忙腳亂。