Linux vlan configuration

Using Config Files

Asuming the VLAN ID is 5. You need to copy file /etc/sysconfig/network-scripts/ifcfg-eth0 to /etc/sysconfig/network-scripts/ifcfg-eth0.5

 cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.5

Now, We have network card (eth0) and it needs to use tagged network traffic for VLAN ID 5.

 eth0 - Your regular network interface
 eth0.5 - Your virtual interface that use untagged frames

Do NOT modify /etc/sysconfig/network-scripts/ifcfg-eth0 file. Now open file /etc/sysconfig/network-scripts/ifcfg-eth0.5 using vi text editor:

 vi /etc/sysconfig/network-scripts/ifcfg-eth0.5

Find DEVICE=ifcfg-eth0line and replace with:

 DEVICE=ifcfg-eth0.5

Append line:

 VLAN=yes

Also make sure you assign correct IP address using DHCP or static IP. Save the file. Remove gateway entry from all other network config files. Only add gateway to /etc/sysconfig/network file. Save and close the file. Restart network:

 /etc/init.d/network restart

Please note that if you need to configure for VLAN ID 2 then copy the copy file /etc/sysconfig/network-scripts/ifcfg-eth0 to /etc/sysconfig/network-scripts/ifcfg-eth0.2 and do the above procedure again.

 

Using vconfig command

Above method is perfect and works with 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.

Please note that this is yet another method of configuring VLAN. If you are happy with above method no need to read below.

Add VLAN

Add VLAN ID 5 with follwing command for eth0:

 vconfig add eth0 5

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:

 ifconfig eth0.5

Use ifconfig to assign IP address to vlan interfere :

 ifconfig eth0.5 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255 up

Get detailed information about VLAN interface:

 cat /proc/net/vlan/eth0.5

Delete VLAN

If you wish to delete VLAN interface delete command:

 ifconfig eth0.5 down
 vconfig rem eth0.5

Linux VLAN Configuration Issue

  • Not all network drivers support VLAN. You may need to patch your driver.
  • MTU may be another problem. It works by tagging each frame i.e. an Ethernet header extension that enlarges the header from 14 to 18 bytes. The VLAN tag contains the VLAN ID and priority. See Linux VLAN site for patches and other information.
  • Do not use VLAN ID 1 as it may be used for admin purpose.

Errors

 sudo vconfig add eth0 15
 WARNING:  Could not open /proc/net/vlan/config.  Maybe you need to load the 8021q module, or maybe you are not using PROCFS??
 Added VLAN with VID == 15 to IF -:eth0:-

Solution

 modprobe 8021q
 
VLAN HOWTO:
The fist step to enable the VLAN support in Linux. The config file is 
/etc/sysconfig/network. What you need there is :
VLAN=yes
VLAN_NAME_TYPE=DEV_PLUS_VID_NO_PAD
The fist line makes the linux kernel load the necessary modules for enabling 
VLAN support. In Redhat that is usually the 8021q module. 

For 
initail setup you can load the module manually with the following command

 
 
modprobe 8021q
The second line is the specification of the naming convention for how the vlan interfaces will be presented on the file system. Generally there most commonly used conventions are VLAN_PLUS_VID - example suffix for interface name is vlan0005 VLAN_PLUS_VID_NO_PAD - example suffix for interface name is vlan5 DEV_PLUS_VID - example suffix for interface name is eth0.0005 DEV_PLUS_VID_NO_PAD - example suffix for interface name is eth0.5 I personally prefer the last naming method - DEV_PLUS_VID_NO_PAD
1. Connect the eth0 interface of your linux machine to the switch.
2. Remove the IP Address information on the eth0 interface

# ifconfig eth0 0.0.0.0
# ifconfig eth0 up

3. Configure 2 VLANs on the eth0 interface using vconfig as follows (100,200 are the VLAN id's). If the 8021q.o module is not loaded, the vconfig command (when invoked first time) will automatically load the module.

# vconfig add eth0 100
# vconfig add eth0 200 

4. Configure IP on the VLAN interfaces 

# ifconfig eth0.100 xxx.xxx.xxx.xxx netmask 255.255.252.0 up
# ifconfig eth0.200 yyy.yyy.yyy.yyy netmask 255.255.255.0 up

5. Preserve the vlan configuration across reboots by adding it to configuration files. Create the appropriate ifcfg files for eth0, eth0.100 and eth0.200 in /etc/sysconfig/network-scripts/

# cd /etc/sysconfig/network-scripts/

Contents of ifcfg-eth0
DEVICE=eth0
ONBOOT=no
TYPE=Ethernet

Contents of ifcfg-eth0.100
DEVICE=eth0.100
IPADDR=xxx.xxx.xxx.xxx
NETMASK=255.255.252.0
VLAN=yes
ONBOOT=yes
BOOTPROTO=none

Contents of ifcfg-eth0.200
DEVICE=eth0.200
IPADDR=yyy.yyy.yyy.yyy
NETMASK=255.255.0.0
VLAN=yes
ONBOOT=yes
BOOTPROTO=none

Update /etc/sysconfig/network file to make the GATEWAYDEV use the public vlan interface.

Contents of /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=un1xf00
GATEWAY=xxx.xxx.xxx.1
DOMAINNAME=dev.un1xf00.com
GATEWAYDEV=eth0.100


6. The VLAN configuration on the server can be verified in the file /proc/net/vlan/config. Sample contents are shown below.

VLAN Dev name | VLAN ID
Name-Type: VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
eth0.100 | 100 | eth0
eth0.200 | 200 | eth0

More at unix linux & storage

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux VLAN 是一种用于在 Linux 操作系统中划分虚拟局域网(Virtual Local Area Network)的技术。通过 Linux VLAN,可以将一个物理网卡划分为多个虚拟子接口,每个子接口可以分配一个 VLAN ID,并与不同的 VLAN 进行通信。这样可以实现对网络流量的隔离和管理。 要在 Linux 中配置 VLAN,可以通过编辑网络配置文件来实现。例如,使用 vi 命令编辑 "/etc/sysconfig/network-scripts/ifcfg-eth0.20" 文件来配置一个名为 eth0.20 的 VLAN 子接口。在文件中,需要设置 TYPE=Vlan,BOOTPROTO=static,ONBOOT=yes,IPADDR=172.16.20.1,PREFIX=24,VLAN=yes,VLAN_ID=20 等参数来定义该子接口的属性和配置。同样,可以编辑 "/etc/sysconfig/network-scripts/ifcfg-eth0.10" 文件来配置另一个名为 eth0.10 的 VLAN 子接口,并设置相应的参数。 通过这种方式,可以在 Linux 系统中实现 VLAN 的划分,并与网络设备进行对接,实现不同 VLAN 之间的通信和隔离。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Linux 划分Vlan的方法及配置](https://blog.csdn.net/m0_69951597/article/details/126324832)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值