Linux节点VLAN Trunk配置指南
概述
本文档描述如何在Linux节点上配置VLAN Trunk,使单个物理网口能够承载多个VLAN流量。
前置条件
- 交换机端口已配置为Trunk模式
- 确认需要配置的VLAN ID
- 具有root权限
- 确认物理网口名称(如:eth0、ens33、bond0等)
配置步骤
步骤1:加载802.1q内核模块
# 加载VLAN支持模块
modprobe 8021q
# 验证模块是否加载
lsmod | grep 8021q
# 永久加载(可选)
echo "8021q" >> /etc/modules-load.d/8021q.conf
步骤2:备份原始配置
# 备份网络配置文件
cd /etc/sysconfig/network-scripts/
cp ifcfg-<物理接口> /tmp/ifcfg-<物理接口>.backup
# 示例
cp ifcfg-bond0 /tmp/ifcfg-bond0.backup
步骤3:清理物理接口配置
# 编辑物理接口配置文件
vim /etc/sysconfig/network-scripts/ifcfg-<物理接口>
# 注释或删除以下配置项
# IPADDR=xxx.xxx.xxx.xxx
# NETMASK=xxx.xxx.xxx.xxx
# GATEWAY=xxx.xxx.xxx.xxx
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# 保留必要配置
DEVICE=<物理接口>
BOOTPROTO=none
ONBOOT=yes
步骤4:创建VLAN子接口
方法A:使用ip命令创建(临时)
# 创建VLAN子接口
ip link add link <物理接口> name <物理接口>.<VLAN_ID> type vlan id <VLAN_ID>
# 示例:创建VLAN 100
ip link add link eth0 name eth0.100 type vlan id 100
方法B:创建持久化配置文件
# 创建VLAN子接口配置文件
vim /etc/sysconfig/network-scripts/ifcfg-<物理接口>.<VLAN_ID>
# 配置内容
DEVICE=<物理接口>.<VLAN_ID>
BOOTPROTO=none
ONBOOT=yes
IPADDR=<IP地址>
PREFIX=<子网掩码位数>
NETWORK=<网络地址>
VLAN=yes
# 示例:VLAN 100配置
DEVICE=eth0.100
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.100.10
PREFIX=24
NETWORK=192.168.100.0
VLAN=yes
步骤5:配置默认网关(可选)
# 编辑网络配置文件
vim /etc/sysconfig/network
# 添加或修改默认网关
GATEWAY=<网关IP>
# 注意:所有VLAN子接口将使用同一默认网关
步骤6:配置内核参数
6.1 编辑sysctl配置
vim /etc/sysctl.conf
# 添加反向路径过滤配置
# 格式根据操作系统版本选择:
# RedHat/CentOS 7.8 及以下版本
net.ipv4.conf.<物理接口>\<VLAN_ID>.rp_filter=2
# RedHat/CentOS 7.9、openEuler、麒麟V10
net.ipv4.conf.<物理接口>/<VLAN_ID>.rp_filter=2
# 示例配置
net.ipv4.conf.eth0/100.rp_filter=2
net.ipv4.conf.eth0/200.rp_filter=2
net.ipv4.conf.all.rp_filter=2
net.ipv4.conf.default.rp_filter=2
6.2 应用配置
# 使配置生效
sysctl -p
# 验证配置
sysctl net.ipv4.conf.<物理接口>/<VLAN_ID>.rp_filter
步骤7:激活网络接口
# 重启物理接口(如果修改了配置)
ifdown <物理接口>
ifup <物理接口>
# 激活VLAN子接口
ifup <物理接口>.<VLAN_ID>
# 示例
ifup eth0
ifup eth0.100
ifup eth0.200
多VLAN配置示例
配置3个VLAN(100, 200, 300)
# 1. 创建三个VLAN子接口
for vlan in 100 200 300; do
ip link add link eth0 name eth0.$vlan type vlan id $vlan
done
# 2. 创建配置文件
# VLAN 100
cat > /etc/sysconfig/network-scripts/ifcfg-eth0.100 << EOF
DEVICE=eth0.100
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.100.10
PREFIX=24
NETWORK=192.168.100.0
VLAN=yes
EOF
# VLAN 200
cat > /etc/sysconfig/network-scripts/ifcfg-eth0.200 << EOF
DEVICE=eth0.200
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.200.10
PREFIX=24
NETWORK=192.168.200.0
VLAN=yes
EOF
# VLAN 300(不配置IP)
cat > /etc/sysconfig/network-scripts/ifcfg-eth0.300 << EOF
DEVICE=eth0.300
BOOTPROTO=none
ONBOOT=yes
VLAN=yes
EOF
# 3. 配置sysctl
echo "net.ipv4.conf.eth0/100.rp_filter=2" >> /etc/sysctl.conf
echo "net.ipv4.conf.eth0/200.rp_filter=2" >> /etc/sysctl.conf
echo "net.ipv4.conf.eth0/300.rp_filter=2" >> /etc/sysctl.conf
sysctl -p
# 4. 激活所有接口
for vlan in 100 200 300; do
ifup eth0.$vlan
done
验证配置
1. 检查接口状态
# 查看所有网络接口
ip link show
# 查看VLAN接口详情
ip -d link show <物理接口>.<VLAN_ID>
# 查看IP配置
ip addr show
2. 检查VLAN标记
# 查看VLAN配置
cat /proc/net/vlan/config
# 查看特定VLAN接口信息
cat /proc/net/vlan/<物理接口>.<VLAN_ID>
3. 连通性测试
# 测试各VLAN网关连通性
ping -c 4 -I <物理接口>.<VLAN_ID> <网关IP>
# 示例
ping -c 4 -I eth0.100 192.168.100.1
4. 查看路由表
# 查看路由信息
ip route show
# 查看特定接口路由
ip route show dev <物理接口>.<VLAN_ID>
故障排查
常见问题及解决方案
1. VLAN接口无法启动
# 检查8021q模块是否加载
lsmod | grep 8021q
# 检查物理接口状态
ip link show <物理接口>
# 查看错误日志
journalctl -xe | grep -i vlan
2. 无法ping通其他VLAN
# 检查rp_filter设置
sysctl net.ipv4.conf.<物理接口>/<VLAN_ID>.rp_filter
# 检查防火墙规则
iptables -L -n -v
# 检查交换机端口配置
# 确认交换机端口允许相应VLAN通过
3. 重启后配置丢失
# 确认配置文件ONBOOT=yes
grep ONBOOT /etc/sysconfig/network-scripts/ifcfg-*
# 检查NetworkManager状态
systemctl status NetworkManager
# 如使用NetworkManager,可能需要:
nmcli con reload
回滚方案
如需回滚配置:
# 1. 删除VLAN子接口
ip link delete <物理接口>.<VLAN_ID>
# 2. 删除配置文件
rm -f /etc/sysconfig/network-scripts/ifcfg-<物理接口>.<VLAN_ID>
# 3. 恢复物理接口配置
cp /tmp/ifcfg-<物理接口>.backup /etc/sysconfig/network-scripts/ifcfg-<物理接口>
# 4. 移除sysctl配置
# 编辑/etc/sysctl.conf,删除添加的rp_filter配置
# 5. 重启网络服务
systemctl restart network
注意事项
-
生产环境操作建议
- 在维护窗口期间进行配置
- 准备回滚方案
- 确保有带外管理访问
-
安全考虑
- VLAN不提供加密,仅提供逻辑隔离
- 考虑使用防火墙规则限制VLAN间访问
- 定期审核VLAN配置和访问策略
-
性能优化
- 大量VLAN可能影响性能
- 监控网络接口错误和丢包
- 考虑使用硬件offload功能
-
兼容性
- 不同Linux发行版配置文件位置可能不同
- NetworkManager与传统network服务可能冲突
- 某些虚拟化环境可能需要特殊配置
附录:常用命令速查
# VLAN操作
modprobe 8021q # 加载模块
ip link add link eth0 name eth0.100 type vlan id 100 # 创建VLAN
ip link delete eth0.100 # 删除VLAN
ip -d link show eth0.100 # 查看VLAN详情
# 配置验证
cat /proc/net/vlan/config # 查看所有VLAN
ip addr show # 查看IP配置
ip route show # 查看路由表
ethtool eth0 # 查看物理接口状态
# 故障排查
tcpdump -i eth0 -nn -e vlan # 抓取VLAN流量
journalctl -u network # 查看网络服务日志
dmesg | grep -i vlan # 查看内核消息
1876

被折叠的 条评论
为什么被折叠?



