Merge pull request #20450 from quinbyjoe/master

translated
This commit is contained in:
Xingyu.Wang 2020-12-15 14:38:38 +08:00 committed by GitHub
commit cecab52cc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,28 +7,26 @@
[#]: via: (https://www.linuxtechi.com/vlan-tagged-nic-ethernet-card-centos-rhel-servers/)
[#]: author: (Pradeep Kumar https://www.linuxtechi.com/author/pradeep/)
How to Use VLAN tagged NIC (Ethernet Card) on CentOS and RHEL Servers
如何在CentOS和RHEL系统的服务器中使用带VLAN标记的以太网卡
======
There are some scenarios where we want to assign multiple IPs from different **VLAN** on the same Ethernet card (nic) on Linux servers ( **CentOS** / **RHEL** ). This can be done by enabling VLAN tagged interface. But for this to happen first we must make sure multiple VLANs are attached to port on switch or in other words we can say we should configure trunk port by adding multiple VLANs on switch.
在某些方案中我们希望在Linux服务器( **CentOS** / **RHEL** )上的同一以太网卡NIC分配来自不同VLAN的多个IP。这个可以通过使用VLAN标记的接口实现。但是要实现这个问题我们必须确保多个VLAN连接到交换机的端口上也就是说我们应该在交换机上添加多个VLAN来配置聚合端口译者注一般有聚合端口Trunk port接入端口Access port混合端口Hybird port三种
<https://www.linuxtechi.com/wp-content/uploads/2019/06/VLAN-Tagged-NIC-Linux-Server.jpg>
![tagged-interface-status-ip-command-linux-server][7]
Lets assume we have a Linux Server, there we have two Ethernet cards (enp0s3 & enp0s8), first NIC ( **enp0s3** ) will be used for data traffic and second NIC ( **enp0s8** ) will be used for control / management traffic. For Data traffic I will using multiple VLANs (or will assign multiple IPs from different VLANs on data traffic ethernet card).
假设我们有一个Linux服务器我们在这里有两张以太网卡 (enp0s3 和 enp0s8),第一张网卡( **enp0s3** )将被用于数据传输,而第二张网卡( **enp0s8** )将被用于控制/流量管理。我将使用多个VLAN用于数据传输或在数据流量网卡上从不同的VLAN中分配多个IP
I am assuming the port from switch which is connected to my server data NIC is configured as trunk port by mapping the multiple VLANs to it.
我假设连接到我服务器的数据网卡的端口是通过映射多个VLAN去配置为聚合端口。
Following are the VLANs which is mapped to data traffic Ethernet Card (NIC):
下面是映射到数据传输网卡(NIC)的VLAN
* VLAN ID (200), VLAN N/W = 172.168.10.0/24
* VLAN ID (300), VLAN N/W = 172.168.20.0/24
要在CentOS 7 / RHEL 7 / CentOS 8 / RHEL 8系统中使用VLAN标记的接口必须加载[内核模块][1] **8021q**
To use VLAN tagged interface on CentOS 7 / RHEL 7 / CentOS 8 /RHEL 8 systems, [kernel module][1] **8021q** must be loaded.
Use the following command to load the kernel module “8021q”
加载内核模块“8021q”可以使用下面的命令
```
[root@linuxtechi ~]# lsmod | grep -i 8021q
[root@linuxtechi ~]# modprobe --first-time 8021q
@ -39,8 +37,7 @@ mrp 18542 1 8021q
[root@linuxtechi ~]#
```
Use below modinfo command to display information about kernel module “8021q”
可以使用modinfo命令显示内核模块“8021q”的详细信息
```
[root@linuxtechi ~]# modinfo 8021q
filename: /lib/modules/3.10.0-327.el7.x86_64/kernel/net/8021q/8021q.ko
@ -58,48 +55,44 @@ sig_hashalgo: sha256
[root@linuxtechi ~]#
```
Now tagged (or mapped) the VLANs 200 and 300 to NIC enp0s3 using the [ip command][2]
现在使用[ip 命令][2]给enp0s3网卡加上200和300的VLAN标签或映射
译者注先给enp0s3网卡映射上200的VLAN标签。
```
[root@linuxtechi ~]# ip link add link enp0s3 name enp0s3.200 type vlan id 200
```
Bring up the interface using below ip command:
使用下面的ip命令打开接口
```
[root@linuxtechi ~]# ip link set dev enp0s3.200 up
```
Similarly mapped the VLAN 300 to NIC enp0s3
同理给enp0s3网卡映射上300的VLAN标签
```
[root@linuxtechi ~]# ip link add link enp0s3 name enp0s3.300 type vlan id 300
[root@linuxtechi ~]# ip link set dev enp0s3.300 up
[root@linuxtechi ~]#
```
Now view the tagged interface status using ip command:
现在使用ip命令查看标记后的接口状态
[![tagged-interface-ip-command][3]][4]
Now we can assign the IP address to tagged interface from their respective VLANs using beneath ip command,
现在我们可以使用下面的ip命令从它们各自的VLAN为已经标记的接口分配IP地址
```
[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 below ip command to see whether IP is assigned to tagged interface or not.
使用下面的ip命令查看是否为已标记的接口分配到IP:
![ip-address-tagged-nic][5]
All the above changes via ip commands will not be persistent across the reboot. These tagged interfaces will not be available after reboot and after network service restart
重启之后上面所有通过ip命令的更改都不会持续译者注修改后可保存至配置文件或数据库中如果未进行保存处理则只有当前环境生效重启后配置失效。系统重启和网络服务重启译者注service network restart或down和up命令之后这些标记的接口将不可用。
So, to make tagged interfaces persistent across the reboot then use interface **ifcfg files**
因此,要使标记的接口在重启后保持不变,需要使用接口的**ifcfg files** 。
Edit interface (enp0s3) file “ **/etc/sysconfig/network-scripts/ifcfg-enp0s3** ” and add the following content,
编辑接口(enp0s3)文件“ **/etc/sysconfig/network-scripts/ifcfg-enp0s3** ”,并且增加下面的内容,
Note: Replace the interface name that suits to your env,
**作者提醒**:替换为您环境中的接口名称。
```
[root@linuxtechi ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
@ -109,9 +102,10 @@ BOOTPROTO=none
ONBOOT=yes
```
Save & exit the file
保存和退出文件。
Create tagged interface file for VLAN id 200 as “ **/etc/sysconfig/network-scripts/ifcfg-enp0s3.200** ” and add the following contents to it.
为id是200的VLAN创建接口文件“ **/etc/sysconfig/network-scripts/ifcfg-enp0s3.200** ”,且增加下面的内容。
```
[root@linuxtechi ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3.200
@ -124,9 +118,9 @@ NETWORK=172.168.10.0
VLAN=yes
```
Save & exit the file
保存并退出此文件。
Similarly create interface file for VLAN id 300 as “/etc/sysconfig/network-scripts/ifcfg-enp0s3.300” and add the following contents to it
同理为id是300的VLAN创建接口文件“ **/etc/sysconfig/network-scripts/ifcfg-enp0s3.300** ”,且增加下面的内容。
```
[root@linuxtechi ~]# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3.300
@ -139,26 +133,26 @@ NETWORK=172.168.20.0
VLAN=yes
```
Save and exit file and then restart network services using the beneath command,
保存并退出文件,这时候使用下面的命令重启网络服务,
```
[root@linuxtechi ~]# systemctl restart network
[root@linuxtechi ~]#
```
Now verify whether tagged interface are configured and up & running using the ip command,
现在使用下面的ip命令检验标记的接口是否已配置和启动并且正在运行中
![tagged-interface-status-ip-command-linux-server][6]
Thats all from this article, I hope you got an idea how to configure and enable VLAN tagged interface on CentOS 7 / 8 and RHEL 7 /8 Severs. Please do share your feedback and comments.
以上就是本文的全部内容我希望您已经得到了在CentOS 7 / 8 and RHEL 7 /8 服务器上如何去配置和使能带VLAN标签的接口方法。请分享您的反馈和意见。
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/vlan-tagged-nic-ethernet-card-centos-rhel-servers/
参考: https://www.linuxtechi.com/vlan-tagged-nic-ethernet-card-centos-rhel-servers/
作者:[Pradeep Kumar][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
译者:[quinbyjoe](https://github.com/quinbyjoe)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
@ -171,3 +165,4 @@ via: https://www.linuxtechi.com/vlan-tagged-nic-ethernet-card-centos-rhel-server
[4]: https://www.linuxtechi.com/wp-content/uploads/2019/06/tagged-interface-ip-command.jpg
[5]: https://www.linuxtechi.com/wp-content/uploads/2019/06/ip-address-tagged-nic-1024x343.jpg
[6]: https://www.linuxtechi.com/wp-content/uploads/2019/06/tagged-interface-status-ip-command-linux-server-1024x656.jpg
[7]: https://www.linuxtechi.com/wp-content/uploads/2019/06/VLAN-Tagged-NIC-Linux-Server.jpg