使用 ifconfig 別名介面瞬間生效:
# 新增第一個虛擬 IP
ifconfig eth0:1 192.168.0.2 netmask 255.255.255.0 up
# 新增第二個虛擬 IP
ifconfig eth0:2 192.168.0.3 netmask 255.255.255.0 up
# 驗證
ifconfig -a | grep -A1 'eth0:'
注意:現代發行版建議改用 ip 指令:
ip addr add 192.168.0.2/24 dev eth0 label eth0:1
ip addr add 192.168.0.3/24 dev eth0 label eth0:2
建立獨立設定檔 /etc/sysconfig/network-scripts/ifcfg-eth0:1:
DEVICE=eth0:1
BOOTPROTO=static
IPADDR=192.168.0.2
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet
同理建立 ifcfg-eth0:2 等。重啟網路服務生效:
systemctl restart network
編輯 /etc/network/interfaces:
auto eth0:1
iface eth0:1 inet static
address 192.168.0.2
netmask 255.255.255.0
auto eth0:2
iface eth0:2 inet static
address 192.168.0.3
netmask 255.255.255.0
重啟:systemctl restart networking 或 ifdown eth0 && ifup eth0。
編輯 /etc/netplan/01-netcfg.yaml(縮排嚴格用空格):
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 192.168.0.1/24
- 192.168.0.2/24
- 192.168.0.3/24
gateway4: 192.168.0.254
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
套用:netplan apply。
nmcli con mod eth0 +ipv4.addresses 192.168.0.2/24
nmcli con mod eth0 +ipv4.addresses 192.168.0.3/24
nmcli con up eth0
ip addr show eth0 # 查看所有 IP
ping -I 192.168.0.2 8.8.8.8 # 指定來源 IP 測試連通
ip route get 8.8.8.8 from 192.168.0.2 # 檢查路由選擇
ifconfig 別名在 ip 工具中顯示為同介面多 IP,非真正虛擬介面。netplan try 先測試。臨時測試用 ip addr add,永久配置依發行版選擇 sysconfig/interfaces/Netplan/nmcli。生產環境務必在維護窗口操作,並準備回滾方案(保留原設定檔備份)。