网络配置
大约 5 分钟
桥接网络
## 创建桥接连接
nmcli connection add type bridge ifname br0 con-name br0
## 添加网络接口到桥接连接
nmcli connection add type bridge-slave ifname enp1s0 master br0
nmcli connection add type bridge-slave ifname enp2s0 master br0
nmcli connection add type bridge-slave ifname enp3s0 master br0
nmcli connection add type bridge-slave ifname enp4s0 master br0
nmcli connection add type bridge-slave ifname enp5s0 master br0
nmcli connection add type bridge-slave ifname enp6s0 master br0
nmcli connection add type bridge-slave ifname enp7s0 master br0
nmcli connection add type bridge-slave ifname enp8s0 master br0
## 配置桥接连接的IPv4地址
# nmcli connection modify br0 ipv4.method manual ipv4.address 192.168.60.100/24
# nmcli connection modify br0 ipv4.addresses "192.168.100.26/24"
# nmcli connection modify br0 ipv4.gateway 192.168.100.2
# nmcli connection modify br0 ipv4.dns 8.8.8.8
## 设置IP地址:192.168.13.166
nmcli connection modify br0 ipv4.method manual ipv4.address 192.168.13.166/24 ipv4.gateway 192.168.13.1
## 添加IP地址:192.168.3.166
nmcli con mod br0 +ipv4.addresses "192.168.3.166/24"
# 重新加载连接
nmcli connection reload
# 启用桥接连接
nmcli connection up br0
systemctl restart network
## 删除桥接
nmcli connection delete br0
``
```bash
## 安装bridge-utils
apt-get install bridge-utils
yum instakk -y bridge-utils
## 创建桥接设备: 使用brctl命令创建一个桥接设备。假设你要创建一个名为br0的桥接设备,可以使用以下命令:
brctl addbr br0
## 将网络接口添加到桥接设备: 将要桥接的网络接口(网卡)添加到新创建的桥接设备中。例如,假设你有两个网卡eth0和eth1,可以使用以下命令:
brctl addif br0 eth0
brctl addif br0 eth1
## 激活桥接设备: 使用以下命令激活桥接设备:
ifconfig br0 up
## 配置IP地址: 为桥接设备分配一个IP地址。例如:
ifconfig br0 192.168.16.100 netmask 255.255.255.0
## 禁用原始网络接口: 如果你希望通过桥接设备来处理网络流量,可以禁用原始网络接口。例如:
ifconfig eth0 down
ifconfig eth1 down
## 或者使用ip link set命令:
ip link set dev eth0 down
ip link set dev eth1 down
ebtables
ebtables(Ethernet Bridge Tables)是用于Linux系统上的一个桥接表管理工具,它主要用于管理Linux内核中的以太网桥接设备的过滤规则。
- 以太网桥接设备管理: ebtables主要用于管理Linux内核中的以太网桥接设备。桥接设备允许将多个网络接口连接在一起,形成一个共享同一网络的逻辑链路。
- 流量过滤: ebtables允许用户定义规则,以控制通过桥接设备的数据流量。这包括允许或拒绝特定的MAC地址、以太网协议类型(如IPv4或IPv6)、以及其他与以太网帧相关的信息。
- MAC地址过滤: 用户可以使用ebtables指定允许或拒绝通过桥接设备的特定MAC地址的流量。这有助于实现网络访问控制。
- 协议过滤: 除了MAC地址过滤外,ebtables还允许用户基于以太网帧的协议类型过滤流量。这意味着可以根据帧中的协议信息来控制流量的通过与阻塞。
- 连接跟踪: ebtables支持连接跟踪功能,可以用于追踪与桥接设备相关的连接信息。
- 虚拟局域网(VLAN)支持: ebtables可以与VLAN一起使用,帮助管理VLAN标记的流量。
## 查看ebtables规则
ebtables -L
## 查看版本
ebtables --versio
应用
## 查看系统上是否存在桥接设备,可通过一下命令查看
ip link show type bridge
yum install -y bridge-utils && brctl show
## 查看桥接设备br0的相关信息
brctl showmacs br0
## 重新加载网络接口,重新加载网络接口可能有助于应用新规则
sudo ip link set dev br0 down
sudo ip link set dev br0 up
## 查看服务状态并启动服务
systemctl status ebtables
systemctl start ebtables
systemctl enable ebtables
## 获取机器的mac地址
ip link show | grep -o -E 'ether [^ ]+' | awk '{print $2}'
## 仅允许mac地址为00:0c:29:1a:53:38的设备访问80端口
ebtables -A INPUT -p ip --ip-proto tcp --ip-dport 443 -s 00:0c:29:1a:53:38 -j ACCEPT
ebtables -A INPUT -p ip --ip-proto tcp --ip-dport 443 -j DROP
## 导出ebtables规则
sh -c 'ebtables-save > /root/ebtables.rules'
## 导入ebtable规则
sh -c 'ebtables-restore < /root/ebtables.rules'
脚本更新白名单
mkdir -p /opt/ebtables/ && cd /opt/ebtables/
## mac地址白名单
cat > mac_list.txt << EOF
11:22:33:44:55:66
22:33:44:55:66:77
EOF
## 更新ebtables规则
cat > whitelist_script.sh << EOF
#!/bin/bash
# 清空旧的规则
ebtables -F
# 从文件中读取MAC地址列表
cat mac_list.txt | while read -r mac_address; do
ebtables -A INPUT -p ip --ip-proto tcp --ip-dport 443 -s "\${mac_address}" -j ACCEPT
done
## 拒绝其他mac地址访问
ebtables -A INPUT -p ip --ip-proto tcp --ip-dport 443 -j DROP
## 备份当前的规则
sh -c 'ebtables-save > \$(date +%Y%m%d%H%M%S)-ebtables.rules'
EOF
chmod +x whitelist_script.sh
bash whitelist_script.sh
``
## iptables
> iptables 是一个用于配置 Linux 内核防火墙规则的工具。它允许系统管理员定义如何处理网络流量,包括允许或拒绝特定端口、IP 地址或协议的流量。iptables 是 Linux 中一个强大而灵活的防火墙管理工具,常用于网络安全、流量控制和网络地址转换(NAT)等方面
### 应用
```bash
## 清空所有的 INPUT 链规则
iptables -F INPUT
## 针对 80 端口进行客户端的 MAC 地址过滤
iptables -A INPUT -p tcp --dport 80 -m mac --mac-source 11:22:33:44:55:66 -j ACCEPT
## 拒绝其他 MAC 地址
iptables -A INPUT -p tcp --dport 80 -j DRO
脚本更新白名单(已验证)
## 白名单列表
cat > mac_whitelist.txt << EOF
11:22:33:44:55:66
22:33:44:55:66:77
EOF
## 更新到iptables中
cat > whitelist_script.sh << EOF
#!/bin/bash
# 定义变量
WHITELIST="mac_whitelist.txt"
PORT=80
# 函数:加载 MAC 地址到 iptables
load_mac_addresses() {
local whitelist=\$1
local port=\$2
# 从文件中读取 MAC 地址列表,并允许这些地址
while IFS= read -r mac; do
iptables -A INPUT -p tcp --dport "\$port" -m mac --mac-source "\$mac" -j ACCEPT
done < "\$whitelist"
}
## 清空所有的 INPUT 链规则
iptables -F INPUT
## 调用函数加载 MAC 地址
load_mac_addresses "\$WHITELIST" "\$PORT"
## 拒绝其他 MAC 地址
iptables -A INPUT -p tcp --dport \$PORT -j DROP
## 备份当前的规则
iptables-save > /root/\$(date +%Y%m%d%H%M%S)-iptables.rules
EOF
## 赋予执行权限并执行脚本
chmod +x whitelist_script.sh
bash whitelist_script.s