VLAN

什么是 VLAN

VLAN ( Virtual Local Area Network )又称虚拟局域网,是指在交换局域网的基础上,采用网络管理软件构建的可跨越不同网段、不同网络的端到端的逻辑网络。一个 VLAN 组成 一个逻辑子网, 即一个逻辑广播域,它可以覆盖多个网络设备,允许处于不同地理位置的网 络用户加入到一个逻辑子网中。

使用 VLAN 优点

  1. 控制广播风暴

    一个 VLAN 就是一个逻辑广播域,通过对VLAN的创建,隔离了广播,缩小了广播范围,可以控制广播风暴的产生。

  2. 提高网络整体安全性

    通过路由访问列表和 MAC 地址分配等VLAN划分原则,可以控制用户访问权限和逻辑网段大小,将不同用户群划分在不同VLAN从而提高交换式网络的整体性能和安全性。

  3. 网络管理简单、直观

    对于交换式以太网, 如果对某些用户重新进行网段分配, 需要网络管理员对网络系统的 物理结构重新进行调整,甚至需要追加网络设备,增大网络管理的工作量。而对于采用VLAN 技术的网络来说,一个 VLAN 可以根据部门职能、对象组或者应用将不同地理位置的网络用 户划分为一个逻辑网段。在不改动网络物理连接的情况下可以任意地将工作站在工作组或子 网之间移动。利用虚拟网络技术,大大减轻了网络管理和维护工作的负担,降低了网络维护 费用。在一个交换网络中,VLAN 提供了网段和机构的弹性组合机制

物理网卡、子网卡、虚拟 VLAN 网卡的关系:

  1. 物理网卡:服务器上物理网络接口设备,也就是要配置 trunk 的具体接口。
  2. 子网卡:子网卡并不是网络接口设备,但是可以作为网络接口在系统中出现,如 eth0:1 、 eth1:2 这种网络接口。必须要依赖于物理网卡,可以与物理网卡同时在系统中存 在并使用不同的 IP 地址,而且也拥有它们自己的网络接口配置文件。但是所依赖的物理网 卡 down 掉时子网卡也不能工作。
  3. 虚拟 VLAN 网卡:虚拟 VLAN 网卡也不是物理网络接口设备,可以作为网络接口在系统 中出现,与子网卡不同的是,没有自己的配置文件。是通过将物理网加入不同的 VLAN 而生 成的 VLAN 虚拟网卡。如果将一个物理网卡添加到多个 VLAN 当中去的话,就会有多个 VLAN 虚拟网卡出现,相关的 VLAN 信息都是保存在/proc/net/vlan/config 这文件中的, 以 eth0.1、eth1.2 命名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
[root@li]# modinfo 8021q # 查看是否有 vlan 模块
[root@li]# modprobe 8021q # 添加模块
[root@li]# vconfig add eth0 10 # 给eth0添加vlan id 10
[root@li]# vconfig add eth0 20 # 给eth0添加vlan id 20
[root@li]# vconfig set_flag eth0.10 1 1 # 设置VLAN的REORDER_HDR参数,默认就行了。
[root@li]# vconfig set_flag eth0.20 1 1 # 同上
[root@TSG-li]# cat /proc/net/vlan/eth0.10 # 查看eth0.10参数
eth0.10 VID: 10 REORDER_HDR: 1 dev->priv_flags: 81
total frames received 1
total bytes received 68
Broadcast/Multicast Rcvd 1
total frames transmitted 3
total bytes transmitted 270
total headroom inc 0
total encap on xmit 3
Device: eth0
INGRESS priority mappings: 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0
EGRESS priority Mappings:

[root@li]# ifconfig eth0 0.0.0.0
[root@li]# ifconfig eth0.10 192.168.10.10 netmask 255.255.255.0 up
[root@li]# ifconfig eth0.20 192.168.20.10 netmask 255.255.255.0 up
[root@li]# ifconfig eth0.10
eth0.10 Link encap:Ethernet HWaddr 00:0C:29:27:D6:74
inet addr:192.168.10.10 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe27:d674/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:68 (68.0 b) TX bytes:270 (270.0 b)

[root@li]# ifconfig eth0.20
eth0.20 Link encap:Ethernet HWaddr 00:0C:29:27:D6:74
inet addr:192.168.20.10 Bcast:192.168.20.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe27:d674/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1 errors:0 dropped:0 overruns:0 frame:0
TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:68 (68.0 b) TX bytes:176 (176.0 b)

[root@li]# brctl addbr br0 # 创建一个网桥
[root@li]# brctl show
[root@li]# brctl addif br0 eth0.10 # 把vlan eth0.10加到网桥上
[root@li]# brctl addif br0 eth0.20 # 把vlan eth0.20加到网桥上
[root@li]# brctl show

bridge name bridge id STP enabled interfaces
br0 8000.000c29f59487 no eth0.10,eth0.20

#STP enabled为no,可以用brctl stp <bridge> <state> 控制网桥是否加入STP树中,<state>可以是'on'或者'yes'表示加入stp树中,'off'表示关闭stp。

[root@li]# brctl stp br0 yes
[root@li]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29f59487 yes eth0.10,eth0.20

[root@li]# brctl stp br0 off
[root@li]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29f59487 no eth0.10,eth0.20

[root@li]# ifconfig br0 172.16.1.254/24 # 配置网桥br0的ip # ip a a 172.16.1.254/24 dev br0

[root@li]# ifconfig eth0.10 down
[root@li]# ip link set eth0.10 name eth10 # 改eth0.10网卡名称
[root@li]# ifconfig eth10 up
[root@li]# ifconfig eth10

[root@li]# brctl showmacs br0 #查看Mac地址和接口的对应关系,即MAC表,思科交换机设备叫CAM表

[root@li]# vconfig rem eth0.10 #删除vlan
[root@li]# vconfig rem eth0.20
[root@li]# ifconfig br0 down
[root@li]# brctl delbr br0 #删除网桥

brctl

有五台主机。其中一台主机装有linux ,安装了网桥模块,而且有四块物理网卡,分别连接同一网段的其他主机。我们希望其成为一个网桥,为其他四台主机(IP分别为192.168.1.2 ,192.168.1.3,192.168.1.4,192.168.1.5) 之间转发数据包。同时,为了方便管理,希望网桥能够有一个IP(192.168.1.1),那样管理员就可以在192.168.1.0/24网段内的主机上telnet到网桥,对其进行配置,实现远程管理。

1
brctl addbr br0 #建立一个逻辑网段,名称为br0

建立一个逻辑网段之后,我们还需要为这个网段分配特定的端口。在Linux中,一个端口实际上就是一个物理网卡。而每个物理网卡的名称则分别为eth0,eth1,eth2,eth3。我们需要把每个网卡一一和br0这个网段联系起来,作为br0中的一个端口。

1
2
3
4
brctl addif br0 eth0 #让eth0成为br0的一个端口
brctl addif br0 eth1 #同上
brctl addif br0 eth2 #同上
brctl addif br0 eth3 #同上

网桥的每个物理网卡作为一个端口,运行于混杂模式,而且是在链路层工作,所以就不需要IP了。

1
2
3
4
ifconfig eth0 0.0.0.0
ifconfig eth1 0.0.0.0
ifconfig eth2 0.0.0.0
ifconfig eth3 0.0.0.0
1
ifconfig br0 192.168.1.1 #给br0的虚拟网卡配置IP,那样就能远程管理网桥。