linux网络路由管理之六(如何创建带vlan标签的网卡子接口)(centos+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

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

附、另一种debian的配置方法

# 第一、配置内核加载8021q模块
  #下面这两软件,第一个要是装不上就算了,第二个默认自带了
root@localhost:/# apt-get install vlan
root@localhost:/# apt-get install ifenslave

root@localhost:/# modprobe 8021q
root@localhost:/# echo "bonding" | tee -a /etc/modules
root@localhost:/# echo "8021q" | tee -a /etc/modules

# 第二、编辑网卡配置文件
root@localhost:/# cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
      address 192.168.115.252
      netmask 255.255.255.0

auto eth1  

auto vlan50
iface vlan50 inet static
    vlan-raw-device eth1  
    address 111.31.218.82
    netmask 255.255.255.248
    gateway 111.31.218.81
    dns-nameservers 114.114.114.114

auto vlan100
iface vlan100 inet static
    vlan-raw-device eth1  
    address 111.31.218.98
    netmask 255.255.255.248
    dns-nameservers 114.114.114.114
    hwaddress ether 00:00:39:9E:58:3C

auto vlan150
iface vlan150 inet static
    vlan-raw-device eth1  
    address 111.31.218.90
    netmask 255.255.255.248
    dns-nameservers 114.114.114.114
    hwaddress ether 00:00:39:9E:58:3D

# 第三、添加路由表文件(就前三行)
root@localhost:/# cat /etc/iproute2/rt_tables
50 vlan50
100 vlan100
150 vlan150
255     local
254     main
253     default
0       unspec

# 第四、配置策略路由,并实现开机自启动(如下策略路由,需手动执行一次)
root@localhost:/# /etc/init.d/networing resatrt
root@localhost:/# chmod +x /etc/rc.local
root@localhost:/# systemctl start rc-local   # 可以不做
root@localhost:/# systemctl enable rc-local    # 可以不做
root@localhost:/# cat /etc/rc.local
ip route flush table vlan50
ip rule add from 111.31.218.82 table vlan50
ip route add default via 111.31.218.81 dev vlan50 src 111.31.218.82 table vlan50
ip route flush table vlan100
ip rule add from 111.31.218.98 table vlan100
ip route add default via 111.31.218.97 dev vlan100 src 111.31.218.98 table vlan100
ip route flush table vlan150
ip rule add from 111.31.218.90 table vlan150
ip route add default via 111.31.218.89 dev vlan150 src 111.31.218.90 table vlan150
exit 0

附、debian9 的另一种网卡配置(其实都是一样的)

auto eth2
iface eth2 inet manual
    bond-master bond0
iface eth2 inet6 manual
    bond-master bond0

auto eth3
iface eth3 inet manual
    bond-master bond0
iface eth3 inet6 manual
    bond-master bond0

auto bond0
iface bond0 inet static
    bond-mode 802.3ad
    bond-slaves eth2 eth3
    bond-miimon 100
    bond-downdelay 200
    bond-updelay 200

auto bond0.449
iface bond0.449 inet static
    vlan-raw-device bond0
    address 223.83.0.198
    netmask 255.255.255.192
    gateway 223.83.0.193

auto bond0.460
iface bond0.460 inet6 static
    vlan-raw-device bond0
    address 2409:8738:5011:D10::2/120
    gateway 2409:8738:5011:D10::1
声明:本文为原创,作者为 辣条①号,转载时请保留本声明及附带文章链接:https://boke.wsfnk.com/archives/468.html
谢谢你请我吃辣条谢谢你请我吃辣条

如果文章对你有帮助,欢迎点击上方按钮打赏作者

最后编辑于:2024/9/21作者: 辣条①号

目标:网络规划设计师、系统工程师、ceph存储工程师、云计算工程师。 不负遇见,不谈亏欠!

暂无评论

发表回复

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

arrow grin ! ? cool roll eek evil razz mrgreen smile oops lol mad twisted wink idea cry shock neutral sad ???

文章目录