OpenVPN路由设置

OpenVPN采用默认设置连接时所有的数据都从vpn通道走,造成所有访问都非常慢,但是openvpn支持路由设置。

主要由以下三个参数决定
1. route-nopull
当客户端加入这个参数后,openvpn 连接后不会添加路由,也就是不会有任何网络请求走openvpn.

2.vpn_gateway
当客户端加入 route-nopull 后,所有出去的访问都不从 Openvpn 出去,但可通过添加 vpn_gateway参数使部分IP访问走 Openvpn 出去

route 192.168.1.0 255.255.0.0 vpn_gateway
route 172.121.0.0 255.255.0.0 vpn_gateway
3.net_gateway
 这个参数和 vpn_gateway 相反,表示在默认出去的访问全部走 Openvpn 时,强行指定部分IP访问不通过 Openvpn 出去.
max-routes  参数表示可以添加路由的条数,默认只允许添加100条路由,如果少于100条路由可不加这个参数.
max-routes 1000
route 172.121.0.0 255.255.0.0 net_gateway

比较常用做法是在客户端配置文件中加上 route-nopull 再使用 vpn-gateway 逐条添加需要走Openvpn的ip。

注:若配置中有redirect-gateway deg1则需要先删除

OpenVPN docker地址:https://hub.docker.com/r/kylemanna/openvpn/

github地址为:https://github.com/kylemanna/docker-openvpn/issues/341


使用步骤

  • Pick a name for the $OVPN_DATA data volume container. It's recommended to
    use the ovpn-data- prefix to operate seamlessly with the reference systemd
    service. Users are encourage to replace example with a descriptive name of
    their choosing.

      OVPN_DATA="ovpn-data-example"
    
  • Initialize the $OVPN_DATA container that will hold the configuration files
    and certificates. The container will prompt for a passphrase to protect the
    private key used by the newly generated certificate authority.

      docker volume create --name $OVPN_DATA
      docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM
      docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki
    
  • Start OpenVPN server process

      docker run -v $OVPN_DATA:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn
    
  • Generate a client certificate without a passphrase

      docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full CLIENTNAME nopass
    
  • Retrieve the client configuration with embedded certificates

      docker run -v $OVPN_DATA:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient CLIE

3、通过默认配置获取的IP是动态的,下面介绍如何配置静态ip

(1)、生成一个新的客户端证书,Common Name 要求不一样,并且记住这个Common Name,后面要用到


(2)、修改服务器的配置文件
    在 C:/Program Files (x86)/OpenVPN/config/ server.conf中增加如下
    client-config-dir  C:/OpenVPN/client-ip-config/ccd
    然后在 C:/OpenVPN/client-ip-config/ccd 目录中放针对每个客户端的个性化配置文件。
    文件名就用客户端名 生成key的时候输入的 "Common Name" 名字
    要设置客户端 testClient为 10.8.10.254 
    只要在 C:/OpenVPN/client-ip-config/ccd/testClient 文件中包含一行:
    ifconfig-push 192.168.2.24  192.168.2.25
#ifconfig-push中的每一对IP地址表示虚拟客户端和服务器的IP端点 #它们必须从连续的/30子网网段中获取(这里是/30表示xxx.xxx.xxx.xxx/30,即子网掩码位数为30) #以便于与Windows客户端和TAP-Windows驱动兼容。明确地说,每个端点的IP地址对的最后8位字节必须取自下面的集合: [ 1, 2 ] [ 5 , 6 ] [ 9 , 10 ] [ 13 , 14 ] [ 17 , 18 ][ 21, 22 ] [ 25 , 26 ] [ 29 , 30 ] [ 33 , 34 ] [ 37 , 38 ][ 41, 42 ] [ 45 , 46 ] [ 49 , 50 ] [ 53 , 54 ] [ 57 , 58 ][ 61, 62 ] [ 65 , 66 ] [ 69 , 70 ] [ 73 , 74 ] [ 77 , 78 ][ 81, 82 ] [ 85 , 86 ] [ 89 , 90 ] [ 93 , 94 ] [ 97 , 98 ][ 101,102 ] [ 105 , 106 ] [ 109 , 110 ] [ 113 , 114 ] [ 117 , 118 ][ 121,122 ] [ 125 , 126 ] [ 129 , 130 ] [ 133 , 134 ] [ 137 , 138 ][ 141,142 ] [ 145 , 146 ] [ 149 , 150 ] [ 153 , 154 ] [ 157 , 158 ][ 161,162 ] [ 165 , 166 ] [ 169 , 170 ] [ 173 , 174 ] [ 177 , 178 ][ 181,182 ] [ 185 , 186 ] [ 189 , 190 ] [ 193 , 194 ] [ 197 , 198 ][ 201,202 ] [ 205 , 206 ] [ 209 , 210 ] [ 213 , 214 ] [ 217 , 218 ][ 221,222 ] [ 225 , 226 ] [ 229 , 230 ] [ 233 , 234 ] [ 237 , 238 ][ 241,242 ] [ 245 , 246 ] [ 249 , 250 ] [ 253 , 254 ]

参考-非常详细介绍:https://my.oschina.net/stache/blog/1512610

猜你喜欢

转载自blog.csdn.net/caizhengwu/article/details/80066338