按照所述步骤这里我已经成功配置了 VPN 服务器和客户端(我可以双向 ping 通)。它们各自的配置文件是:
服务器:
;local a.b.c.d
port 1194
;proto tcp
proto udp
push "redirect-gateway def1"
;dev tap
dev tun
ca ca.crt
cert certificate_server.crt
key certificate_server.key
dh dh1024.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
;server-bridge
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
;learn-address ./script
;push "redirect-gateway def1 bypass-dhcp"
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"
;client-to-client
;duplicate-cn
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
;log openvpn.log
;log-append openvpn.log
客户:
client
;dev tap
dev tun
;dev-node MyTap
;proto tcp
proto udp
remote <external_server_ip> 1194
;remote my-server-2 1194
push "dhcp-option DNS 10.8.0.1"
;remote-random
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert certificate_client.crt
key certificate_client.key
ns-cert-type server
;tls-auth ta.key 1
comp-lzo
网络布局如下:我正在使用客户端连接到位于 NAT 后面的服务器。我已转发 NAT 设备上的端口 1194,并成功 ping 通服务器。现在我想将所有流量从客户端的 eth0 接口路由到 tun0 接口。在客户端上运行 ifconfig 可获得:
eth0 Link encap:Ethernet HWaddr 01:02:03:04:05:06
inet addr:172.26.0.206 Bcast:172.26.255.255 Mask:255.255.0.0
inet6 addr: fe80::3285:a9ff:fe0b:fee8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15579878 errors:0 dropped:2 overruns:0 frame:0
TX packets:3774742 errors:0 dropped:0 overruns:0 carrier:4
collisions:0 txqueuelen:1000
RX bytes:7365014496 (7.3 GB) TX bytes:349016660 (349.0 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:132571 errors:0 dropped:0 overruns:0 frame:0
TX packets:132571 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:6718530 (6.7 MB) TX bytes:6718530 (6.7 MB)
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:10.8.0.6 P-t-P:10.8.0.5 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:1296 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 B) TX bytes:102968 (102.9 KB)
wlan0 Link encap:Ethernet HWaddr 01:01:01:01:01:01
inet addr:192.168.0.103 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::de85:deff:fe32:241f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:284664 errors:0 dropped:0 overruns:0 frame:0
TX packets:99157 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:177617284 (177.6 MB) TX bytes:32064393 (32.0 MB)
在服务器上运行命令时输出:
eth0 Link encap:Ethernet HWaddr 06:05:04:03:02:01
inet addr:192.168.2.7 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::219:d1ff:fefe:de8a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:44596 errors:0 dropped:0 overruns:0 frame:0
TX packets:22418 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9082333 (9.0 MB) TX bytes:5007949 (5.0 MB)
Interrupt:20 Memory:e3200000-e3220000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:394157 errors:0 dropped:0 overruns:0 frame:0
TX packets:394157 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:19763027 (19.7 MB) TX bytes:19763027 (19.7 MB)
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:10.8.0.1 P-t-P:10.8.0.2 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:7615 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:463861 (463.8 KB) TX bytes:588 (588.0 B)
在我的客户端上,IPv4 数据包转发已启用;客户端计算机上的路由表是: 客户:
$ netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.8.0.5 128.0.0.0 UG 0 0 0 tun0
0.0.0.0 172.26.0.1 0.0.0.0 UG 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
<server_external_ip> 172.26.0.1 255.255.255.255 UGH 0 0 0 eth0
128.0.0.0 10.8.0.5 128.0.0.0 UG 0 0 0 tun0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
172.26.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
在服务器上:
服务器:
0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 eth0
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
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
我如何在客户端计算机上将所有流量从 eth0 路由到 tun0?我尝试在路由表中添加一个条目:
sudo route add default gw 10.8.0.1
输出为:
SIOCADDRT: No such process
此外,在此步骤之后,连接停止工作,我无法再从客户端 ping 服务器;eth0 上的流量无法再转发。
答案1
您的 OpenVPN 实例正在以点对点模式运行,因此您的默认网关不是 10.8.0.1。
查看您的客户端路由表,似乎 OpenVPN 客户端已正确设置路由,因此 VPN 服务器现在是您的默认网关(这是redirect-gateway def1
在服务器配置中指示的):
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.8.0.5 128.0.0.0 UG 0 0 0 tun0
128.0.0.0 10.8.0.5 128.0.0.0 UG 0 0 0 tun0
所以你基本上已经实现了你想要的 - 让所有原来通过 eth0 的流量现在都转到 tun0。
如果你对为什么拨通VPN后无法上网(无法访问其他网站)有疑惑,可以按照@Bill 的建议:在服务器上设置NAT和IP转发。
这也很简单:
# sysctl -w net.ipv4.ip_forward=1
# iptables -t nat -A POSTROUTING ! -o lo -j MASQUERADE
如果您比较偏执,请根据@Bill 的回答更改第二行。
答案2
将流量从本地子网重新路由到 tun0 接口:
iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o tun0 -j MASQUERADE
答案3
你可以尝试这个:
route add -net 172.26.0.0 netmask 255.255.0.0 gw 10.0.8.5