OpenVPN/网桥

来自 ArchWiki
(重定向自 OpenVPN Bridge)

本页面描述了如何在 Arch Linux 上创建网络桥接,并使用基于 IP 二层以太网桥接(TAP)而不是基于 IP 三层 IP 隧道(TUN)来托管 OpenVPN 服务器。通用的 OpenVPN 页面更详细地描述了 PAM 身份验证或 OpenSSL 安全证书的设置。

简介

OpenVPN 文档页面全面概述了 OpenVPN 支持的服务器端和客户端选项。在隧道模式下设置 OpenVPN 并控制流量路由更容易,通常建议这样做,如果它能满足您的目的。但是,某些网络应用程序(例如 Windows 文件共享)依赖于以太网级别的网络广播,并受益于认为它们在物理上位于同一子网中,而软件桥接服务于此目的。

有多种方法可以设置桥接。动态方法是 OpenVPN 将在其系统上管理自己的网桥,并将自行启动、停止和配置它。这是设置桥接的最快方法,尽管它会在 OpenVPN 启动和停止时中断其他网络服务。如果系统要管理自己的网桥,可能是因为除了 OpenVPN 的虚拟网络适配器之外,还有其他虚拟网络适配器连接到该网桥,那么最好使用静态方法。

动态网桥安装

您将需要安装 OpenVPN 和 Linux 桥接实用程序,这些实用程序在 openvpnbridge-utils 软件包中可用。要使用下面的示例脚本,您还需要安装 dhcpcd 软件包。

动态网桥配置

OpenVPN 将自动为配置文件中指定的名称创建/销毁 TAP 设备。示例配置文件中未显示 TUN 或 TAP 通用的 OpenVPN 设置,仅显示影响 TAP 模式的设置。确保在编写 updown 脚本后,它们是可执行的

(TUN 和 TAP 通用的部分已省略)

/etc/openvpn/server.conf
# this uses a dhcp server, server-side
#  clients must support binding their dhcp client to their tap adapter
# do not append 'nogw' if using dhcp
server-bridge
# can specify interface, like tap0 or tap1
#  or use up/down routing scripts to handle
#  more than one, if needed
dev tap0
# needed to call scripts like up/down
#  which call external programs within the scripts
script-security 2
# user defined scripts for adding/removing tap to bridge
#  'dev mtu link_mtu ifconfig_local_ip ifconfig_remote_ip' are appended if set
# make sure 'user' has permission to run 'down' ('up' will be root)
up "up br0 eth0"
down "down br0 eth0"
# call 'down' before TUN/TAP close
down-pre
# drop root priveledges once connected
#  good idea, for servers running on linux
# 'up' script not affected, 'down' script is
;user nobody
;group nobody
/etc/openvpn/up
#!/bin/bash
br=$1
eth=$2
dev=$3
mtu=$4
cd /usr/bin/

# only if you start dhcpcd and leave it
#  running for eth
#dhcpcd -k $eth

# needed if script is run independently
# but when run through openvpn
# openvpn will do this automatically
#  could also use 'ip tuntap ..'
#openvpn --mktun --dev $dev

brctl addbr $br
# set forwarding delay to 0
#  otherwise dhcp called below would timeout
brctl setfd $br 0
brctl addif $br $eth
# order matters here.. right now there is only
#  one mac in the bridge's table
# if there were two.. there is no guarantee
#  which would be passed to the dhcp server
dhcpcd $br
brctl addif $br $dev

ip link set $eth up promisc on mtu $mtu
ip link set $dev up promisc on mtu $mtu
/etc/openvpn/down
#!/bin/bash
br=$1
eth=$2
cd /usr/bin/

dhcpcd -k $br

ip link set $br down
brctl delbr $br

# needed if script is run independently
# but when run through openvpn
# openvpn will do this automatically
#  could also use 'ip tuntap ..'
#openvpn --rmtun --dev $dev

# only if you start dhcpcd and leave it
#  running for eth
#dhcpcd $eth

这些示例用于 dhcp。如果您要使用静态 IP 地址,则需要进行相应的调整。

使用 Systemd

OpenVPN systemd 脚本默认在 /etc/openvpn 文件夹中查找 <name>.conf 文件。因此,假设您有一个名为 server.conf 的文件,您可以启用并启动 openvpn@server

请注意同时启用 dhcpcd(即 dhcpcd@eth0.service)。它有可能(尽管不太可能)在 OpenVPN 之后完成并破坏 OpenVPN 的 dhcp 设置。您可能可以禁用 dhcpcd@eth0.service,因为您知道 openvpn@server.service 将无论如何都会重置 dhcp。

警告: 静态网桥部分根本没有描述使用 systemd 的方法。此外,它可能包含过时的信息。应在某个时候进行修订。

静态网桥安装

您要做的第一件事是安装这些软件包:openvpnbridge-utilsnetctl

静态网桥配置

早期版本的 OpenVPN 团队或各种 Linux 软件包维护者提供的 OpenVPN 指南提供了在启动 OpenVPN 时构建网桥并在关闭 OpenVPN 时销毁网桥的示例脚本。

但是,这是一种有点过时的方法,因为从 2.1.1 开始,OpenVPN 默认不允许自身调用外部脚本或程序,除非显式启用,这是出于安全原因。

此外,与网络初始化过程的所有其他部分相比,构建网桥相对较慢。(事实上,非常慢,以至于 dhcpcd 会在网桥准备就绪之前超时。请参阅#静态网桥故障排除。)此外,当在配置更改后重新启动 OpenVPN 时,没有理由重建工作网桥,从而中断所有其他网络应用程序。因此,建议按如下方式设置静态网桥配置。

要为您的服务器创建 OpenVPN 网桥,您将必须使用netctl并创建两个网络配置文件 - 一个用于 tap 接口,另一个用于网桥。

转到 /etc/netctl 并将 tuntap 示例文件复制到该目录

# cd /etc/netctl/
# cp examples/tuntap openvpn_tap

现在编辑 openvpn_tap 以创建 tap 接口。它可能看起来像这样

/etc/netctl/openvpn_tap
Description='tuntap connection'
Interface=tap0
Connection=tuntap
Mode='tap'
User='nobody'
Group='nobody'

不要在此处配置 IP 地址,这将为网桥接口完成!

要创建 bridge 配置文件,请复制示例文件

# cp examples/bridge openvpn_bridge

现在编辑 openvpn_bridge。它可能看起来像这样

/etc/netctl/openvpn_bridge
Description="Bridge connection"
Interface=br0
Connection=bridge
BindsToInterfaces=(eth0 tap0)
IP=static
Address=('192.168.11.1/24')
Gateway='192.168.11.254'
DNS=('192.168.11.254')

有关更多信息,例如如何使用 DHCP,请查看 netctl 文章。

现在启用并启动这两个配置文件,使用

# netctl enable openvpn_tap
# netctl enable openvpn_bridge
# netctl start openvpn_tap
# netctl start openvpn_bridge

静态网桥故障排除

无法启动网络

这可能是因为您在网桥上使用 DHCP,并且设置网桥所花费的时间比 dhcpcd 愿意等待的时间长。您可以通过在您的网桥网络配置文件 (openvpn_bridge) 中设置 FWD_DELAY 参数来解决此问题。从值 5 开始并减小它,直到它起作用为止。

使用 DHCP 时网桥上没有 IP 地址

您可能需要在通过 DHCP 请求 IP 之前释放分配给您的以太网接口的 IP 地址。为此

dhcpcd -k

然后,修改 dhcpcd 配置文件以确保未将 ip 地址分配给以太网接口(在本例中为 enp3s0

/etc/dhcpcd.conf
denyinterfaces enp3s0

在文件末尾附近(假设您的网桥名为 br0

/etc/dhcpcd.conf
interface br0

现在按照上述说明创建网络桥接,然后运行 dhcpcd 以将 ip 地址分配给您的接口。检查以查看 ip addr 是否显示分配给网桥(即 br0)的有效 ip 地址。

更多资源

  • OpenVPN - 关于配置 OpenVPN 的通用页面,包括设置身份验证方法。