1. 程式人生 > >OpenVPN 安裝、配置客戶端和服務端,以及OpenVPN的使用 (Windows 平臺)

OpenVPN 安裝、配置客戶端和服務端,以及OpenVPN的使用 (Windows 平臺)

OpenVPN 開源,好用,而且免費,感謝 OpenVPN 團隊開發此產品。

簡介

OpenVPN允許參與建立VPN的單點使用公開金鑰電子證書、或者使用者名稱/密碼來進行身份驗證。它大量使用了OpenSSL加密庫中的SSLv3/TLSv1協議函式庫。目前OpenVPN能在SolarisLinuxOpenBSDFreeBSDNetBSDMac OS XWindows 2000XPVista/Windows 7以及Android上執行,幷包含了許多安全性的功能。它並不是一個基於Web的VPN軟體,也不與IPsec及其他VPN軟體包相容。

加密

OpenVPN使用OpenSSL庫加密資料與控制資訊:它使用了OpenSSL的加密以及驗證功能,意味著,它能夠使用任何OpenSSL支援的演算法。它提供了可選的資料包

HMAC功能以提高連線的安全性。此外,OpenSSL的硬體加速也能提高它的效能。

驗證

OpenVPN提供了多種身份驗證方式,用以確認參與連線雙方的身份,包括:預享私鑰,第三方證書以及使用者名稱/密碼組合。預享金鑰最為簡單,但同時它只能用於建立點對點的VPN;基於PKI的第三方證書提供了最完善的功能,但是需要額外的精力去維護一個PKI證書體系。OpenVPN2.0後引入了使用者名稱/口令組合的身份驗證方式,它可以省略客戶端證書,但是仍有一份伺服器證書需要被用作加密。

網路

OpenVPN所有的通訊都基於一個單一的IP,預設且推薦使用UDP協議通訊,同時TCP也被支援。OpenVPN連線能通過大多數的

代理伺服器,並且能夠在NAT的環境中很好地工作。服務端具有向客戶端“推送”某些網路配置資訊的功能,這些資訊包括:IP地址路由設定等。OpenVPN提供了兩種虛擬網路介面:通用Tun/Tap驅動,通過它們,可以建立三層IP隧道,或者虛擬二層乙太網,後者可以傳送任何型別的二層乙太網絡資料。傳送的資料可通過LZO演算法壓縮。IANA(Internet Assigned Numbers Authority)指定給OpenVPN的官方埠為1194。OpenVPN 2.0以後版本每個程序可以同時管理數個併發的隧道。

OpenVPN使用通用網路協議(TCPUDP)的特點使它成為IPsec等協議的理想替代,尤其是在

ISP(Internet service provider)過濾某些特定VPN協議的情況下。

安全

OpenVPN與生俱來便具備了許多安全特性:它在使用者空間執行,無須對核心及網路協議棧作修改;初始完畢後以chroot方式執行,放棄root許可權;使用mlockall以防止敏感資料交換到磁碟。

OpenVPN通過PKCS#11支援硬體加密標識,如智慧卡

申明:此文與免流無關,以免流為目的讀者請離開。

 OpenVPN 使用比較簡單,流程上稍微注意一下,你一定可以很好的操作它,本文僅以證書及 key 作為驗證方式對 OpenVPN 的使用進行介紹,有問題Q群裡交流(群號在文章中)。

連線模式大致如圖所示

  OpenVPN 可以為1對1或者1對多,甚至多對多的分散計算機構築安全的網際網路絡,簡單的說,就是通過物理網絡卡的連線,建立虛擬網路,藉由虛擬網絡卡訪問虛擬網路,形成一個跨越廣域網的虛擬區域網。

  相互間的通訊經嚴格加密,安全可靠,比如遠端管理伺服器,但又當心3389或者FTP之類的埠被那些草根黑客用字典猜密碼,複雜的密碼雖然不大可能猜中,但也實在很煩,但真正的黑客我還是很敬仰他們的,那麼言歸正傳。管理這類伺服器通過 OpenVPN 的證書認證連線成功後,你和伺服器之間如同區域網,對內網再開放埠會安全的多,維護也較容易,防火牆對外網,則只針對例如80 埠和OpenVPN 埠(可以自定,預設是1194)開放,否則就無法訪問了。

(如果以下下載不能,請到文中Q群共享檔案下載)

OpenVPN 2.4.2 -- released on 2017.05.11

OpenVPN 2.4.0 -- released on 2016.12.27

OpenVPN 2.3.16 (old stable) -- released on 2017.05.19

OpenVPN 2.3.13-- released on 2016.11.03

OpenVPN src & bin released on 2016.8.23

OpenVPN src & bin released on 2016.05.10

OpenVPN 2.3.10 src & bin, released on 2016.01.04

OpenVPN 2.3.8 src & bin, released on 2015.08.04

OpenVPN 2.3.6 bin & src 下載

OpenVPN 2.3.5 I001 32/64 bin & src 下載

OpenVPN 2.3.4 I003/I603 下載地址

OpenVPN 2.3.4 I002 下載地址(已修復心臟出血漏洞)

OpenVPN 2.3.3  下載地址

OpenVPN 2.3.2 下載地址

OpenVPN 2.3 下載地址

OpenVPN 2.2 下載地址

其它歷史釋出版本

OpenVPN 的配置不復雜,以 Windows 環境下為例,選擇全部元件安裝,安裝完畢後 cmd 控制檯進入C:\Program Files\OpenVPN\easy-rsa 目錄,我們需要建立服務端和客戶端證書。

進入cmd前可能需要將C:\Program Files\OpenVPN\bin新增到環境變數的path中。

OpenVPN 經測試,能運行於32位和64位作業系統,不過若在Windows 7下安裝的話,必須以管理員許可權執行。舊版本 OpenVPN 很可能在 Windows 8 執行不正常,其他舊版 Windows 32/64 平臺均沒問題,最新版已經支援 Windows 10。

製作證書,執行控制檯命令

#初始化配置

init-config

#複製配置檔案為批處理
vars

#清除key資料夾內的檔案,資料夾不存在會自動建立
clean-all

#生成證書及key,需要填寫一些引數,建議不要預設
build-ca

#建立服務端證書及key,需要填寫一些引數,建議不要預設
build-key-server server

#建立一個客戶端證書及key,需要填寫一些引數,建議不要預設
build-key client1

#再建立一個客戶端證書及key,需要填寫一些引數,建議不要預設
build-key client2

#再建立一個客戶端證書及key,需要填寫一些引數,建議不要預設
build-key client3

#生成1024bit的key,並結束此次建立
build-dh

證書製作完畢,得到一些檔案,但如果引數填寫不恰當,可能生成一些0位元組的crt檔案,那麼就得重新生成證書。生成結束後,可按照如下表格分別將檔案拷貝到客戶端和服務端的 config 目錄中(測試時全部放一起也沒關係)。

      檔名       

位置

目的

  保密

ca.crt

server + all clients

Root CA certificate

NO

ca.key

 key signing machine only

Root CA key

YES

dh{n}.pem

server only

 Diffie Hellman parameters

NO

server.crt

server only

Server Certificate

NO

server.key

server only

Server Key

YES

client1.crt

client1 only

Client1 Certificate

NO

client1.key

client1 only

Client1 Key

YES

client2.crt

client2 only

Client2 Certificate

NO

client2.key

client2 only

Client2 Key

YES

client3.crt

client3 only

Client3 Certificate

NO

client3.key

client3 only

Client3 Key

YES

若要增加證書,如果不是第一次建立使用者,只需執行

vars

build-key mark

若要撤銷證書,則使用以下命令,並得到 crl.pem 檔案,

vars
revoke-full client2

再在服務端配置中加上

crl-verify crl.pem

下一步是配置檔案,在 C:\Program Files\OpenVPN\sample-config 目錄內有client.ovpnserver.ovpn,將client.ovpn拷貝到客戶端的config,server.ovpn拷貝到服務端的 C:\Program Files\OpenVPN\config 目錄。

配置檔案內需要指定證書檔名(預設已存在),請務必確認配置檔案內證書檔名描述正確!

如果僅僅使用證書配置,除證書檔名描述正確外,只需要修改客戶端配置檔案指向的服務端IP即可。

也就是說,simple-config 內的配置幾乎不需要修改,在證書正常的情況下,就可以直接使用!

修改客戶端的 client.ovpnremote 項,讓它指向服務端外部 IP 地址,格式例如:remote x.x.x.x 1194,1194是服務端定義的埠,這個可以通過服務端的配置檔案修改,服務端和客戶端埠必須匹配。

證書:在第92,93行的(大概)位置,修改指定的 crt 和 key 檔案,與之生成的證書檔名匹配。

修改服務端的 server.ovpn,其實服務端配置不修改也已經可以使用了,如果想指定內部地址10.254.254.1可自行調整;預設的crtkey就是server,若名字不同,則需要修改一下。

確保客戶端 DHCP Client 服務開啟,客戶端需要通過這個服務接收服務端分配的 IP。

開啟作業系統自帶的 DHCP Client 服務(管理工具的服務,但通常是自動),用於獲取被分配的IP地址。

部署完畢後,服務端執行 OpenVPN GUI,右鍵選擇 server 再選擇connect,啟用服務端連線。注意看日誌,或者 控制檯執行openvpn --config server 也可以,日誌將詳細顯示連線情況,若有問題很容易排查,全部除錯通過後建議以自動啟動的服務的方式執行(服務內已經被安裝了OpenVPN的一項,預設手動),這樣不登入桌面也可以使用VPN。

客戶端執行 OpenVPN GUI,右鍵選擇 client1 在選擇connect,嘗試連線服務端。注意看日誌,便於排除問題,這次連線成功後,下次可以使用系統服務來使用OpenVPN,不需要OpenVPN GUI,這樣使用更方便。

至此 OpenVPN 連線完成。

至於看不明白是為什麼的,照葫蘆畫瓢走一兩道,應該就明白了。

有一點注意,若需要更新證書,自己又處於遠端位置,則在服務端控制檯將證書全部生成後,先把客戶端證書下載好(但不部署),接著部署服務端證書,然後重新啟動 OpenVPN 的服務端的服務(這時VPN中斷),再將下載好的客戶端證書部署好,並重新連線,即完成全部證書更新,不過整個過程需特別謹慎,不能有差錯,否則可能被關在伺服器外面,或者先把3389開啟,再更新證書,但風險會有提高。

官方推薦使用UDP協議(UDP要比TCP節省資源),不過據我個人測試下來,TCP連線在意外中斷後自動重連線速度更快一些,但UDP協議相容性更高;客戶端和服務端的配置檔案還有一些其他屬性,各位可以參考備註或者下面留的連結。

通過配置檔案的 client to client 開關,還能將網際網路上的多臺終端集合到一個虛擬的區域網中,實現相互訪問。

主要參考資料 來自 https://openvpn.net 不過無法通過正常渠道開啟,大家可以使用國外免費代'|'理進行訪問,或藉助一些工具,將更簡單,進群尋找答案。

另推薦一本書《OpenVPN 2 Cookbook》,有100例使用配置,通常足夠使用了(群共享有下載)。

若需要 Android 客戶端,可進入

下載,Android 4.0 以上支援。

適當調整服務端配置,並且在加上一個NAT服務,OpenVPN 就是一個翻牆利器……

注:此文與免流無關,以免流為目的加群者莫自取其辱。免流實為作惡,或盜竊,或銷贓,奉勸莫用免流。

有問題Q群討論:236201801
群內共享提供 OpenVPN 下載。



Introduction

OpenVPN is a full-featured SSL VPN which implements OSI layer 2 or 3 secure network extension using the industry standard SSL/TLS protocol, supports flexible client authentication methods based on certificates, smart cards, and/or username/password credentials, and allows user or group-specific access control policies using firewall rules applied to the VPN virtual interface. OpenVPN is not a web application proxy and does not operate through a web browser.

OpenVPN 2.0 expands on the capabilities of OpenVPN 1.x by offering a scalable client/server mode, allowing multiple clients to connect to a single OpenVPN server process over a single TCP or UDP port. OpenVPN 2.3 includesa large number of improvements, including full IPv6 support and PolarSSL support.

This document provides step-by-step instructions for configuring an OpenVPN 2.x client/server VPN, including:

The impatient may wish to jump straight to the sample configuration files:

Intended Audience

This HOWTO assumes that readers possess a prior understanding of basic networking concepts such as IP addresses, DNS names, netmasks, subnets, IP routing, routers, network interfaces, LANs, gateways, and firewall rules.

Additional Documentation

OpenVPN Books

OpenVPN 1.x HOWTO

The original OpenVPN 1.x HOWTO is still available, and remains relevant for point-to-point or static-key configurations.

OpenVPN Articles

For additional documentation, see the articles page and the OpenVPN wiki.

OpenVPN Quickstart

While this HOWTO will guide you in setting up a scalable client/server VPN using an X509 PKI (public key infrastruction using certificates and private keys), this might be overkill if you are only looking for a simple VPN setup with a server that can handle a single client.

If you would like to get a VPN running quickly with minimal configuration, you might check out theStatic Key Mini-HOWTO.

Static Key advantages

  • Simple Setup
  • No X509 PKI (Public Key Infrastructure) to maintain

Static Key disadvantages

  • Limited scalability -- one client, one server
  • Lack of perfect forward secrecy -- key compromise results in total disclosure of previous sessions
  • Secret key must exist in plaintext form on each VPN peer
  • Secret key must be exchanged using a pre-existing secure channel

Installing OpenVPN

OpenVPN source code and Windows installers can be downloaded here. Recent releases (2.2 and later) are also available as Debian and RPM packages; see theOpenVPN wiki for details.

For security, it's a good idea to check the file release signature after downloading.

The OpenVPN executable should be installed on both server and client machines, since the single executable provides both client and server functions.

Linux Notes (using RPM package)

If you are using a Linux distribution which supports RPM packages (SuSE, Fedora, Redhat, etc.), it's best to install using this mechanism. The easiest method is to find an existing binary RPM file for your distribution. You can also build your own binary RPM file:

rpmbuild -tb openvpn-[version].tar.gz

Once you have the .rpm file, you can install it with the usual

rpm -ivh openvpn-[details].rpm

or upgrade an existing installation with

rpm -Uvh openvpn-[details].rpm

Installing OpenVPN from a binary RPM package has these dependencies:

  • openssl
  • lzo
  • pam

Furthermore, if you are building your own binary RPM package, there are several additional dependencies:

  • openssl-devel
  • lzo-devel
  • pam-devel

See the openvpn.spec file for additional notes on building an RPM package for Red Hat Linux 9 or building with reduced dependencies.

Linux Notes (without RPM)

If you are using Debian, Gentoo, or a non-RPM-based Linux distribution, use your distro-specific packaging mechanism such asapt-get on Debian oremerge on Gentoo.

It is also possible to install OpenVPN on Linux using the universal ./configure method. First expand the .tar.gz file:

tar xfz openvpn-[version].tar.gz

Then cd to the top-level directory and type:

./configure
make
make install

Windows Notes

OpenVPN for Windows can be installed from the self-installing exe file on the OpenVPN download page. Remember that OpenVPN will only run on Windows XP or later. Also note that OpenVPN must be installed and run by a user who has administrative privileges (this restriction is imposed by Windows, not OpenVPN). The restriction can be sidestepped by running OpenVPN in the background as a service, in which case even non-admin users will be able to access the VPN, once it is installed.More discussion on OpenVPN + Windows privilege issues.

Official OpenVPN Windows installers include OpenVPN-GUI, which allows managing OpenVPN connections from a system tray applet. OtherGUI applications are also available.

After you've run the Windows installer, OpenVPN is ready for use and will associate itself with files having the.ovpn extension. To run OpenVPN, you can:

  • Right click on an OpenVPN configuration file (.ovpn) and select Start OpenVPN on this configuration file. Once running, you can use theF4key to exit.
  • Run OpenVPN from a command prompt Window with a command such as:
    openvpn myconfig.ovpn

    Once running in a command prompt window, OpenVPN can be stopped by the F4 key.

  • Run OpenVPN as a service by putting one or more .ovpn configuration files in \Program Files\OpenVPN\config and starting the OpenVPN Service, which can be controlled from Start Menu -> Control Panel -> Administrative Tools -> Services.

Mac OS X Notes

Angelo Laub and Dirk Theisen have developed an OpenVPN GUI for OS X.

Other OSes

Some notes are available in the INSTALL file for specific OSes. In general, the

./configure
make
make install

method can be used, or you can search for an OpenVPN port or package which is specific to your OS/distribution.

Determining whether to use a routed or bridged VPN

See FAQ for an overview of Routing vs. Ethernet Bridging. See also the OpenVPN Ethernet Bridging page for more notes and details on bridging.

Overall, routing is probably a better choice for most people, as it is more efficient and easier to set up (as far as the OpenVPN configuration itself) than bridging. Routing also provides a greater ability to selectively control access rights on a client-specific basis.

I would recommend using routing unless you need a specific feature which requires bridging, such as:

  • the VPN needs to be able to handle non-IP protocols such as IPX,
  • you are running applications over the VPN which rely on network broadcasts (such as LAN games), or
  • you would like to allow browsing of Windows file shares across the VPN without setting up a Samba or WINS server.

Numbering private subnets

Setting up a VPN often entails linking together private subnets from different locations.

The Internet Assigned Numbers Authority (IANA) has reserved the following three blocks of the IP address space for private internets (codified in RFC 1918):

10.0.0.0 10.255.255.255 (10/8 prefix)
172.16.0.0 172.31.255.255 (172.16/12 prefix)
192.168.0.0 192.168.255.255 (192.168/16 prefix)

While addresses from these netblocks should normally be used in VPN configurations, it's important to select addresses that minimize the probability of IP address or subnet conflicts. The types of conflicts that need to be avoided are:

  • conflicts from different sites on the VPN using the same LAN subnet numbering, or
  • remote access connections from sites which are using private subnets which conflict with your VPN subnets.

For example, suppose you use the popular 192.168.0.0/24 subnet as your private LAN subnet. Now you are trying to connect to the VPN from an internet cafe which is using the same subnet for its WiFi LAN. You will have a routing conflict because your machine won't know if 192.168.0.1 refers to the local WiFi gateway or to the same address on the VPN.

As another example, suppose you want to link together multiple sites by VPN, but each site is using 192.168.0.0/24 as its LAN subnet. This won't work without adding a complexifying layer of NAT translation, because the VPN won't know how to route packets between multiple sites if those sites don't use a subnet which uniquely identifies them.

The best solution is to avoid using 10.0.0.0/24 or 192.168.0.0/24 as private LAN network addresses. Instead, use something that has a lower probability of being used in a WiFi cafe, airport, or hotel where you might expect to connect from remotely. The best candidates are subnets in the middle of the vast 10.0.0.0/8 netblock (for example 10.66.77.0/24).

And to avoid cross-site IP numbering conflicts, always use unique numbering for your LAN subnets.

Setting up your own Certificate Authority (CA) and generating certificates and keys for an OpenVPN server and multiple clients

Overview

The first step in building an OpenVPN 2.x configuration is to establish a PKI (public key infrastructure). The PKI consists of:

  • a separate certificate (also known as a public key) and private key for the server and each client, and
  • a master Certificate Authority (CA) certificate and key which is used to sign each of the server and client certificates.

OpenVPN supports bidirectional authentication based on certificates, meaning that the client must authenticate the server certificate and the server must authenticate the client certificate before mutual trust is established.

Both server and client will authenticate the other by first verifying that the presented certificate was signed by the master certificate authority (CA), and then by testing information in the now-authenticated certificate header, such as the certificate common name or certificate type (client or server).

This security model has a number of desirable features from the VPN perspective:

  • The server only needs its own certificate/key -- it doesn't need to know the individual certificates of every client which might possibly connect to it.
  • The server will only accept clients whose certificates were signed by the master CA certificate (which we will generate below). And because the server can perform this signature verification without needing access to the CA private key itself, it is possible for the CA key (the most sensitive key in the entire PKI) to reside on a completely different machine, even one without a network connection.
  • If a private key is compromised, it can be disabled by adding its certificate to a CRL (certificate revocation list). The CRL allows compromised certificates to be selectively rejected without requiring that the entire PKI be rebuilt.
  • The server can enforce client-specific access rights based on embedded certificate fields, such as the Common Name.

Note that the server and client clocks need to be roughly in sync or certificates might not work properly.

Generate the master Certificate Authority (CA) certificate & key

In this section we will generate a master CA certificate/key, a server certificate/key, and certificates/keys for 3 separate clients.

For PKI management, we will use easy-rsa, a set of scripts which is bundled with OpenVPN 2.2.x and earlier. If you're using OpenVPN 2.3.x, you need to download easy-rsa separately fromhere.

If you are using Linux, BSD, or a unix-like OS, open a shell and cd to the easy-rsa subdirectory. If you installed OpenVPN from an RPM or DEB file, the easy-rsa directory can usually be found in/usr/share/doc/packages/openvpn or/usr/share/doc/openvpn(it's best to copy this directory to another location such as/etc/openvpn, before any edits, so that future OpenVPN package upgrades won't overwrite your modifications). If you installed from a .tar.gz file, the easy-rsa directory will be in the top level directory of the expanded source tree.

If you are using Windows, open up a Command Prompt window and cd to \Program Files\OpenVPN\easy-rsa. Run the following batch file to copy configuration files into place (this will overwrite any preexisting vars.bat and openssl.cnf files):

init-config

Now edit the vars file (called vars.bat on Windows) and set the KEY_COUNTRY, KEY_PROVINCE, KEY_CITY, KEY_ORG, and KEY_EMAIL parameters. Don't leave any of these parameters blank.

Next, initialize the PKI. On Linux/BSD/Unix:

. ./vars
./clean-all
./build-ca

On Windows:

vars
clean-all
build-ca

The final command (build-ca) will build the certificate authority (CA) certificate and key by invoking the interactiveopensslcommand:

ai:easy-rsa # ./build-ca
Generating a 1024 bit RSA private key
............++++++
...........++++++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [KG]:
State or Province Name (full name) [NA]:
Locality Name (eg, city) [BISHKEK]:
Organization Name (eg, company) [OpenVPN-TEST]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:OpenVPN-CA
Email Address [[email protected]]:

Note that in the above sequence, most queried parameters were defaulted to the values set in thevars orvars.bat files. The only parameter which must be explicitly entered is theCommon Name. In the example above, I used "OpenVPN-CA".

Generate certificate & key for server

Next, we will generate a certificate and private key for the server. On Linux/BSD/Unix:

./build-key-server server

On Windows:

build-key-server server

As in the previous step, most parameters can be defaulted. When the Common Name is queried, enter "server". Two other queries require positive responses, "Sign the certificate? [y/n]" and "1 out of 1 certificate requests certified, commit? [y/n]".

Generate certificates & keys for 3 clients

Generating client certificates is very similar to the previous step. On Linux/BSD/Unix:

./build-key client1
./build-key client2
./build-key client3

On Windows:

build-key client1
build-key client2
build-key client3

If you would like to password-protect your client keys, substitute the build-key-pass script.

Remember that for each client, make sure to type the appropriate Common Namewhen prompted, i.e. "client1", "client2", or "client3". Always use a unique common name for each client.

Generate Diffie Hellman parameters

Diffie Hellman parameters must be generated for the OpenVPN server. On Linux/BSD/Unix:

./build-dh

On Windows:

build-dh

Output:

ai:easy-rsa # ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
.................+...........................................
...................+.............+.................+.........
......................................

Key Files

Now we will find our newly-generated keys and certificates in the keyssubdirectory. Here is an explanation of the relevant files:

Filename Needed By Purpose Secret
ca.crt server + all clients Root CA certificate NO
ca.key key signing machine only Root CA key YES
dh{n}.pem server only Diffie Hellman parameters NO
server.crt server only Server Certificate NO
server.key server only Server Key YES
client1.crt client1 only Client1 Certificate NO
client1.key client1 only Client1 Key YES
client2.crt client2 only Client2 Certificate NO
client2.key client2 only Client2 Key YES
client3.crt client3 only Client3 Certificate NO
client3.key client3 only Client3 Key YES

The final step in the key generation process is to copy all files to the machines which need them, taking care to copy secret files over a secure channel.

Now wait, you may say. Shouldn't it be possible to set up the PKI without a pre-existing secure channel?

The answer is ostensibly yes. In the example above, for the sake of brevity, we generated all private keys in the same place. With a bit more effort, we could have done this differently. For example, instead of generating the client certificate and keys on the server, we could have had the client generate its own private key locally, and then submit a Certificate Signing Request (CSR) to the key-signing machine. In turn, the key-signing machine could have processed the CSR and returned a signed certificate to the client. This could have been done without ever requiring that a secret .key file leave the hard drive of the machine on which it was generated.

Creating configuration files for server and clients

Getting the sample config files

It's best to use the OpenVPN sample configuration files as a starting point for your own configuration. These files can also be found in

  • the sample-config-files directory of the OpenVPN source distribution
  • the sample-config-files directory in /usr/share/doc/packages/openvpnor/usr/share/doc/openvpn if you installed from an RPM or DEB package
  • Start Menu -> All Programs -> OpenVPN -> OpenVPN Sample Configuration Files on Windows

Note that on Linux, BSD, or unix-like OSes, the sample configuration files are namedserver.conf andclient.conf. On Windows they are namedserver.ovpnandclient.ovpn.

Editing the server configuration file

The sample server configuration file is an ideal starting point for an OpenVPN server configuration. It will create a VPN using a virtualTUN network interface (for routing), will listen for client connections onUDP port 1194 (OpenVPN's official port number), and distribute virtual addresses to connecting clients from the10.8.0.0/24 subnet.

Before you use the sample configuration file, you should first edit the cacertkey, and dh parameters to point to the files you generated in thePKI section above.

At this point, the server configuration file is usable, however you still might want to customize it further:

  • If you are using Ethernet bridging, you must use server-bridge and dev tapinstead ofserver anddev tun.
  • If you want your OpenVPN server to listen on a TCP port instead of a UDP port, useproto tcp instead ofproto udp (If you want OpenVPN to listen on both a UDP and TCP port, you must run two separate OpenVPN instances).
  • If you want to use a virtual IP address range other than 10.8.0.0/24, you should modify theserver directive. Remember that this virtual IP address range should be a private range which is currently unused on your network.
  • Uncomment out the client-to-client directive if you would like connecting clients to be able to reach each other over the VPN. By default, clients will only be able to reach the server.
  • If you are using Linux, BSD, or a Unix-like OS, you can improve security by uncommenting out theuser nobody andgroup nobody directives.

If you want to run multiple OpenVPN instances on the same machine, each using a different configuration file, it is possible if you:

  • Use a different port number for each instance (the UDP and TCP protocols use different port spaces so you can run one daemon listening on UDP-1194 and another on TCP-1194).
  • If you are using Windows, each OpenVPN configuration taneeds to have its own TAP-Windows adapter. You can add additional adapters by going toStart Menu -> All Programs -> TAP-Windows -> Add a new TAP-Windows virtual ethernet adapter.
  • If you are running multiple OpenVPN instances out of the same directory, make sure to edit directives which create output files so that multiple instances do not overwrite each other's output files. These directives includelog,log-append,status, and ifconfig-pool-persist.

Editing the client configuration files

The sample client configuration file (client.conf on Linux/BSD/Unix orclient.ovpnon Windows) mirrors the default directives set in the sample server configuration file.

  • Like the server configuration file, first edit the cacert, and key parameters to point to the files you generated in thePKI section above. Note that each client should have its owncert/key pair. Only the cafile is universal across the OpenVPN server and all clients.
  • Next, edit the remotedirective to point to the hostname/IP address and port number of the OpenVPN server (if your OpenVPN server will be running on a single-NIC machine behind a firewall/NAT-gateway, use the public IP address of the gateway, and a port number which you have configured the gateway to forward to the OpenVPN server).
  • Finally, ensure that the client configuration file is consistent with the directives used in the server configuration. The major thing to check for is that thedev (tun or tap) andproto (udp or tcp) directives are consistent. Also make sure thatcomp-lzo andfragment, if used, are present in both client and server config files.

Starting up the VPN and testing for initial connectivity

Starting the server

First, make sure the OpenVPN server will be accessible from the internet. That means:

  • opening up UDP port 1194 on the firewall (or whatever TCP/UDP port you've configured), or
  • setting up a port forward rule to forward UDP port 1194 from the firewall/gateway to the machine running the OpenVPN server.

To simplify troubleshooting, it's best to initially start the OpenVPN server from the command line (or right-click on the.ovpn file on Windows), rather than start it as a daemon or service:

openvpn [server config file] 

A normal server startup should look like this (output will vary across platforms):

Sun Feb  6 20:46:38 2005 OpenVPN 2.0_rc12 i686-suse-linux [SSL] [LZO] [EPOLL] built on Feb  5 2005
Sun Feb  6 20:46:38 2005 Diffie-Hellman initialized with 1024 bit key
Sun Feb  6 20:46:38 2005 TLS-Auth MTU parms [ L:1542 D:138 EF:38 EB:0 ET:0 EL:0 ]
Sun Feb  6 20:46:38 2005 TUN/TAP device tun1 opened
Sun Feb  6 20:46:38 2005 /sbin/ifconfig tun1 10.8.0.1 pointopoint 10.8.0.2 mtu 1500
Sun Feb  6 20:46:38 2005 /sbin/route add -net 10.8.0.0 netmask 255.255.255.0 gw 10.8.0.2
Sun Feb  6 20:46:38 2005 Data Channel MTU parms [ L:1542 D:1450 EF:42 EB:23 ET:0 EL:0 AF:3/1 ]
Sun Feb  6 20:46:38 2005 UDPv4 link local (bound): [undef]:1194
Sun Feb  6 20:46:38 2005 UDPv4 link remote: [undef]
Sun Feb  6 20:46:38 2005 MULTI: multi_init called, r=256 v=256
Sun Feb  6 20:46:38 2005 IFCONFIG POOL: base=10.8.0.4 size=62
Sun Feb  6 20:46:38 2005 IFCONFIG POOL LIST
Sun Feb  6 20:46:38 2005 Initialization Sequence Completed

Starting the client

As in the server configuration, it's best to initially start the OpenVPN server from the command line (or on Windows, by right-clicking on theclient.ovpn file), rather than start it as a daemon or service:

openvpn [client config file] 

A normal client startup on Windows will look similar to the server output above, and should end with theInitialization Sequence Completed message.

Now, try a ping across the VPN from the client. If you are using routing (i.e.dev tunin the server config file), try:

ping 10.8.0.1

If you are using bridging (i.e. dev tap in the server config file), try to ping the IP address of a machine on the server's ethernet subnet.

If the ping succeeds, congratulations! You now have a functioning VPN.

Troubleshooting

If the ping failed or the OpenVPN client initialization failed to complete, here is a checklist of common symptoms and their solutions:

  • You get the error message: TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity). This error indicates that the client was unable to establish a network connection with the server.

    Solutions:

    • Make sure the client is using the correct hostname/IP address and port number which will allow it to reach the OpenVPN server.
    • If the OpenVPN server machine is a single-NIC box inside a protected LAN, make sure you are using a correct port forward rule on the server's gateway firewall. For example, suppose your OpenVPN box is at 192.168.4.4 inside the firewall, listening for client connections on UDP port 1194. The NAT gateway servicing the 192.168.4.x subnet should have a port forward rule that saysforward UDP port 1194 from my public IP address to 192.168.4.4.
    • Open up the server's firewall to allow incoming connections to UDP port 1194 (or whatever TCP/UDP port you have configured in the server config file).
  • You get the error message: Initialization Sequence Completed with errors-- This error can occur on Windows if (a) You don't have the DHCP client service running, or (b) You are using certain third-party personal firewalls on XP SP2.

    Solution: Start the DHCP client server and make sure that you are using a personal firewall which is known to work correctly on XP SP2.

  • You get the Initialization Sequence Completedmessage but the ping test fails -- This usually indicates that a firewall on either server or client is blocking VPN network traffic by filtering on the TUN/TAP interface.

    Solution: Disable the client firewall (if one exists) from filtering the TUN/TAP interface on the client. For example on Windows XP SP2, you can do this by going toWindows Security Center -> Windows Firewall -> Advanced and unchecking the box which corresponds to the TAP-Windows adapter (disabling the client firewall from filtering the TUN/TAP adapter is generally reasonable from a security perspective, as you are essentially telling the firewall not to block authenticated VPN traffic). Also make sure that the TUN/TAP interface on the server is not being filtered by a firewall (having said that, note that selective firewalling of the TUN/TAP interface on the server side can confer certain security benefits. See theaccess policies section below).

  • The connection stalls on startup when using a proto udpconfiguration, the server log file shows this line:
    TLS: Initial packet from x.x.x.x:x, sid=xxxxxxxx xxxxxxxx

    however the client log does not show an equivalent line.

    Solution: You have a one-way connection from client to server. The server to client direction is blocked by a firewall, usually on the client side. The firewall can either be (a) a personal software firewall running on the client, or (b) the NAT router gateway for the client. Modify the firewall to allow returning UDP packets from the server to reach the client.

See the FAQ for additional troubleshooting information.

Configuring OpenVPN to run automatically on system startup

The lack of standards in this area means that most OSes have a different way of configuring daemons/services for autostart on boot. The best way to have this functionality configured by default is to install OpenVPN as a package, such as via RPM on Linux or using the Windows installer.

Linux

If you install OpenVPN via an RPM or DEB package on Linux, the installer will set up aninitscript. When executed, the initscript will scan for.conf configuration files in/etc/openvpn, and if found, will start up a separate OpenVPN daemon for each file.

Windows

The Windows installer will set up a Service Wrapper, but leave it turned off by default. To activate it, go to Control Panel / Administrative Tools / Services, select the OpenVPN service, right-click on properties, and set the Startup Type to Automatic. This will configure the service for automatic start on the next reboot.

When started, the OpenVPN Service Wrapper will scan the \Program Files\OpenVPN\config folder for.ovpn configuration files, starting a separate OpenVPN process on each file.

Controlling a running OpenVPN process

Running on Linux/BSD/Unix

OpenVPN accepts several signals:

  • SIGUSR1 -- Conditional restart, designed to restart without root privileges
  • SIGHUP -- Hard restart
  • SIGUSR2 -- Output connection statistics to log file or syslog
  • SIGTERMSIGINT -- Exit

Use the writepid directive to write the OpenVPN daemon's PID to a file, so that you know where to send the signal (if you are starting openvpn with aninitscript, the script may already be passing a--writepid directive on theopenvpn command line).

Running on Windows as a GUI

Running in a Windows command prompt window

On Windows, you can start OpenVPN by right clicking on an OpenVPN configuration file (.ovpn file) and selecting "Start OpenVPN on this config file".

Once running in this fashion, several keyboard commands are available:

  • F1 -- Conditional restart (doesn't close/reopen TAP adapter)
  • F2 -- Show connection statistics
  • F3 -- Hard restart
  • F4 -- Exit

Running as a Windows Service

When OpenVPN is started as a service on Windows, the only way to control it is:

  • Via the service control manager (Control Panel / Administrative Tools / Services) which gives start/stop control.
  • Via the management interface (see below).

Modifying a live server configuration

While most configuration changes require you to restart the server, there are two directives in particular which refer to files which can be dynamically updated on-the-fly, and which will take immediate effect on the server without needing to restart the server process.

client-config-dir -- This directive sets a client configuration directory, which the OpenVPN server will scan on every incoming connection, searching for a client-specific configuration file (see thethe manual page for more information). Files in this directory can be updated on-the-fly, without restarting the server. Note that changes in this directory will only take effect for new connections, not existing connections. If you would like a client-specific configuration file change to take immediate effect on a currently connected client (or one which has disconnected, but where the server has not timed-out its instance object), kill the client instance object by using the management interface (described below). This will cause the client to reconnect and use the newclient-config-dir file.

crl-verify -- This directive names a Certificate Revocation List file, described below in theRevoking Certificates section. The CRL file can be modified on the fly, and changes will take effect immediately for new connections, or existing connections which are renegotiating their SSL/TLS channel (occurs once per hour by default). If you would like to kill a currently connected client whose certificate has just been added to the CRL, use the management interface (described below).

Status File

The default server.conf file has a line

status openvpn-status.log

which will output a list of current client connections to the file openvpn-status.logonce per minute.

Using the management interface

The OpenVPN management interface allows a great deal of control over a running OpenVPN process. You can use the management interface directly, by telneting to the management interface port, or indirectly by using anOpenVPN GUI which itself connects to the management interface.

To enable the management interface on either an OpenVPN server or client, add this to the configuration file:

management localhost 7505

This tells OpenVPN to listen on TCP port 7505 for management interface clients (port 7505 is an arbitrary choice -- you can use any free port).

Once OpenVPN is running, you can connect to the management interface using a telnet client. For example:

ai:~ # telnet localhost 7505
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
>INFO:OpenVPN Management Interface Version 1 -- type 'help' for more info
help
Management Interface for OpenVPN 2.0_rc14 i686-suse-linux [SSL] [LZO] [EPOLL] built on Feb 15 2005
Commands:
echo [on|off] [N|all]  : Like log, but only show messages in echo buffer.
exit|quit              : Close management session.
help                   : Print this message.
hold [on|off|release]  : Set/show hold flag to on/off state, or
                         release current hold and start tunnel.
kill cn                : Kill the client instance(s) having common name cn.
kill IP:port           : Kill the client instance connecting from IP:port.
log [on|off] [N|all]   : Turn on/off realtime log display
                         + show last N lines or 'all' for entire history.
mute [n]               : Set log mute level to n, or show level if n is absent.
net                    : (Windows only) Show network info and routing table.
password type p        : Enter password p for a queried OpenVPN password.
signal s               : Send signal s to daemon,
                         s = SIGHUP|SIGTERM|SIGUSR1|SIGUSR2.
state [on|off] [N|all] : Like log, but show state history.
status [n]             : Show current daemon status info using format #n.
test n                 : Produce n lines of output for testing/debugging.
username type u        : Enter username u for a queried OpenVPN username.
verb [n]               : Set log verbosity level to n, or show if n is absent.
version                : Show current version number.
END
exit
Connection closed by foreign host.
ai:~ #

Expanding the scope of the VPN to include additional machines on either