Linux网桥配置
CentOS:
1、配置临时网桥,重启后风格配置丢失
[root@CentOS ~]# yum -y install bridge-utils
[root@CentOS ~]# brctl addbr br0 # 创建网桥逻辑接口
[root@CentOS ~]# brctl addif br0 eth0 # 将eth0网卡添加到网桥中
[root@CentOS ~]# brctl addif br0 eth1 # 将eth1网卡添加到网桥中
[root@CentOS ~]# brctl show br0 # 查看br0网桥状态
[root@CentOS ~]# brctl setageing br0 0 # 将br0的MAC老化时间设置为0,把br0模拟成一个hub
[root@CentOS ~]# ifconfig eth0 promisc # 将eth0网卡设为混杂模式
[root@CentOS ~]# ifconfig eth0 -promisc # 取消eth0网卡的混杂模式
[root@CentOS ~]# ip link set eth1 promisc on/off # 在eth1口开启或关闭端口混杂模式
[root@CentOS ~]# ip link show eth0 # 如果看到PROMISC字样,说明eth0网卡处于混杂模式
2: eth0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:e2:be:42 brd ff:ff:ff:ff:ff:ff
[root@CentOS ~]# ifconfig br0 # 查看网桥状态
[root@CentOS ~]# ifconfig br0 10.10.10.82/24 # 给桥网卡设置IP地址
[root@CentOS ~]# brctl hairpin br0 eth1 on/off # 在eth1上开启或关闭发夹模式
[root@CentOS ~]# bridge -d link show # 查看接口优先级、开销和是否开启发夹模式,
发夹模式的定义
在默认情况下,网桥设备是不允许一个数据包从一个端口进来后,再从这个端口发出去的。但是,它允许你为这个端口开启 Hairpin Mode,从而取消这个限制。
注意:
1、着重注意数据包
2、发夹模式是为bridge上的端口设置的,而不是bridge本身
2、创建永久网桥
[root@CentOS ~]# vi /etc/sysconfig/network-scripts/ifcfg-br0 # 创建网桥配置文件
STP=yes # 开启stp,关闭stp:STP=no,stp默认为关闭 DELAY=15 # 设置STP中的转发延迟时间,单位为秒,默认值是15秒。当一个端口加入网桥时,它将在15秒后切换到转发状态 BRIDGING_OPTS="priority=32768 hello_time=2 max_age=20 ageing_time=0 multicast_hash_max=512" # 设置MAC地址的老化时间(ageing time)为0,把br0配置成一个hub TYPE=Bridge NAME=br0 DEVICE=br0 BOOTPROTO=static IPADDR=10.47.8.17 NETMASK=255.255.255.0 GATEWAY=10.47.8.1 DNS1=223.5.5.5 DNS2=10.0.0.9 ONBOOT=yes
[root@CentOS ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 # 将eth0网卡加入到网桥中
TYPE=Ethernet NAME=eth0 DEVICE=eth0 BOOTPROTO=none ONBOOT=yes BRIDGE=br0 PROMISC=yes # 将eth0网卡设为为混杂模式 BRIDGING_OPTS="priority=20 path_cost=200 hairpin_mode=1" # 设置端口优先级和端口开销,开启端口发夹模式
[root@CentOS ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth1 # 将eth1网卡加入到网桥中
TYPE=Ethernet NAME=eth1 DEVICE=eth1 BOOTPROTO=none ONBOOT=yes BRIDGE=br0 BRIDGING_OPTS=priority=30 # 设置端口优先级,端口开销默认为100
# 常见BRIDGING_OPTS选项 # 设置桥优先级,默认为32768 priority=32768 # 设置STP中的 Hello 时间间隔,默认值是2秒 hello_time=2 # 设置STP中的最大年龄时间,默认值是20秒 max_age=20 # 设置最大等待时间,通常用在STP中的某些场景 max_wait=<value> # 设置MAC地址表的老化时间,单位为秒,默认值为300 ageing_time=300 # 设置端口到根桥的路径开销,默认值是100 path_cost=100 # 设置端口的优先级,值越小优先级越高。默认值是32 priority=32 # 设置端口关闭发夹模式,默认为0,1为开启 hairpin_mode=0
Debian:
1、配置临时网桥
root@debian:~# apt-get install bridge-utils #安装依赖包
root@debian:~# brctl addbr br0 # 创建网桥
root@debian:~# ifconfig br0 up # 启用网桥
root@debian:~# brctl show br0 # 查看网桥
root@debian:~# brctl addif br0 eth0 eth1 # 将网卡加入到网桥
root@debian:~# brctl delif br0 eth1 # 从网桥中删除eth1网卡
root@debian:~# ifconfig br0 down # 关闭网桥
root@debian:~# brctl delbr br0 # 删除网桥
root@debian:~# ip address help
root@debian:~# ip link set br0 up/down
root@debian:~# ip address show dev br0
root@debian:~# ip address add 192.168.120.10/24 dev br0
root@debian:~# ip address del 192.168.120.10/24 dev br0
2、创建永久网桥
root@debian:~# nano /etc/network/interfaces
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). source /etc/network/interfaces.d/* # The loopback network interface # auto lo br0 两行写成一行 auto lo auto br0 iface lo inet loopback iface br0 inet static iface eth0 inet manual iface eth1 inet manual address 10.1.1.122 broadcast 10.1.1.255 netmask 255.255.255.0 gateway 10.1.1.1 bridge_ports eth0 eth1 # 把eth0,eth1加入到网桥 bridge_stp off # disable Spanning Tree Protocol bridge_waitport 0 # no delay before a port becomes available bridge_fd 0 # no forwarding delay
Linux使用iptables实现端口流量镜像,不需要开启防火墙,直接使用iptables配置即可
# 注意使用iptables TEE实现端口镜像会改变源MAC、目的MAC # 将所有到达本机端口80的TCP流量复制并发送到192.168.1.100 iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TEE --gateway 192.168.1.100 # 将所有eth0入向的流量复制并发送到192.168.1.100 iptables -I PREROUTING -t mangle -i eth0 -j TEE --gateway 192.168.1.100 # 将所有eth0出向的流量复制并发送到192.168.1.100 iptables -I POSTROUTING -t mangle -o eth0 -j TEE --gateway 192.168.1.100
参考链接:
http://www.javashuo.com/article/p-dxtywwah-ue.html # Linux网络管理
https://www.cnblogs.com/nidey/p/6227963.html # CentOS7 配置网卡端口镜像
https://www.cnblogs.com/dpf-10/p/10682128.html # linux双网卡桥接,实现网卡流量镜像与转发