Tha*_*Don 14 networking ubuntu openvpn linux-networking ubuntu-16.04
摘要:我想连接到我的 VPN 并访问某些服务器,但对于所有其他流量,我想使用我的常规网络。
我在 VPS 上设置了 OpenVPN 服务器,我的server.conf文件如下所示:
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key # This file should be kept secret
dh dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
keepalive 10 120
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
log /var/log/openvpn.log
verb 4
push "route 10.132.0.0 255.255.0.0"
Run Code Online (Sandbox Code Playgroud)
我使用以下.ovpn文件来设置 VPN 连接:
client
dev tun
proto udp
remote <my.vpn.server.com> 1194
nobind
user nobody
group nogroup
persist-key
persist-tun
remote-cert-tls server
comp-lzo
verb 3
<ca>....</ca>
<cert>...</cert>
<key>...</key>
Run Code Online (Sandbox Code Playgroud)
最后,在 VPN 连接的网络管理器中,在 IPv4 设置下,我确保将“方法”设置为“仅自动(VPN)地址”。
VPN 连接正常,我可以访问我需要的所有内部服务器 (10.132.xx),但是我无法访问其他任何内容(例如 google.com)。除了我希望通过 VPN 路由的 10.132.xx IP 之外,我希望我的 eth0 设置用于所有内容。
PS 基于我尝试no-pull在 .ovpn 文件中使用的其他文章并在route那里添加我的设置但无济于事。
编辑 1:
运行ip a和traceroute连接到 VPN 时的结果:
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:dc:a6:ef brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
valid_lft 86320sec preferred_lft 86320sec
inet6 fe80::f3d1:6eb3:e13e:d61b/64 scope link
valid_lft forever preferred_lft forever
15: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100
link/none
inet 10.8.0.6 peer 10.8.0.5/32 brd 10.8.0.6 scope global tun0
valid_lft forever preferred_lft forever
$ traceroute google.com
google.com: Temporary failure in name resolution
Cannot handle "host" cmdline arg `google.com' on position 1 (argc 1)
Run Code Online (Sandbox Code Playgroud)
编辑2:结果ip r
$ ip r
default via 10.8.0.5 dev tun0 proto static metric 50
default via 10.0.2.2 dev eth0 proto static metric 100
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100
10.8.0.1 via 10.8.0.5 dev tun0 proto static metric 50
10.8.0.5 dev tun0 proto kernel scope link src 10.8.0.6 metric 50
10.132.0.0/16 via 10.8.0.5 dev tun0 proto static metric 50
104.236.239.153 via 10.0.2.2 dev eth0 proto static metric 100
169.254.0.0/16 dev eth0 scope link metric 1000
Run Code Online (Sandbox Code Playgroud)
小智 16
将“仅使用资源在其网络这种连接”复选框,在纳米连接编辑器控制NetworkManager的是否应该通过VPN添加缺省路由。如果像您一样选中它,则只有定向到 VPN 子网的数据包将通过 VPN 网关,系统将使用现有的默认路由到达其他目的地。
您可以使用 nmcli 从命令行更改相同的设置:
nmcli connection modify <VPN connection> ipv4.never-default yes
Run Code Online (Sandbox Code Playgroud)
我设法通过使用客户端 GUI (Ubuntu NetworkManager) 来获得所需的效果。我必须确保选中IPv4 Settings -> Routes“仅将此连接用于其网络上的资源”下的复选框:
我不完全确定我需要在 .ovpn 文件中做什么才能复制它。
我的路由表现在看起来像这样:
$ sudo netstat -r -n
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 eth0
10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.8.0.1 10.8.0.5 255.255.255.255 UGH 0 0 0 tun0
10.8.0.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun0
10.132.0.0 10.8.0.5 255.255.0.0 UG 0 0 0 tun0
104.236.239.153 10.0.2.2 255.255.255.255 UGH 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
Run Code Online (Sandbox Code Playgroud)
请记住,我push "route 10.132.0.0 255.255.0.0"在我的server.conf以便解释条目10.132.0.0以及为什么我现在可以访问我的服务器,而其他所有内容都路由到 VPN 之外(即0.0.0.0条目)
如果没有在 GUI 中检查此设置,我的路由表如下所示:
$ sudo netstat -r -n
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.8.0.5 0.0.0.0 UG 0 0 0 tun0
0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 eth0
10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.8.0.1 10.8.0.5 255.255.255.255 UGH 0 0 0 tun0
10.8.0.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun0
10.132.0.0 10.8.0.5 255.255.0.0 UG 0 0 0 tun0
104.236.239.153 10.0.2.2 255.255.255.255 UGH 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
Run Code Online (Sandbox Code Playgroud)
我的猜测是第一个0.0.0.0条目(默认路由)把一切都搞砸了。
| 归档时间: |
|
| 查看次数: |
9765 次 |
| 最近记录: |