centos如何创建带vlan标签的网卡子接口(ubuntu18.04之前适用)

第一:确认系统加载了802.1Q模块,并让主网卡零配置

#查看系统是否加载802.1Q模块
[root@localhost ~]# lsmod |grep 8021q
8021q                  20475  0 
garp                    7152  1 8021q

#主网卡配置(无特殊,只要开机自启)
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp3s0
TYPE=Ethernet
UUID=e3811502-5ce4-40c4-87ca-52e8c6b566d4
DEVICE=enp3s0
PROXY_METHOD=none
BROWSER_ONLY=no
IPV6INIT=no
NAME="System enp3s0"
ONBOOT=yes

第二:创建带vlan标签的网卡文件

#创建带vlan的网卡设备(注意名字,这是命令规范,这里vlan100 对应网卡名字为ifcfg-enp3s0.100 )
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp3s0.100 
BOOTPROTO=none
DEVICE=enp3s0.100
ONBOOT=yes
VLAN=yes
IPADDR=111.222.111.111
PREFIX=24
GATEWAY=11.222.111.1

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp3s0.200
BOOTPROTO=none
DEVICE=enp3s0.200
ONBOOT=yes
VLAN=yes
IPADDR=222.111.111.111
PREFIX=24
GATEWAY=222.111.111.1


#第三:配置策略路由(较为重要哦)
	ip route flush table 100
	ip rou add default via 111.222.111.1 dev enp3s0.100 src 111.222.111.111 table 100 prio 50
	ip rule add from 111.222.111.111 table 100

	ip route flush table 200
	ip rou add default via 222.111.111.1 dev enp3s0.200 src 222.111.111.111 table 200 prio 50
	ip rule add from 222.111.111.111 table 200

第四:重启网络服务,并验证vlan标签是否可使用

#重启网络服务
    systemctl stop NetworkManager
    systemctl restart network

#验证vlan标签是否创建成功
[root@localhost ~]# cat /proc/net/vlan/enp3s0.100 
enp3s0.100  VID: 100	 REORDER_HDR: 1  dev->priv_flags: 1
         total frames received          525
          total bytes received        35155
      Broadcast/Multicast Rcvd            0

      total frames transmitted          322
       total bytes transmitted        54196
Device: enp3s0
INGRESS priority mappings: 0:0  1:0  2:0  3:0  4:0  5:0  6:0 7:0
 EGRESS priority mappings

#验证IP是否生效
[root@localhost ~]# ip a
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
   link/ether 00:24:ec:f0:62:fe brd ff:ff:ff:ff:ff:ff
24: enp3s0.100@enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
   link/ether 00:24:ec:f0:62:fe brd ff:ff:ff:ff:ff:ff
   inet 111.222.111.111/24 brd 110.185.122.159 scope global enp3s0.100
      valid_lft forever preferred_lft forever
   inet6 fe80::224:ecff:fef0:62fe/64 scope link 
      valid_lft forever preferred_lft forever
25: enp3s0.200@enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
   link/ether 00:24:ec:f0:62:fe brd ff:ff:ff:ff:ff:ff
   inet 222.111.111.111/24 brd 101.207.228.95 scope global enp3s0.200
      valid_lft forever preferred_lft forever
   inet6 fe80::224:ecff:fef0:62fe/64 scope link 
      valid_lft forever preferred_lft forever

#验证网络是否畅通(带源MTR)
[root@localhost ~]# mtr -a 111.222.111.111 jk.fnk.com -r
Start: Tue Sep 25 22:56:01 2018
HOST: localhost.localdomain       Loss%   Snt   Last   Avg  Best  Wrst StDev
  1.|-- gateway                    0.0%    10    1.9   1.9   1.8   2.0   0.0
  2.|-- 172.29.251.1               0.0%    10    1.0   5.4   1.0  20.7   8.0
  3.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
  4.|-- 101.207.114.237           60.0%    10    2.4   2.6   2.4   2.8   0.0
  5.|-- 119.6.195.49              10.0%    10    2.5   6.7   2.4  40.1  12.5
  6.|-- 119.6.197.33               0.0%    10    2.3   2.2   2.1   2.7   0.0
  7.|-- 219.158.99.221             0.0%    10   42.0  42.0  42.0  42.1   0.0
  8.|-- 120.86.0.82                0.0%    10   47.0  48.2  44.9  51.9   2.2
  9.|-- 120.80.99.2                0.0%    10   46.1  47.5  46.1  49.5   1.2
 10.|-- 112.95.237.194             0.0%    10   46.8  47.0  46.8  48.0   0.0
 11.|-- 42.120.242.234             0.0%    10   50.2  49.8  49.4  50.3   0.0
 12.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 13.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 14.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 15.|-- 47.106.128.25             0.0%    10   48.9  48.9  48.9  49.0   0.0
[root@localhost ~]# mtr -a 222.111.111.111 jk.fnk.com -r
Start: Tue Sep 25 22:57:15 2018
HOST: localhost.localdomain       Loss%   Snt   Last   Avg  Best  Wrst StDev
  1.|-- gateway                    0.0%    10    3.6   5.1   3.6  16.0   3.8
  2.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
  3.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
  4.|-- 101.207.114.237           30.0%    10    2.2   2.1   2.1   2.2   0.0
  5.|-- 119.6.195.49              70.0%    10    2.2   2.1   2.1   2.2   0.0
  6.|-- 119.6.195.29               0.0%    10    2.8   2.6   2.5   2.8   0.0
  7.|-- 219.158.105.9              0.0%    10   39.8  39.8  39.7  40.0   0.0
  8.|-- 221.4.0.194                0.0%    10   45.3  48.4  45.3  52.2   2.4
  9.|-- 120.80.98.198              0.0%    10   44.9  45.1  43.6  47.1   1.1
 10.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 11.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 12.|-- 119.38.215.77              0.0%    10   42.3  42.3  42.2  42.7   0.0
 13.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 14.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 15.|-- 47.106.128.125             0.0%     9   44.3  44.3  44.3  44.4   0.0

Ubuntu18.04之前的版本适用(列如16.04LTS)

#第一确认:是否有802.1q模块,并安装vlan软件包(不安装该包会导致带vlan的网卡无法启动)
   lsmod |grep -i 8021q   #若是没有,需要执行下面的modprobe 8021q

   modprobe 8021q
   apt install vlan -y

#######其他方法,该步骤是插叙,可忽略####
##添加vlan
   vconfig add eth0 100
   ifconfig eth0.100 192.168.100.2 netmask 255.255.255.0
#删除vlan
   vconfig rem eth0.100
###################################


#第二:编辑网卡配置文件

auto enp3s0f1.2503
iface enp3s0f1.2503 inet static
	address 58.82.243.153
	netmask 255.255.255.128
	gateway 58.82.243.129
	dns-nameservers 114.114.114.114
auto enp3s0f1.2908
iface enp3s0f1.2908 inet static
	address 58.82.210.130
	netmask 255.255.255.128
	gateway 58.82.210.129

#第三:配置策略路由,
	ip route flush table 100
	ip rou add default via 58.82.243.129 dev enp3s0f1.2503 src 58.82.243.153 table 100 prio 50
	ip rule add from 58.82.243.153 table 100

	ip route flush table 200
	ip rou add default via 58.82.210.129 dev enp3s0f1.2908 src 58.82.210.130 table 200 prio 50
	ip rule add from 58.82.210.130 table 200

#第四:重启系统或者重启网络服务


声明:本文为原创,作者为 辣条①号,转载时请保留本声明及附带文章链接:https://boke.wsfnk.com/archives/649.html

最后编辑于:2018/12/27作者: 辣条①号

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注