VLAN is an acronym for Virtual Local Area Network. Several VLANs can co-exist on a single physical switch, which are configured via Linux software and not through hardware interface (you still need to configure actual hardware switch too).
Hubs or switch connects all nodes in a LAN and node can communicate without a router. For example, all nodes in LAN A can communicate with each other without the need for a router. If a node from LAN A wants to communicate with LAN B node, you need to use a router. Therefore, each LAN (A, B, C and so on)are separated using a router.
VLAN as a name suggest combine multiple LANs at once. But, what are the advantages of VLAN?
Fundamental discussion about VLAN or switches is beyond the scope of this blog post. I suggest the following textbooks:
A note about your LAN hardware
To be able to use VLANs you will need a switch that support the IEEE 802.1q standard on an Ethernet network. You will also need a NIC (Network Interface Card) that works with Linux and support 802.1q standard .
Linux VLAN configuration issues
I am lucky enough to get a couple of hints from our internal wiki:
Enough talk, let's get to the Linux VLAN configurations.
Setting up 802.1q VLAN tagging by loading 8021q Linux kernel driver
First, make sure that the Linux kernel driver (module) called 8021q is loaded:
If the module is not loaded, load it with the following modprobe command:
Method #1: CentOS/RHLE/Fedora Linux VLAN HowTo
I am using RHEL/CentOS Linux with VLAN ID # 5. So I need to copy file /etc/sysconfig/network-scripts/ifcfg-eth0 to /etc/sysconfig/network-scripts/ifcfg-eth0.5
Now, I've one network card (eth0) and it needs to tagged network traffic for VLAN ID 5.
Do not modify /etc/sysconfig/network-scripts/ifcfg-eth0 file. Now open file /etc/sysconfig/network-scripts/ifcfg-eth0.5 using a text editor such as vi, type:
Find DEVICE=eth0 line and replace with:
- /etc/sysconfig/network-scripts/ifcfg-eth0.5 file
Method #2: Using the vconfig command
Above method is perfect and works with a Red hat Enterprise Linux / CentOS / Fedora Linux without any problem. However, you will notice that there is a command called vconfig. The vconfig program allows you to create and remove vlan-devices on a vlan enabled kernel. Vlan-devices are virtual Ethernet devices which represents the virtual lans on the physical lan. This is yet another method of configuring VLAN. To add VLAN ID 5 with following command for eth0 interface:
The vconfig add command creates a vlan-device on eth0 which result into eth0.5 interface. You can use normal ifconfig command to see device information:
Use ifconfig command to assign IP address to vlan interfere:
To get detailed information about VLAN interface, type:
If you wish to delete VLAN interface use delete command as follows:
Method #3: Create the VLAN device using the ip command
Use the ip command as follows for the interface eth0, and the vlan id is 5:
You need to activate and add an IP address to vlan link, type:
All traffic will go through the eth0 interface bith with a BLAN tag 5. Only VLAN aware devices can accept the traffic, otherwise the traffic is dropped.
How can I remove VLAN ID 5?
Type the following commands
How do I make above VLAN configuration permanent on a Debian or Ubuntu based system?
Edit the /etc/network/interfaces file, enter:
* [Linux 小技巧] 使用 vconfig 設定 vlan