linux網卡的vlan配置

來源:互聯網
上載者:User

標籤:網卡   vlan   

   如果伺服器上連的交換器連接埠已經預先設定了TRUNK,並允許特定的VLAN可以通過,那麼伺服器的網卡在配置時就必須指定所屬的VLAN,否則就不通了,這種情形在虛擬化部署時較常見。

   例如在一個辦公環境中,辦公所在VLAN為1020,虛擬化的物理主機上有兩塊萬兆網卡,分屬與3111和3112VLAN,這三個VLAN間可以互連,那麼該如何配置呢?

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6B/8E/wKiom1UwlybDrobzAACnGvfptco560.jpg" title="111.jpg" alt="wKiom1UwlybDrobzAACnGvfptco560.jpg" />

   

一.首先要確認Linux系統核心是否已經支援VLAN功能:

當前使用核心以及作業系統版本:

[[email protected] ~]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 6.5 (Santiago)

首先yum安裝vconfig,如果有就不用裝了 

yum install vconfig

查看核心是否提供VLAN 功能,執行 

dmesg | grep -i 802

[[email protected]]# dmesg | grep -i 802

802.1Q VLAN Support v1.8 Ben Greear<[email protected]>

[[email protected]]# modprobe 8021q

[[email protected]~]#lsmod |grep 8021q         //查看系統核心是否支援802.1q協議

8021q                  18633 0

 

[[email protected] ~]# lspci       //確認網卡驅動是否已經正常載入

00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01)

00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01)

00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)

00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)

00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)

00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10)

00:0f.0 VGA compatible controller: VMware SVGA II Adapter

00:11.0 PCI bridge: VMware PCI bridge (rev 02)

00:15.0 PCI bridge: VMware PCI Express Root Port (rev 01)

00:15.1 PCI bridge: VMware PCI Express Root Port (rev 01)

00:15.2 PCI bridge: VMware PCI Express Root Port (rev 01)

 

 

二.物理網卡、子網卡、虛擬VLAN網卡的關係:

1.物理網卡:物理網卡這裡指的是伺服器上實際的網路介面裝置,在系統中可以看到的,比如2個物理網卡分別對應是eth0和eth1這兩個網路介面。

 

2. 子網卡:子網卡在這裡並不是實際上的網路介面裝置,但是可以作為網路介面在系統中出現,如eth0:1、eth1:2這種網路介面。它們必須要依賴於物理網卡,雖然可以與物理網卡的網路介面同時在系統中存在並使用不同的IP地址,而且也擁有它們自己的網路介面設定檔。但是當所依賴的物理網卡不啟用時(Down狀態)這些子網卡也將一同不能工作。

 

3.虛擬VLAN網卡:這些虛擬VLAN網卡也不是實際上的網路介面裝置,也可以作為網路介面在系統中出現,但是與子網卡不同的是,他們沒有自己的設定檔。他們只是通過將物理網加入不同的VLAN而產生的VLAN虛擬網卡。如果將一個物理網卡添加到多個VLAN當中去的話,就會有多個VLAN虛擬網卡出現,他們的資訊以及相關的VLAN資訊都是儲存在/proc/net/vlan/config這個臨時檔案中的,而沒有獨自的設定檔。它們的網路介面名是eth0.1、eth1.2這種名字。

 

註:當需要啟用VLAN虛擬網卡工作的時候,關聯的物理網卡網路介面上必須沒有IP地址的配置資訊。

 

[[email protected] ~]# vi/etc/sysconfig/network-scripts/ifcfg-eth1

————————————————–

DEVICE=eth1

ONBOOT=yes

BOOTPROTO=static

設定靜態地址並且不給IP地址設定。

 

三、在Linux配置VLAN Trunk:

由於在Linux上eth1要被設定為Trunk,並且屬於特定的3111VLAN。eth2也要被設定為Trunk,並且屬於特定的3112VLAN。

1.將eth1添加到VLAN 3111中:

[[email protected]]# vconfig add eth1 3111

WARNING: Could not open/proc/net/vlan/config. Maybe you need to load the 8021q module, or maybe youare not using PROCFS??

Added VLAN with VID == 3111 to IF -:eth1:-

第一次添加VLAN虛擬網卡的時候就一定會出現上面的那句提示,原因是因為預設下/proc/net/vlan/config這個專門用來儲存VLAN資訊的檔案是沒有的。由於第一次添加VLAN網卡,那麼這個檔案也會被自動建立起來。另外,在/proc/目錄下面的檔案都是系統的臨時檔案,因此重新啟動後必定丟失休息,所以在配置並測試VLAN成功後,可以將一些相關命令添加到rc.local這個啟動指令碼當中去。在執行該命令之前可以先到/proc/net/目錄查看下,並不存在vlan檔案夾,執行後會建立一個vlan檔案夾,並產生config設定檔,以及對應的虛擬vlan網卡設定檔 eth1.3111等.


 

 

2.同樣將eth2添加到VLAN3112中:

[[email protected] ~]# vconfig add eth2 3112

Added VLAN with VID == 3112 to IF -:eth2:-


 

3.檢查添加的VLAN虛擬網卡資訊:

[[email protected] ~]# cat/proc/net/vlan/config

--------------------------------------------------

VLAN Dev name    | VLAN ID

Name-Type:VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD

eth1.3111         | 3111 |      eth1

eth2.3112         | 3112 |      eth2

可以看到所有的VLAN虛擬網卡以及它們所歸屬的主物理網卡。

(如果只有一個物理網卡,也可配置為檔案ifcfg-eth1.3111和ifcfg-eth1.3112)


 

4.為VLAN虛擬網卡設定IP地址並且進行啟用:

[[email protected] ~]# ifconfig eth1.3111 192.168.20.1 up

[[email protected] ~]# ifconfig eth2.3112 192.168.30.1 up

這是臨時的,可以這樣做

利用cp命令複製介面的ip地址配置 

cp ifcfg-eth1 ifcfg-eth1.3111

cp ifcfg-eth2 ifcfg-eth2.3112

把ifcfg-eth1和ifcfg-eth2中的原有地址去掉,再vi修改ifcfg-eth1.3111和ifcfg-eth2.3112檔案,設定IP地址等資訊。

 

5.最後重啟network服務,令配置生效。

重新啟動網路服務

[[email protected] ~]# service network restart

--------------------------------------------------

Shutting down interface eth1:                            [ OK ]

Shutting down interface eth2:                            [ OK ]

Shutting down loopback interface:                        [ OK ]

Bringing up loopback interface:                          [ OK ]

Bringing up interface eth1:                              [ OK ]

Bringing up interface eth2:                              [ OK ]

--------------------------------------------------

 

6.檢查Linux系統下的所有網路介面資訊:

[[email protected] ~]# ifconfig |more

到這裡已經基本上將VLAN的主要配置完成了。


7、驗證連通性

[[email protected] ~]# ping 192.168.20.254

PING 192.168.20.254 (192.168.20.254) 56(84) bytes of data.

64 bytes from 192.168.20.254: icmp_seq=1 ttl=255 time=8.42 ms

64 bytes from 192.168.20.254: icmp_seq=2 ttl=255 time=1.47 ms

64 bytes from 192.168.20.254: icmp_seq=3 ttl=255 time=1.51 ms

64 bytes from 192.168.20.254: icmp_seq=4 ttl=255 time=1.58 ms

^C

--- 192.168.20.254 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 6325ms

rtt min/avg/max/mdev = 1.470/2.550/8.427/2.400 ms


[[email protected] ~]# ping 192.168.30.254

PING 192.168.30.254 (192.168.30.254) 56(84) bytes of data.

64 bytes from 192.168.30.254: icmp_seq=1 ttl=255 time=8.42 ms

64 bytes from 192.168.30.254: icmp_seq=2 ttl=255 time=1.47 ms

64 bytes from 192.168.30.254: icmp_seq=3 ttl=255 time=1.51 ms

64 bytes from 192.168.30.254: icmp_seq=4 ttl=255 time=1.58 ms

^C

--- 192.168.30.254 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 6325ms

rtt min/avg/max/mdev = 1.470/2.550/8.427/2.400 ms 


本文出自 “滴水穿石孫傑” 部落格,請務必保留此出處http://xjsunjie.blog.51cto.com/999372/1633815

linux網卡的vlan配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.