openvpn linux客户端路由表设置
开启内核转发功能
[root@localhost ~]#echo 1 > /proc/sys/net/ipv4/ip_forward
首先给本地路由表里添加设置
[root@localhost ~]# route add -net 10.8.0.0/24 tun0
[root@localhost ~]# route add -net 192.168.100.0/24 eth0
[root@localhost ~]# route add -net 10.8.0.0/24 gw 10.8.0.1
出现的问题是内网和vpn的网络是可以互通的,但是不能上inter网,
首先是,没有开启vpn时 的路由表
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
174.136.43.78 192.168.100.1 255.255.255.255 UGH 0 0 0 eth0
192.168.100.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.100.1 0.0.0.0 UG 0 0 0 eth0
启动vpn
[root@localhost ~]# openvpn --config /etc/openvpn/vpnclient.conf
[root@localhost ~]# route
Destination Gateway Genmask Flags Metric Ref Use Iface
10.8.0.5 * 255.255.255.255 UH 0 0 0 tun0
174.136.43.78 192.168.100.1 255.255.255.255 UGH 0 0 0 eth0
192.168.100.0 10.8.0.5 255.255.255.0 UG 0 0 0 tun0
192.168.100.0 * 255.255.255.0 U 0 0 0 eth0
10.8.0.0 10.8.0.5 255.255.255.0 UG 0 0 0 tun0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 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
default 192.168.100.1 0.0.0.0 UG 0 0 0 eth0
上面蓝色字体哪条明显是有错误了,删掉
[root@localhost ~]# route del -net 192.168.100.0/24 gw 10.8.0.5
红色的哪条是vpn服务器每次启动默认添加的,需要从服务器上去掉这条记录
先从客户端这里临时删除
[root@localhost ~]# route del default netmask 128.0.0.0
现在看看路由表的情况
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.8.0.5 * 255.255.255.255 UH 0 0 0 tun0
174.136.43.78 192.168.100.1 255.255.255.255 UGH 0 0 0 eth0
192.168.100.0 * 255.255.255.0 U 0 0 0 eth0
10.8.0.0 10.8.0.5 255.255.255.0 UG 0 0 0 tun0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
128.0.0.0 10.8.0.5 128.0.0.0 UG 0 0 0 tun0
default 192.168.100.1 0.0.0.0 UG 0 0 0 eth0
看一下外网能否连接
[root@localhost ~]# traceroute
traceroute to (66.249.89.99), 30 hops max, 40 byte packets
1 192.168.100.1 (192.168.100.1) 0.635 ms 0.824 ms 1.005 ms
2 172.16.70.254 (172.16.70.254) 4.642 ms 4.565 ms 4.484 ms
3 222.90.193.1 (222.90.193.1) 7.605 ms 8.048 ms 8.950 ms
4 117.36.124.1 (117.36.124.1) 4.301 ms 5.250 ms 5.714 ms
5 222.91.155.1 (222.91.155.1) 4.690 ms 5.130 ms 5.597 ms
6 61.134.0.121 (61.134.0.121) 5.844 ms 2.489 ms 1.855 ms
7 (202.97.36.165) 50.088 ms 59.819 ms 48.866 ms
8 (202.97.36.161) 150.520 ms 150.676 ms 152.912 ms
9 (202.97.33.50) 51.427 ms 51.332 ms 51.749 ms
10 (202.97.34.126) 153.171 ms 153.563 ms 153.465 ms
11 * * *
12 * * *
13 209.85.249.195 (209.85.249.195) 86.633 ms 85.946 ms 86.052 ms
14 209.85.249.195 (209.85.249.195) 188.731 ms 187.873 ms 187.978 ms
15 (66.249.89.99) 86.672 ms 86.768 ms 97.648 ms
ok,好了可以用了
测一下vpn网络能否互通
[root@localhost ~]# ping 10.8.0.1
PING 10.8.0.1 (10.8.0.1) 56(84) bytes of data.
64 bytes from 10.8.0.1: icmp_seq=1 ttl=64 time=466 ms
64 bytes from 10.8.0.1: icmp_seq=2 ttl=64 time=470 ms
64 bytes from 10.8.0.1: icmp_seq=3 ttl=64 time=465 ms
ok ,可以使用。
注:如果有防火墙 还需要单独设置。