假設這樣一種場景:
某運營商的Linux服務器上裝配了2家互聯網公司的Web服務,每個Web服務分配了一個公網IP地址。但是運營商的Linux服務器只有一塊網卡。這就需要在一塊網卡上綁定多個IP地址。對用戶來說,就好像每個Web服務再單獨一台服務器上一樣。
實驗環境:Redhat 6.4 企業版64位
在目錄/etc/sysconfig/network-scripts下可以看到物理網卡的配置文件ifcfg-eth#,如果要在某個物理網卡上綁定多個虛擬IP address,就需要在這個物理網卡上虛擬出網卡ifcfg-eth#:$(注:#、$代表數字0,1,2....,其中$取值范圍0-255,#視服務器上插了多少個以太網卡而定)。
使用ifconfig命令在ifcfg-eth0上面添加多個虛擬網卡
添加之前
[root@51cto network-scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:82:AA:8B inet addr:192.168.80.222 Bcast:192.168.80.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe82:aa8b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1084 errors:0 dropped:0 overruns:0 frame:0 TX packets:868 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:98302 (95.9 KiB) TX bytes:89630 (87.5 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
添加虛擬網卡eth0:0(不一定非得從0開始,只要是0-255隨便寫)
[root@51cto network-scripts]# ifconfig eth0:0 192.168.80.223 [root@51cto network-scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:82:AA:8B inet addr:192.168.80.222 Bcast:192.168.80.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe82:aa8b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1208 errors:0 dropped:0 overruns:0 frame:0 TX packets:940 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:108516 (105.9 KiB) TX bytes:98514 (96.2 KiB) eth0:0 Link encap:Ethernet HWaddr 00:0C:29:82:AA:8B inet addr:192.168.80.223 Bcast:192.168.80.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
由於這是使用命令臨時加上去的,當然在/etc/sysconfig/network-scripts下面的網卡配置文件不會與任何變化。在宿主機Win10 ping WMWare Workstation中Linux虛擬機 192.168.80.223 OK。

同樣的方法再添加一個虛擬網卡eth0:1 192.168.80.224
[root@51cto network-scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:82:AA:8B inet addr:192.168.80.222 Bcast:192.168.80.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe82:aa8b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1322 errors:0 dropped:0 overruns:0 frame:0 TX packets:1023 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:118308 (115.5 KiB) TX bytes:108496 (105.9 KiB) eth0:0 Link encap:Ethernet HWaddr 00:0C:29:82:AA:8B inet addr:192.168.80.223 Bcast:192.168.80.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:82:AA:8B inet addr:192.168.80.224 Bcast:192.168.80.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
可見虛擬網卡與真是網卡,IP不一樣,但是MAC一樣。
如果要禁用虛擬網,操作方式和禁用物理網卡是一樣的,ifconfig eth#:$ down即可。禁用之后Win10就ping不通了,在Linux上使用ifconfig看不到eth#:$這個虛擬網卡。如果我們想再次起用這個虛擬網卡可以嗎?ifconfig eth#:$ up是否可以呢?
不好意思,用ifconfig臨時加的虛擬網卡一旦down就沒了,再也up不起來。
[root@51cto network-scripts]# ifconfig eth0:1 up SIOCSIFFLAGS: Cannot assign requested address
如果重啟網絡服務,重啟系統,臨時加的虛擬網卡也會消失。
永久生效的辦法只有一個,修改配置文件。這個配置文件系統沒有自帶,需要手動創建,在/etc/sysconfig/network-scripts下面創建
ifcfg-eth#:$
[root@51cto network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0 [root@51cto network-scripts]# vi ifcfg-eth0:0 [root@51cto network-scripts]# cat ifcfg-eth0:0 DEVICE=eth0:0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.80.223 PREFIX=24 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0:0" HWADDR=00:0C:29:82:AA:8B
[root@51cto network-scripts]# service network restart
