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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章