Blame
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 1 | # OpenVPN - Getting started How-To |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 2 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 3 | Setting up a VPN based on OpenVPN requires setting up a few "groups" of configuration options. Each of them covers separate elements of a VPN tunnel. One part is the connection between server and clients. Next up is the encryption layer, then there is the authentication layer and at the end we cover the network inside the tunnel. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 4 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 5 | One nifty detail about OpenVPN configuration options. Almost all of these options can be used either on the command line directly or via a configuration file. The main difference is that on the command line you must use two leading dashes (--) for OpenVPN to understand what you mean. When using these options in the configuration file must not use any leading dashes at all. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 6 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 7 | When starting OpenVPN, you can either use the --config option to tell OpenVPN which configuration file to use. Or if you do not use any options at all, you can just provide the file name directly. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 8 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 9 | ``` |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 10 | # openvpn --config myvpn.conf |
| 11 | # openvpn myvpn.conf |
|||
| 12 | ``` |
|||
| 13 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 14 | Notice that you can use --config multiple times, to merge several configuration files. Or you can use 'config' inside a configuration file to "include" another configuration file. |
| 15 | ||||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 16 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 17 | ## Setting up the connection |
| 18 | You must first of all decide if you want to use UDP or TCP for connections. Generally speaking, UDP is the preferred alternative in most cases. If you cannot get a reliable UDP connection, then you might need to look into TCP. The reason for avoding TCP can be found here: |
|||
| 19 | http://sites.inka.de/bigred/devel/tcp-tcp.html |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 20 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 21 | Using UDP requires just to add |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 22 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 23 | ``` |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 24 | proto udp |
| 25 | ``` |
|||
| 26 | ||||
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 27 | To use TCP, you need: |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 28 | ``` |
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 29 | proto tcp |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 30 | ``` |
| 31 | ||||
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 32 | This needs to be added to both client and server configurations. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 33 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 34 | Then a port number is needed. The official OpenVPN port number is 1194, but any port number between 1 and 65535 will work. If you don't provide the 'port' option, 1194 will be used. An example using port 443 |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 35 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 36 | ``` |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 37 | port 443 |
| 38 | ``` |
|||
| 39 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 40 | In the client configuration you need to tell where to connect. Both hostnames and IP addresses can be used. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 41 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 42 | ``` |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 43 | remote myvpn.example.com |
| 44 | remote 192.168.200.200 |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 45 | ``` |
| 46 | ||||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 47 | You can list multiple --remote options in the configuration file, and OpenVPN will try all of them until it gets a connection. |
| 48 | ||||
| 49 | You can also set different port numbers and protocols for each --remote, like this: |
|||
| 50 | ||||
| 51 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 52 | remote myvpn.example.com 1194 udp |
| 53 | remote myvpn.example.com 443 tcp-client |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 54 | ``` |
| 55 | ||||
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 56 | For advanced setups, it is also possible to use <connection> blocks, read more about that in the [OpenVPN man page](https://build.openvpn.net/man/openvpn-2.7/openvpn.8.html). |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 57 | |
| 58 | If you want to run multiple VPN clients on the same host, it is advisable to also add 'nobind' to your configuration file. This makes OpenVPN use a random client side port when connecting. Without it, it will use the same port number as used to connect to the server. |
|||
| 59 | ||||
| 60 | On the server side, you can use --local to tell OpenVPN to listen on a particular IP address. For example listening to IP address 192.168.100.1, you need first to have a network adapter configured with this IP address. Then you can add this line to the server configuration: |
|||
| 61 | ||||
| 62 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 63 | local 192.168.100.1 |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 64 | ``` |
| 65 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 66 | ## Configuring encryption |
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 67 | OpenVPN can work in two different modes in regards to encryption. This guide will describe the way OpenVPN is commonly used, using Public Key Infrastricture (PKI). Newer OpenVPN releases also supports what is called `--peer-fingerprint`, which is a more lightweight setup than PKI. There are good examples for the `peer-fingerprint` approach in the [`openvpn-examples(5)` man-page](https://github.com/OpenVPN/openvpn/blob/master/doc/man-sections/example-fingerprint.rst). |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 68 | |
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 69 | In PKI mode. it allows multiple clients to connect to the same server, where each client and server have separate keys. A PKI setup requires a Certificate Authority (CA), which issues certificates for both the server and clients. There exists plenty of alternatives for CA management. You should NOT go any where to buy yourself new certificates as that will make the VPN tunnel much less secure (unless you add extra authentication layers). In addition to that, a commercial certificate for OpenVPN does not provide you with any additional benefits. You will need to control your own CA for optimal security. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 70 | |
| 71 | Setting up a proper CA is not covered in this How-To. But a good starting point will be to look at easy-rsa, in particular version 3. |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 72 | https://github.com/OpenVPN/easy-rsa/blob/master/doc/EasyRSA-Readme.md |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 73 | |
| 74 | **BEWARE:** |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 75 | One common mistake when setting up a new CA is to place all the CA files on the OpenVPN server. [[span(style=background: #FFD0D0;padding-left: 6px; padding-right:6px;, DO ***NOT*** DO THAT! )] A CA requires a private key which is used for signing the certificates your clients and servers will use. If you loose control of your CA private key, you can no longer trust any certificates from this CA. Anyone with access to this CA private key can sign new certificates without your knowledge, which then can connect to your OpenVPN server without needing to modify anything on the VPN server. Place your CA files on a storage which can be offline as much as possible, only to be activated when you need to get a new certificate for a client or server. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 76 | |
| 77 | The files you need to copy out from a CA are just 3 files for each client and server. |
|||
| 78 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 79 | * Private key (often a .key or .pem file) |
| 80 | * Certificate (often a .crt or .pem file) |
|||
| 81 | * CA certificate (also a .crt or .pem file) |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 82 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 83 | The server in addition needs a DH parameters file. This can be generated by using the OpenSSL: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 84 | |
| 85 | ``` |
|||
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 86 | $ openssl dhparam -out dh4096.pem 4096 |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 87 | ``` |
| 88 | ||||
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 89 | The 4096 indicates how many bits the DH prime number should consist of. The value of 4096 bits should in most cases be secure enough (as of October 2025), but it is ideal to have the DH prime number length to match the length of the RSA key. |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 90 | |
| 91 | BEWARE: |
|||
| 92 | You should avoid generating keys on any devices which does not have a good entropy source for random data. This includes most of the common wifi routers and similar embedded devices. In many cases virtual machines also does not have a good entropy source or it can be manipulated by the hypervisor. Try as far as possible to generate keys and DH parameters on bare-metal equipment. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 93 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 94 | To better understand how PKI work, have a look at this introduction: https://github.com/OpenVPN/easy-rsa/blob/master/doc/Intro-To-PKI.md |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 95 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 96 | Now we need to make use of these key and certificate files in the OpenVPN configuration files. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 97 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 98 | Server config: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 99 | |
| 100 | ``` |
|||
| 101 | tls-server |
|||
| 102 | key server-key.pem |
|||
| 103 | cert server-crt.pem |
|||
| 104 | ca ca-crt.pem |
|||
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 105 | dh dh4096.pem |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 106 | remote-cert-eku "TLS Web Client Authentication" |
| 107 | ``` |
|||
| 108 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 109 | The client config need to look something like this: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 110 | |
| 111 | ``` |
|||
| 112 | tls-client |
|||
| 113 | key client-key.pem |
|||
| 114 | cert client-key.pem |
|||
| 115 | ca ca-crt.pem |
|||
| 116 | remote-cert-eku "TLS Web Server Authentication" |
|||
| 117 | ``` |
|||
| 118 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 119 | This provides a fairly good and secure starting point for an OpenVPN client and server to start talking to each other. And when certificates are used, the first level of authentication is already added. Only clients with a certificate signed by the CA identified in ca-crt.pem will be accepted on the server. And the client will also authenticate that the server certificate is signed by the CA the client has in its local ca-crt.pem file. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 120 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 121 | The --remote-cert-eku is optional, but highly recommended. It ensures that a server will verify that the client certificate provided is truly a client certificate, and vice versa for the client which checks that the server certificate truly aimed for a server. Otherwise, an OpenVPN server can use a client certificate acting as a server. We can add a few more hardening steps, but will come back to that later on. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 122 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 123 | And then there is the --tls-server and --tls-client options. They are just used to tell OpenVPN that it will act as a server or client with the TLS layers activated. Those options are needed for --key, --cert and --ca to be accepted. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 124 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 125 | With this in place, the certificates and private keys are actually just used to secure the exchange of a temporary encryption key for the OpenVPN session. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 126 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 127 | This temporary encryption key (which you will not see for yourself; it will be in RAM only) is used for encrypting the data which will be passed over the VPN connection, also known as the data channel. So all your network traffic between your server and client goes in the data channel and will be encrypted by this temporary key. |
| 128 | ||||
| 129 | The encryption algorithm which is used for the data channel can be modified as well. OpenVPN mostly provides the same algorithms as your SSL library supports. To see which algorithms are available, see the outpout of: |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 130 | |
| 131 | ``` |
|||
| 132 | $ openvpn --show-ciphers |
|||
| 133 | ``` |
|||
| 134 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 135 | Those ciphers which are listed with '(variable)' in the output can have a variable key length, controlled by the --keysize option. |
| 136 | ||||
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 137 | **WARNING:** By default OpenVPN used the Blowfish cipher, but that is discouraged due to general issues with Blowfish, RC4, CAST5 and DES/3DES. (See [wiki:SWEET32] for details) OpenVPN 2.6 and newer will negotiate the best encryption cipher both sides supports. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 138 | |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 139 | To use the preferred AES-GCM algorithm with 256 bits encryption, add this line to both client and server configs: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 140 | |
| 141 | ``` |
|||
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 142 | cipher AES-256-GCM |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 143 | ``` |
| 144 | ||||
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 145 | You can do another step to strengthen the encryption layer. The temporary session key was already mentioned, which is used for encrypting the tunnelled network data. This key will rotate by default every hour. But you can also tweak how often it gets rotated by adjusting --reneg-sec, --reneg-pkts and --reneg-bytes. See the [OpenVPN man page](https://build.openvpn.net/man/openvpn-2.7/openvpn.8.html) for more information about these options. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 146 | |
| 147 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 148 | ## Configuring authentication |
| 149 | There are several more authentication layers which can be added in OpenVPN on top of the basic one which certificates provides. The authentication layers in this section is purely optional. But it is advisable to add at least one or more of them. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 150 | |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 151 | ### TLS Crypt |
| 152 | OpenVPN has supported `--tls-auth` many years, which added a kind of firewall against attacks when using the UDP protocol. It would also give some layer of protection when the VPN tunnel used the TCP protocol too. This enabled OpenVPN to be able to drop network packets coming from the Internet which wasn't signed correctly with a key provided to the `--tls-auth`. |
|||
| 153 | ||||
| 154 | This feature has been extended, with `--tls-crypt` and later on `--tls-crypt-v2`. Using one of these features, another layer of symmetric encryption is added on top of the encrypted OpenVPN. In these days where Post-Quantum encryption is a hot topic, this feature can add a good layer of protection to what is considered plausible attack vectors from systems capable of quantum computing. This is especially important if you are concerned encrypted OpenVPN traffic will be stored now, to be cracked open later on in the future. And this can add some post-quantum protection if your connecting devices are not capable of using any post-quantum algorithms. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 155 | |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 156 | There are two versions of TLS Crypt. For smaller setups where you trust all the users connecting to your VPN server, using the `--tls-crypt` alone gives you a reasonable protection. All users will here use a shared secret key. The risk is if this shared secret key is leaked. And a leaked shared key can be used to crack open already collected encrypted VPN traffic. The `--tls-crypt` shared secret key uses the exact same key format as `--tls-auth`. |
| 157 | ||||
| 158 | If you have more users, you might want to consider using `--tls-crypt-v2`. And this is what will be described here. |
|||
| 159 | ||||
| 160 | First you need to generate a TLS Crypt v2 server key: |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 161 | |
| 162 | ``` |
|||
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 163 | $ openvpn --genkey tls-crypt-v2-server server.tc2 |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 164 | ``` |
| 165 | ||||
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 166 | In the OpenVPN server configuration, add this line: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 167 | ``` |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 168 | tls-crypt-v2 server.tc2 |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 169 | ``` |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 170 | |
| 171 | For each client you prepare a configuration profile for, you will need to create a unique tls-crypt-v2 client key as well. |
|||
| 172 | ||||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 173 | ``` |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 174 | $ openvpn --tls-crypt-v2 server.tc2 --genkey tls-crypt-v2-client |
| 175 | -----BEGIN OpenVPN tls-crypt-v2 client key----- |
|||
| 176 | nwnTidKiY0OVXY/5Bu4tXA5dk/mEH/oLdEp+oePyU19Ivwnp0oCZQtQaibGMiMjA |
|||
| 177 | pXQASHEVHoJrpgXDJnsAlSPL8esmbasAeMJTZpNXNGj5Af38s0CaQZIleSVzd6hv |
|||
| 178 | IkGzM+qHPBnJBw3+YjRsqOMUGxcjwWqAe8JCvwDpxiqthUUdHfuKmJ1FD1xjWORp |
|||
| 179 | WqYz2ejIEIUcGWcRP6WVPxydkE/p2CEW7LnbaWq3Mebh3Fkbo7SBvRHqQGP2hxFN |
|||
| 180 | 9hpgUj6DHigw9uKFonAPmh8Lf4ZahT2B5nJDPUVxZmfYIvTm2a+t1u7ZfvrYY1cN |
|||
| 181 | KkhOtRgySC3wG/tOIV6TKjAzIR05wYuOSs0sRPorMjBguyPn+A+q3mAOBXW8nKaQ |
|||
| 182 | iRKWsbxeN8ijF2osbPLdLe5CA3Md58fT7WGRpH8DkS3SB/ViflDBc8ciWnioKtpU |
|||
| 183 | 48F4GddTYoHkIR9wF4N6urFECRxKM3YmxNHuu20JM/25PAPVxNcPAmH3Y96pr1QD |
|||
| 184 | pmeXLlKu/E/jsj+z7kWqGr55hzdXVAJ1i6BoymtUOhCcPvviegoYoFIV8jFlUFzc |
|||
| 185 | kLcoYgci+ZeWw+0lNba/Viz77G/AxS9pJ+/p22kohjmxLLC7Dn+tn2iZHLrLO+Rc |
|||
| 186 | sov3TzPWQl45oTbPtScUWTDu525tJAngy6NOf/dfYZcSZQf4XAsoPFLmptNt6MWM |
|||
| 187 | yJHb7j0arrKimVy0GjgdbSI81SNCJZHMcgEr |
|||
| 188 | -----END OpenVPN tls-crypt-v2 client key----- |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 189 | ``` |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 190 | This can be written directly to a file if you prefer, by adding an additional argument with the filename to use. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 191 | |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 192 | In the client configuration, you can now add this output and encapsulate it in `<tls-crypt-v2>{...}</tls-crypt-v2>` tags. |
| 193 | ``` |
|||
| 194 | <tls-crypt-v2> |
|||
| 195 | -----BEGIN OpenVPN tls-crypt-v2 client key----- |
|||
| 196 | nwnTidKiY0OVXY/5Bu4tXA5dk/mEH/oLdEp+oePyU19Ivwnp0oCZQtQaibGMiMjA |
|||
| 197 | pXQASHEVHoJrpgXDJnsAlSPL8esmbasAeMJTZpNXNGj5Af38s0CaQZIleSVzd6hv |
|||
| 198 | IkGzM+qHPBnJBw3+YjRsqOMUGxcjwWqAe8JCvwDpxiqthUUdHfuKmJ1FD1xjWORp |
|||
| 199 | WqYz2ejIEIUcGWcRP6WVPxydkE/p2CEW7LnbaWq3Mebh3Fkbo7SBvRHqQGP2hxFN |
|||
| 200 | 9hpgUj6DHigw9uKFonAPmh8Lf4ZahT2B5nJDPUVxZmfYIvTm2a+t1u7ZfvrYY1cN |
|||
| 201 | KkhOtRgySC3wG/tOIV6TKjAzIR05wYuOSs0sRPorMjBguyPn+A+q3mAOBXW8nKaQ |
|||
| 202 | iRKWsbxeN8ijF2osbPLdLe5CA3Md58fT7WGRpH8DkS3SB/ViflDBc8ciWnioKtpU |
|||
| 203 | 48F4GddTYoHkIR9wF4N6urFECRxKM3YmxNHuu20JM/25PAPVxNcPAmH3Y96pr1QD |
|||
| 204 | pmeXLlKu/E/jsj+z7kWqGr55hzdXVAJ1i6BoymtUOhCcPvviegoYoFIV8jFlUFzc |
|||
| 205 | kLcoYgci+ZeWw+0lNba/Viz77G/AxS9pJ+/p22kohjmxLLC7Dn+tn2iZHLrLO+Rc |
|||
| 206 | sov3TzPWQl45oTbPtScUWTDu525tJAngy6NOf/dfYZcSZQf4XAsoPFLmptNt6MWM |
|||
| 207 | yJHb7j0arrKimVy0GjgdbSI81SNCJZHMcgEr |
|||
| 208 | -----END OpenVPN tls-crypt-v2 client key----- |
|||
| 209 | </tls-crypt-v2> |
|||
| 210 | ``` |
|||
| 211 | If you want to load it from a file instead: |
|||
| 212 | ``` |
|||
| 213 | tls-crypt-v2 client.tc2 |
|||
| 214 | ``` |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 215 | |
| 3ada27 | David Sommerseth | 2026-12-04 21:07:03 | 216 | If a client tries to connect to your server with `--tls-crypt-v2` enabled and that client tries to use a `tls-crypt-v2` client key |
| 217 | which is not generated with the same `tls-crypt-v2` key the server uses, the client will fail to connect. You could say that the |
|||
| 218 | client keys are signed by the server key, and the server will recognise its signature when a client tries to connect. |
|||
| 219 | ||||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 220 | ### Username / password authentication |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 221 | There exists many methods for adding username/password authentication. There exists plenty of plug-ins and scripts for PAM, LDAP, Radius and so on. There are also more advanced authentication and access controls available, such as |
| 222 | the eurephia project. We will not cover any of these setups here. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 223 | |
| 224 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 225 | ### Even stricter certificate checks |
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 226 | It is also possible through a plug-in or the `--tls-verify` script hook to add additional checks on certificates. This can also protect you somewhat better if you loose control over your CA private key, if you check the client certificate's finger print/digest against a local database you have collected. |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 227 | |
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 228 | For client configurations you can also easily add `--verify-x509-name`, where you provide for example the certificate subject of the server. This will ensure that only a server who identifies itself as a particular server will be accepted. For example like this: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 229 | |
| b9c87b | Samuli Seppänen | 2025-02-11 08:12:19 | 230 | ``` |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 231 | verify-x509-name 'C=US, O=Test CA, CN=myvpn.example.com' subject |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 232 | ``` |
| 233 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 234 | You can also just check against the CN attribute in the subject: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 235 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 236 | ``` |
| 237 | verify-x509-name myvpn.example.com name |
|||
| 238 | ``` |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 239 | |
| 240 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 241 | ### Adding certificate revocation lists |
| 242 | Within the CA, you can also revoke certificates as needed. Using the CA management tool of your choice, you should be able to generate a Certificate Revocation List (CRL file). By adding this to the OpenVPN server, all client certificates will be checked against this revocation list. Clients which have their certificates listed in the CRL will not be able to connect. This is a common way to disable access to a VPN service on a per user level. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 243 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 244 | Add this line to the OpenVPN server configuration: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 245 | |
| 246 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 247 | crl /full/path/to/crl.pem |
| 248 | ``` |
|||
| 249 | ||||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 250 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 251 | ## Configuring the network layer |
| 252 | So far we've covered getting a connection between an OpenVPN client and server and the server and client have authenticated each other through various methods. So the client and server can now communicate, but they have no idea what to do with the network. So lets configure that. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 253 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 254 | First we need to setup a virtual network device. OpenVPN supports two types of such devices, TUN and TAP. The difference is that TUN only |
| 255 | passes TCP/IP traffic and does not provide any broadcast traffic across the VPN tunnel. This provides a fairly efficient tunnel with the lowest overhead. For more advanced setups the TAP device may be used, which is also able to transport other protocols than just TCP/IP. TAP mode transports Ethernet frames instead of just IP packets. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 256 | |
| 054e8c | David Sommerseth | 2026-12-04 21:24:53 | 257 | So there are many ways to configure the network layer in OpenVPN. The general recommendation, and the one we will cover here, is what is often called "routed tun". That means we use a tun device and use traditional TCP/IP routing techniques. For an overview of TAP mode and bridging vs routing, see the [Bridging and Routing](/Pages/BridgingAndRouting) page for more information. |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 258 | |
| 259 | To configure a tun device, just add this line to both client and server configurations: |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 260 | |
| b9c87b | Samuli Seppänen | 2025-02-11 08:12:19 | 261 | ``` |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 262 | dev tun |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 263 | ``` |
| 264 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 265 | You can also provide a specific tun device, for example tun0 instead of tun. But if the 'tun0' device is already configured, OpenVPN will fail. |
| 266 | ||||
| 267 | It is also possible to use your own name for the virtual network device, but you then need to use --dev-type in addition. |
|||
| 268 | ||||
| 269 | ``` |
|||
| 270 | dev myvpn |
|||
| 271 | dev-type tun |
|||
| 272 | ``` |
|||
| 273 | ||||
| 274 | BEWARE: |
|||
| 275 | The device type must be identical on server and clients. You cannot use TAP on clients and TUN on servers or vice versa. |
|||
| 276 | ||||
| 277 | Next we will need to configure IP addresses. In this How-To we will push the IP address the client should use from the server to the client, as that is the simplest setups. In the server configuration add this line: |
|||
| 278 | ||||
| 279 | ``` |
|||
| 280 | topology subnet |
|||
| 281 | server 10.8.0.0 255.255.255.0 |
|||
| 282 | ``` |
|||
| 283 | ||||
| 284 | This sets up a VPN subnet using the 10.8.0.XXX address scope. The server will become 10.8.0.1, and the first client will start at 10.8.0.2. This is due also due to that we set OpenVPN to use the subnet toplogy. This is the recommend OpenVPN setup and behaves more closer to traditional networks. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 285 | |
| 286 | In the client configuration, you need to add: |
|||
| 287 | ||||
| 288 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 289 | topology subnet |
| 290 | pull |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 291 | ``` |
| 292 | ||||
| 293 | This tells the client that the server uses the subnet topology and that it should use the IP address and routing the VPN server provides. |
|||
| 294 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 295 | At this point, it should be possible to start up both the server and client. They should be able to connect and from the VPN client you should be able to ping 10.8.0.1 and from the server side you should be able to ping 10.8.0.2. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 296 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 297 | If you want to access particular network resources on other IP addresses via the VPN tunnel, you need to add network routes. A network route tells your operating system where it needs to send the network traffic when you want to access certain resources. An operating system can handle multiple routes via multiple gateways at the same time. So if you have a server on 192.168.1.10 behind your VPN server and you want to access this server via the VPN, you need to tell OpenVPN to configure a route for either a specific host or a network range to go via the tunnel. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 298 | |
| 299 | So to configure this, you need to add one line in the server configuration and restart server and client. |
|||
| 300 | ||||
| 301 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 302 | push "route 192.168.1.0 255.255.255.0" |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 303 | ``` |
| 304 | ||||
| 305 | When the client now connects, the server tells the VPN client that it should route all traffic for IP addresses in the 192.168.1.XXX scope via the VPN connection. |
|||
| 306 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 307 | This is a very basic setup. And when we now start on the routing part, the VPN setup is mostly done. All you need now is to add the needed routes you need, just like you would do for normal TCP/IP routing. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 308 | |
| 309 | **BEWARE**: |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 310 | Remember that you also need to consider what is called "return routes". If your VPN client can access a host behind your VPN server, it does not mean that the host behind the VPN server will send the response via the same route. So you need to ensure that your hosts behind your VPN server also knows which gateway to use for your VPN. Nowadays this is most commonly fixed by adding a route on your existing default gateway. And if you run OpenVPN on an existing gateway, you have this return route already impllicitly configured. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 311 | |
| 312 | For a more detailed example using routing, see the [Using routing](https://community.openvpn.net/openvpn/wiki/BridgingAndRouting#Usingrouting) section in the 'Bridiging and routing' wiki page. |
|||
| 313 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 314 | ### Routing everything over the VPN |
| 315 | It is possible to route absolutely all network traffic over the VPN. The configuration in OpenVPN is fairly simple. But you will need to investigate how to configure NAT on your VPN server for the virtual tun adapter. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 316 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 317 | You can either push such a "route everything over VPN" via the server, or you can add it explicitly in the client configuration. Do not use both at the same time. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 318 | |
| 319 | Server push: |
|||
| 320 | ||||
| 321 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 322 | push "redirect-gateway def1" |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 323 | ``` |
| 324 | Client configuration alternative: |
|||
| 325 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 326 | redirect-gateway def1 |
| b9c87b | Samuli Seppänen | 2025-02-11 08:12:19 | 327 | ``` |
| 328 | ||||
| 329 | ### What about IPv6? |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 330 | OpenVPN v2.3 and later supports IPv6. To set up IPv6 in the tunnel is pretty much the same as for the IPv4 examples we already have covered. You need to use the --server-ipv6 and --route-ipv6 options to configure IPv6. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 331 | |
| 332 | For example, adding this will configure the IPv6 addresses for server and clients: |
|||
| 333 | ``` |
|||
| 334 | server-ipv6 2001:db8:cada::/64 |
|||
| 335 | ``` |
|||
| 336 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 337 | You can use the --route-ipv6 option, either pushing it from the server or using it directly in the client configuration, just as you can with the --route option. The syntax is similar too: |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 338 | ``` |
| 339 | route-ipv6 2001:db8:daca::/64 |
|||
| 340 | ``` |
|||
| 341 | ||||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 342 | |
| 343 | ## Other aspects to consider when configuring a VPN |
|||
| 344 | There are a few more things which may need to be configured, but those are mostly outside of OpenVPN. The most common issues are related to adjusting your operating system to allow forwarding of packets and configuring the firewall properly. |
|||
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 345 | |
| 346 | On most Unix/Linux based operating systems, setting up IP forwarding is just a matter of doing: |
|||
| 347 | ``` |
|||
| 348 | sysctl net.ipv4.ip_forward=1 |
|||
| 349 | sysctl net.ipv6.conf.all.forwarding=1 |
|||
| 350 | ``` |
|||
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 351 | These values are reset upon boot, so they can also be stored in /etc/sysctl.conf to be enabled at boot. |
| 8c972b | Samuli Seppänen | 2025-02-11 08:09:27 | 352 | |
| 8432e1 | Samuli Seppänen | 2025-02-27 09:51:28 | 353 | Configuring firewall is so different between Linux and other Unix based OSes, in addition several Linux distributions have their own tools to manage iptables. So it is better to read the manuals for the firewall configuration on your operating system. |
