Howto: Configure Linux Virtual Local Area Network (VLAN)

情境 : 我想組態某台 Linux 的 eth0 網路介面可以帶 VLAN100 and VLAN500 的 tags.

解決方案 :

在 Cisco Switch 上要先設定該 Linux Port.

interface GigabitEthernet1/0/1
switchport trunk encapsulation dot1q
switchport trunk allowed vlan 100,500
switchport mode trunk
!

如果要自動載入 VLAN 模組, 可於 /etc/sysconfig/network 檔案內加入下列參數 :

VLAN=yes

而在 Linux 上的 /etc/sysconfig/network-scripts, /etc/sysconfig/networking/devices 和 /etc/sysconfig/networking/profiles/default 目錄內各複製一份 ifcfg-eth0  為 ifcfg-eth0.100 與 ifcfg-eth0.500 檔案

編輯 ifcfg-eth0.100

DEVICE=eth1.100
BOOTPROTO=none
HWADDR=xx:xx:xx:xx:xx:xx
IPADDR=10.1.1.100
NETMASK=255.255.255.0
ONBOOT=yes
GATEWAY=10.1.1.254
TYPE=Ethernet
VLAN=yes

再編輯 ifcfg-eth0.500

DEVICE=eth1.500
BOOTPROTO=none
HWADDR=xx:xx:xx:xx:xx:xx
IPADDR=10.2.2.100
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet
VLAN=yes

然後重新起動網路

# /etc/rc.d/init.d/network restart

或是你想用 vconfig 指令設定也是可以, 只是每次重新開機都要再下一次指令. 請注意在使用 VLAN 前不要忘記把 802.1q module掛上去. 用下列指令載入 802.1q module

# modprobe 8021q

然後用下列指令加入 vlan tags :

# vconfig add eth0 100
# vconfig add eth0 500

下列指令設定ip :

# ifconfig eth0.100 10.1.1.100 netmask 255.255.255.0 broadcast 10.1.1.255 up
# ifconfig eth0.500 10.2.2.100 netmask 255.255.255.0 broadcast 10.2.2.255 up

記得下 route 指令查看是否有正確的預設閘道器 :

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.1.1.0           *                 255.255.255.0  U     0      0        0 eth0.100
10.1.1.0           *                 255.255.255.0  U     0      0        0 eth0
10.2.2.0           *                 255.255.255.0  U     0      0        0 eth0.500
169.254.0.0     *                 255.255.255.0  U     0      0        0 eth0
default             10.1.1.254  0.0.0.0              UG   0      0        0 eth0.100

這樣就完成了. 🙂

如果要移除 VLAN100 & VLAN500就用下列命令

# ifconfig eth0.100 down
# ifconfig eth0.500 down
# vconfig rem eth0.100
# vconfig rem eth0.500

參考 :

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.