将 VPN 流量路由到第二个网络适配器时出现问题 - 请帮忙。

将 VPN 流量路由到第二个网络适配器时出现问题 - 请帮忙。

我正在运行 ubuntu server 16.04 和 openvpn。我想更改 vpnserver 上的设置,以便所有流量都通过当前连接 (192.168.1.254) 进入,而所有流量都通过我的第二个互联网连接 (192.168.2.3) 流出。

我当前的路由表:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.254   0.0.0.0         UG    0      0        0 ens33
10.8.0.0        10.8.0.2        255.255.255.0   UG    0      0        0 tun0
10.8.0.2        0.0.0.0         255.255.255.255 UH    0      0        0 tun0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 ens34

目前我可以在 ens33 上访问 vpn,但它将使用 ens33 作为默认网关 (192.168.1.254)。如果我将度量添加到接口,那么 ens34 度量 10 和 ens33 度量 100,我无法访问服务器。如果我更改/etc/ufw/before.rules

# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# I CHANGED THIS FROM DEFAULT ens33 to ens34   
-A POSTROUTING -s 10.8.0.0/8 -o ens34 -j MASQUERADE
COMMIT
# END OPENVPN RULE

我可以访问服务器进行连接,但不能通过浏览器访问互联网......

我的界面设置如下:

    # 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
iface lo inet loopback

# The primary network interface
#auto ens33
#iface ens33 inet dhcp
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.72
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.254
#metric 10

# the secondary network interface
auto ens34
iface ens34 inet  static
address 192.168.2.12
netmask 255.255.255.0
broadcast 192.168.2.255
gateway 192.168.2.3
dns-nameservers 8.8.4.4 8.8.8.8
#metric 100

我遗漏了路线规则之类的东西,但最近几天我一直在为此绞尽脑汁……有人能帮我吗?任何帮助我都会很感激!!提前谢谢!!<3<3<3

答案1

对我来说,你试图实现的目标并不十分清楚。假设你有两个以太网接口:eth0 和 eth1。eth0 连接到互联网,而 eth1 连接到你的家庭局域网。在这种情况下,你可以将服务器用作路由器,使连接到 eth1 的所有设备都可以通过 eth0 访问互联网。

为了实现这一点,您需要一个 dhcp 服务器,为连接到 eht1 接口的设备分配 IP。您还需要转发两个 eth 设备之间的流量。

Dhcp服务器安装与配置:

sudo apt-get install dnsmasq

sudo nano /etc/dnsmasq.conf

interface=eth1 # The interface for the dhcp server to listen to
dhcp-range=192.168.179.10,192.168.179.150,255.255.255.0,12h # Dhcp server IP adress + Range + Netmask + Leasetime

路由和转发

sudo nano /etc/sysctl.conf

取消注释该行

net.ipv4.ip_forward=1

并让更改生效:

sudo sysctl -p

修改 IP 表。我建议您为此编写一个脚本,这样您就可以快速进行更改,以防您需要进行实验。该脚本将删除所有 iptables 规则,将链规则设置为 ACCEPT,然后附加路由规则:

nano firewall-script.sh
/bin/bash #!/bin/bash
IPTABLES="/sbin/iptables"

# 默认接受一切。

$IPTABLES -P 输入接受
$IPTABLES -P 转发接受
$IPTABLES -P 输出接受

# 将 nat/mangle/raw 表的链设置为 ACCEPT
$IPTABLES -t nat -P 预路由接受
$IPTABLES -t nat -P 输出接受
$IPTABLES -t nat -P POSTROUTING 接受

$IPTABLES -t mangle -P 预路由接受
$IPTABLES -t mangle -P 输入接受
$IPTABLES -t mangle -P 转发接受
$IPTABLES -t mangle -P 输出接受
$IPTABLES -t mangle -P POSTROUTING 接受

# 清理 - 删除所有 iptables 规则。
#------------------------------------------------------------------------------

# 删除所有
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F

# 删除所有
$IPTABLES -X
$IPTABLES -t nat -X
$IPTABLES -t mangle -X

# 将所有数据包和计数器清零。
$IPTABLES -Z
$IPTABLES -t nat -Z
$IPTABLES -t mangle -Z

#------------------------------------------------------------------------------    

$IPTABLES -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$IPTABLES -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPTABLES -A FORWARD -i eth1 -o eth0 -j 接受

保存文件CTRL+X并使其可执行chmod +x firewall-script.sh

当您想要保存 iptables 规则时(否则它们将在重启后丢失),请安装sudo apt-get install iptables-persistent。如果您已经安装了它,请输入sudo dpkg-reconfigure iptables-persistent。我建议您不要保存我的脚本制定的规则,因为它会打开所有内容。我的 IPTABLES 脚本仅用于测试目的。

现在您应该能够将服务器用作路由器了。下一步是修改脚本以使用服务器的 vpn 连接。

启动您的 VPN 连接。建立连接后,输入ifconfig。您应该会看到一个 tun-device。我猜您的 vpn-connection 正在使用tun0eth1连接到您的 lan-homenetwork:

$IPTABLES -t nat -A POSTROUTING -o tun0 -j MASQUERADE
$IPTABLES -A FORWARD -i tun0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPTABLES -A FORWARD -i eth1 -o tun0 -j 接受

现在所有家庭流量都应通过您的 vpn 路由。希望这对您有所帮助。

相关内容