如果以太网桥中的 eth0 没有 IPv4,则主机无法访问

如果以太网桥中的 eth0 没有 IPv4,则主机无法访问

我目前正在尝试使用以下方法配置以太网桥接 VPNOpenVPN。但是,每当我尝试设置以太网桥时,服务器(Raspberry Pi)都会与内部网络断开连接。我使用以下脚本来初始化我的以太网桥

#!/bin/bash
# Define physical ethernet interface to be bridged
# with TAP interface(s) above.
eth="eth0"
eth_ip="192.168.144.200"
eth_netmask="255.255.255.0"
eth_broadcast="192.168.144.255"
eth_gateway="192.168.144.1"
eth_mac="B8:27:EB:48:6D:E3"


# Define Bridge Interface
br="br0"

# Define list of TAP interfaces to be bridged together
tap="tap0"

#################################
# Set up Ethernet bridge on Linux
# Requires: bridge-utils
#################################

for t in $tap; do
    openvpn --mktun --dev $t
done

brctl addbr $br
brctl addif $br $eth

for t in $tap; do
    brctl addif $br $t
done

for t in $tap; do
    ifconfig $t 0.0.0.0 promisc up
    iptables -A INPUT -i $t -j ACCEPT
done

iptables -A INPUT -i $br -j ACCEPT
iptables -A FORWARD -i $br -j ACCEPT

ifconfig $eth 0.0.0.0 promisc up
ifconfig $br $eth_ip netmask $eth_netmask broadcast $eth_broadcast
ip link set $br address $eth_mac
dhclient $br
ifconfig $br promisc up
route add default gw 192.168.144.1

在调试了整个故事之后,我发现

ifconfig $eth 0.0.0.0 promisc up

导致连接丢失。桥接后的接口配置为

br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.144.200  netmask 255.255.255.0  broadcast   192.168.144.255
    inet6 fe80::7b9:e278:843e:6f22  prefixlen 64  scopeid 0x20<link>
    ether b8:27:eb:48:6d:e3  txqueuelen 1000  (Ethernet)
    RX packets 147  bytes 23549 (22.9 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 155  bytes 75012 (73.2 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1500
    inet6 fe80::3abb:5937:1299:3dc5  prefixlen 64  scopeid 0x20<link>
    ether b8:27:eb:48:6d:e3  txqueuelen 1000  (Ethernet)
    RX packets 1590  bytes 208685 (203.7 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 1422  bytes 314068 (306.7 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 18  bytes 1244 (1.2 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 18  bytes 1244 (1.2 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

tap0: flags=4355<UP,BROADCAST,PROMISC,MULTICAST>  mtu 1500
    ether 5e:f6:5f:df:6b:10  txqueuelen 100  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.144.150  netmask 255.255.255.0  broadcast 192.168.144.255
    inet6 fe80::9cd2:30b9:74fc:c92d  prefixlen 64  scopeid 0x20<link>
    ether b8:27:eb:1d:38:b6  txqueuelen 1000  (Ethernet)
    RX packets 366  bytes 111524 (108.9 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 156  bytes 30687 (29.9 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

有什么办法可以解决这个问题吗?通常只有桥接接口才应该有 IP 地址。但如果我没有为 eht0 分配 IP 地址,终端连接就会中止,我无法再连接。

笔记: 我已经检查过 iptables 并且 net.ipv4.ip_forward 设置为 1

答案1

这就是我设法保持连接正常运转的方法。

我使用了一个脚本,因为我是通过远程连接的eth0- 关闭它会导致连接关闭。

与问题中的脚本相比,我所做的更改首先是确保 DHCP 服务未运行,以防止其(重新)分配 IP 地址,其次eth0冲洗 eth0删除地址。

# Define Bridge Interface
br="br0"

# Define list of TAP interfaces to be bridged,
# for example tap="tap0 tap1 tap2".
tap="tap0"

# Define physical ethernet interface to be bridged
# with TAP interface(s) above.
eth="eth0"
eth_ip="192.168.178.20"
eth_netmask="255.255.255.0"
eth_broadcast="192.168.178.255"
eth_gateway="192.168.178.1"

for t in $tap; do
  openvpn --mktun --dev $t
done

brctl addbr $br
brctl addif $br $eth

# Stop dhcpcd if running
systemctl stop dhcpcd

for t in $tap; do
  brctl addif $br $t
done

for t in $tap; do
  ifconfig $t 0.0.0.0 promisc up
done

#ip addr del $eth_ip dev $eth
ip addr flush dev $eth

ifconfig $eth 0.0.0.0 promisc up

ifconfig $br $eth_ip netmask $eth_netmask broadcast $eth_broadcast
route add default gw $eth_gateway $br


for t in $tap; do
  ifconfig $t up
done
ifconfig $br up

根据您的防火墙规则(无论您是否有活动防火墙),您可能需要相应地设置规则(如上所述)。

相关内容