How to use VLAN tagged Ethernet card in CentOS/RHEL system

How to use VLAN tagged Ethernet card in CentOS/RHEL system

In some scenarios, we want to assign multiple IPs from different VLANs on the same Ethernet Card (NIC) in Linux servers (CentOS/RHEL). This can be achieved by using VLAN tagged interfaces. But to do this, we must ensure that multiple VLANs are connected to the switch ports, that is, we can add multiple VLANs to the switch to configure Aggregate Ports Trunk port (LCTT translation note: Generally there are Aggregate Ports Trunk port , Access port Access port , Hybrid Port Hybird port three types).

Assume we have a Linux server, we have two Ethernet cards here ( enp0s3 and enp0s8 ), the first card ( enp0s3 ) will be used for data transmission and the second card ( enp0s8 ) will be used for control/traffic management. I would use multiple VLANs for data transfer (or assign multiple IPs from different VLANs on the data traffic NIC).

I assume that the port connected to the data NIC of my server is configured as an aggregate port by mapping multiple VLANs.

The following are the VLANs mapped to the data transmission network interface card (NIC):

  • VLAN ID (200), VLAN N/W = 172.168.10.0/24
  • VLAN ID (300), VLAN N/W = 172.168.20.0/24

To use VLAN tagged interfaces in CentOS 7 / RHEL 7 / CentOS 8 / RHEL 8 systems, you must load the kernel module 8021q .

To load the kernel module 8021q , use the following command:

[root@linuxtechi ~]# lsmod | grep -i 8021q
[root@linuxtechi ~]# modprobe --first-time 8021q
[root@linuxtechi ~]# lsmod | grep -i 8021q
8021q 29022 0
garp 14384 1 8021q
mrp 18542 1 8021q
[root@linuxtechi ~]#

You can use modinfo command to display detailed information about the kernel module 8021q :

[root@linuxtechi ~]# modinfo 8021q
filename: /lib/modules/3.10.0-327.el7.x86_64/kernel/net/8021q/8021q.ko
version: 1.8
license: GPL
alias: rtnl-link-vlan
rhelversion: 7.2
srcversion: 2E63BD725D9DC11C7DA6190
depends: mrp,garp
intree: Y
vermagic: 3.10.0-327.el7.x86_64 SMP mod_unload modversions
signer: CentOS Linux kernel signing key
sig_key: 79:AD:88:6A:11:3C:A0:22:35:26:33:6C:0F:82:5B:8A:94:29:6A:B3
sig_hashalgo: sha256
[root@linuxtechi ~]#

Now use the ip command to tag (or map) VLANs 200 and 300 to enp0s3 network card.

(LCTT translation note: This is to map the VLAN tag 200 to the enp0s3 network card first.)

[root@linuxtechi ~]# ip link add link enp0s3 name enp0s3.200 type vlan id 200

Use the following ip command to open the interface:

[root@linuxtechi ~]# ip link set dev enp0s3.200 up

Similarly, map the VLAN tag 300 to the enp0s3 network card:

[root@linuxtechi ~]# ip link add link enp0s3 name enp0s3.300 type vlan id 300
[root@linuxtechi ~]# ip link set dev enp0s3.300 up

Now use the ip command to view the status of the marked interface:

Now we can assign IP addresses to the tagged interfaces from their respective VLANs using the following ip command:

[root@linuxtechi ~]# ip addr add 172.168.10.51/24 dev enp0s3.200
[root@linuxtechi ~]# ip addr add 172.168.20.51/24 dev enp0s3.300

Use the following ip command to check whether an IP is assigned to the marked interface:

After the reboot, all the changes made through the ip command above will not be kept (LCTT translation note: the changes can be saved to the configuration file or database. If the save process is not performed, only the current environment will take effect, and the configuration will become invalid after the reboot). These marked interfaces will be unavailable after service network restart (LCTT translation note: service network restart, or down and up commands).

Therefore, to make the marked interface persistent across reboots, you need to use the ifcfg file for the interface.

Edit the interface ( enp0s3 ) file /etc/sysconfig/network-scripts/ifcfg-enp0s3 and add the following content:

Author's reminder: Replace with the interface name in your environment.

[root@linuxtechi ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
DEVICE=enp0s3
BOOTPROTO=none
ONBOOT=yes

Save and exit the file.

Create an interface file /etc/sysconfig/network-scripts/ifcfg-enp0s3.200 for VLAN id 200 and add the following content:

[root@linuxtechi ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3.200
DEVICE=enp0s3.200
BOOTPROTO=none
ONBOOT=yes
IPADDR=172.168.10.51
PREFIX=24
NETWORK=172.168.10.0
VLAN=yes

Save and exit the file.

Similarly, create an interface file /etc/sysconfig/network-scripts/ifcfg-enp0s3.300 for VLAN id 300 and add the following content:

[root@linuxtechi ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3.300
DEVICE=enp0s3.300
BOOTPROTO=none
ONBOOT=yes
IPADDR=172.168.20.51
PREFIX=24
NETWORK=172.168.20.0
VLAN=yes

Save and exit the file. At this time, use the following command to restart the network service

[root@linuxtechi ~]# systemctl restart network

Now verify that the marked interface is configured, up and running using the following ip command:

This is the end of this article about how to use Ethernet cards with VLAN tags in CentOS/RHEL systems. For more information about using Ethernet cards with VLAN tags in CentOS systems, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • VMware installation of CentOS virtual machine and configuration network graphic tutorial
  • Centos7.3 automatically starts or executes specified commands when booting
  • Detailed steps to install RedHat RHEL7.2 system in vmware12 (picture and text)
  • Detailed explanation of VLAN configuration of Linux virtual network devices
  • Breakout VLAN
  • About the DHCP domain in VLAN

<<:  Implementation steps of Mysql merge results and horizontal splicing fields

>>:  JS implements circular progress bar drag and slide

Recommend

Implementation of Nginx configuration of multi-port and multi-domain name access

To deploy multiple sites on a server, you need to...

W3C Tutorial (13): W3C WSDL Activities

Web Services are concerned with application-to-ap...

Modularity in Node.js, npm package manager explained

Table of contents The basic concept of modularity...

MySQL table auto-increment id overflow fault review solution

Problem: The overflow of the auto-increment ID in...

Detailed explanation of MySQL trigger trigger example

Table of contents What is a trigger Create a trig...

element-ui Mark the coordinate points after uploading the picture

What is element-ui element-ui is a desktop compon...

Steps for Django to connect to local MySQL database (pycharm)

Step 1: Change DATABASES in setting.py # Configur...

Summary of Linux command methods to view used commands

There are many commands used in the system, so ho...

JavaScript implementation of verification code case

This article shares the specific code for JavaScr...

Vue encapsulation component tool $attrs, $listeners usage

Table of contents Preface $attrs example: $listen...

HTML uses the title attribute to display text when the mouse hovers

Copy code The code is as follows: <a href=# ti...

Example method to view the IP address connected to MySQL

Specific method: First open the command prompt; T...

5 ways to determine whether an object is an empty object in JS

1. Convert the json object into a json string, an...