.\" Man page generated from reStructuredText. . .TH OPENVPN EXAMPLES 5 "" "" "Configuration files" .SH NAME openvpn examples \- Secure IP tunnel daemon . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .SH INTRODUCTION .sp This man page gives a few simple examples to create OpenVPN setups and configuration files. .SH SMALL OPENVPN SETUP WITH PEER-FINGERPRINT .sp This section consists of instructions how to build a small OpenVPN setup with the \fBpeer\-fingerprint\fP option. This has the advantage of being easy to setup and should be suitable for most small lab and home setups without the need for a PKI. For bigger scale setup setting up a PKI (e.g. via easy\-rsa) is still recommended. .sp Both server and client configuration can be further modified to customise the setup. .SS Server setup .INDENT 0.0 .IP 1. 3 Install openvpn .sp Compile from source\-code (see \fIINSTALL\fP file) or install via a distribution (apt/yum/ports) or via installer (Windows). .IP 2. 3 Generate a self\-signed certificate for the server: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C openssl req \-x509 \-newkey ec:<(openssl ecparam \-name secp384r1) \-keyout server.key \-out server.crt \-nodes \-sha256 \-days 3650 \-subj \(aq/CN=server\(aq .ft P .fi .UNINDENT .UNINDENT .IP 3. 3 Generate SHA256 fingerprint of the server certificate .sp Use the OpenSSL command line utility to view the fingerprint of just created certificate: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C openssl x509 \-fingerprint \-sha256 \-in server.crt \-noout .ft P .fi .UNINDENT .UNINDENT .sp This output something similar to: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C SHA256 Fingerprint=00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff .ft P .fi .UNINDENT .UNINDENT .IP 4. 3 Write a server configuration (\fIserver.conf\fP): .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C # The server certificate we created in step 1 cert server.crt key server.key dh none dev tun # Listen on IPv6+IPv4 simultaneously proto udp6 # The ip address the server will distribute server 10.8.0.0 255.255.255.0 server\-ipv6 fd00:6f76:706e::/64 # A tun\-mtu of 1400 avoids problems of too big packets after VPN encapsulation tun\-mtu 1400 # The fingerprints of your clients. After adding/removing one here restart the # server # Notify clients when you restart the server to reconnect quickly explicit\-exit\-notify 1 # Ping every 60s, restart if no data received for 5 minutes keepalive 60 300 .ft P .fi .UNINDENT .UNINDENT .IP 5. 3 Add at least one client as described in the client section. .IP 6. 3 .INDENT 3.0 .TP .B Start the server. .INDENT 7.0 .IP \(bu 2 On systemd based distributions move \fIserver.crt\fP, \fIserver.key\fP and \fIserver.conf\fP to \fB/etc/openvpn/server\fP and start it via systemctl .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C sudo mv server.conf server.key server.crt /etc/openvpn/server sudo systemctl start openvpn\-server@server .ft P .fi .UNINDENT .UNINDENT .UNINDENT .UNINDENT .UNINDENT .SS Adding a client .INDENT 0.0 .IP 1. 3 Install OpenVPN .IP 2. 3 Generate a self\-signed certificate for the client. In this example the client name is alice. Each client should have a unique name. Replace alice with a different name for each client. .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C openssl req \-x509 \-newkey ec:<(openssl ecparam \-name secp384r1) \-nodes \-sha256 \-days 3650 \-subj \(aq/CN=alice\(aq .ft P .fi .UNINDENT .UNINDENT .sp This generate a certificate and a key for the client. The output of the command will look something like this: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C \-\-\-\-\-BEGIN PRIVATE KEY\-\-\-\-\- [base64 content] \-\-\-\-\-END PRIVATE KEY\-\-\-\-\- \-\-\-\-\- \-\-\-\-\-BEGIN CERTIFICATE\-\-\-\-\- [base 64 content] \-\-\-\-\-END CERTIFICATE\-\-\-\-\- .ft P .fi .UNINDENT .UNINDENT .IP 3. 3 Create a new client configuration file. In this example we will name the file \fIalice.ovpn\fP: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C # The name of your server to connect to remote yourserver.example.net client # use a random source port instead the fixed 1194 nobind # Uncomment the following line if you want to route # all traffic via the VPN # redirect\-gateway def1 ipv6 # To set a DNS server # dhcp\-option DNS 192.168.234.1 \-\-\-\-\-BEGIN PRIVATE KEY\-\-\-\-\- [Insert here the key created in step 2] \-\-\-\-\-END PRIVATE KEY\-\-\-\-\- \-\-\-\-\-BEGIN CERTIFICATE\-\-\-\-\- [Insert here the certificate created in step 2] \-\-\-\-\-END CERTIFICATE\-\-\-\-\- # This is the fingerprint of the server that we trust. We generated this fingerprint # in step 2 of the server setup peer\-fingerprint 00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff # The tun\-mtu of the client should match the server MTU tun\-mtu 1400 dev tun .ft P .fi .UNINDENT .UNINDENT .IP 4. 3 Generate the fingerprint of the client certificate. For that we will let OpenSSL read the client configuration file as the x509 command will ignore anything that is not between the begin and end markers of the certificate: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C openssl x509 \-fingerprint \-sha256 \-noout \-in alice.ovpn .ft P .fi .UNINDENT .UNINDENT .sp This will again output something like .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C SHA256 Fingerprint=ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00:ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00 .ft P .fi .UNINDENT .UNINDENT .IP 5. 3 Edit the \fIserver.conf\fP configuration file and add this new client fingerprint as additional line between \fB\fP and \fB\fP .sp After adding \fItwo\fP clients the part of configuration would look like this: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00:ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00 99:88:77:66:55:44:33:22:11:00:ff:ee:dd:cc:bb:aa:99:88:77:66:55:44:33:22:11:00:88:77:66:55:44:33 .ft P .fi .UNINDENT .UNINDENT .IP 6. 3 (optional) if the client is an older client that does not support the \fBpeer\-fingerprint\fP (e.g. OpenVPN 2.5 and older, OpenVPN Connect 3.3 and older), the client config \fIalice.ovpn\fP can be modified to still work with these clients. .sp Remove the line starting with \fBpeer\-fingerprint\fP\&. Then add a new \fB\fP section at the end of the configuration file with the contents of the \fBserver.crt\fP created in step 2 of the server setup. The end of \fIalice.ovpn\fP file should like: .INDENT 3.0 .INDENT 3.5 .sp .nf .ft C [...] # Beginning of the file skipped # The tun\-mtu of the client should match the server MTU tun\-mtu 1400 dev tun [contents of the server.crt] .ft P .fi .UNINDENT .UNINDENT .sp Note that we put the \fB\fP section after the \fB\fP section to make the fingerprint generation from step 4 still work since it will only use the first certificate it finds. .IP 7. 3 Import the file into the OpenVPN client or just use the \fBopenvpn alice.ovpn\fP to start the VPN. .UNINDENT .SH EXAMPLES .sp Prior to running these examples, you should have OpenVPN installed on two machines with network connectivity between them. If you have not yet installed OpenVPN, consult the INSTALL file included in the OpenVPN distribution. .SS Firewall Setup: .sp If firewalls exist between the two machines, they should be set to forward the port OpenVPN is configured to use, in both directions. The default for OpenVPN is 1194/udp. If you do not have control over the firewalls between the two machines, you may still be able to use OpenVPN by adding \fB\-\-ping 15\fP to each of the \fBopenvpn\fP commands used below in the examples (this will cause each peer to send out a UDP ping to its remote peer once every 15 seconds which will cause many stateful firewalls to forward packets in both directions without an explicit firewall rule). .sp Please see your operating system guides for how to configure the firewall on your systems. .SS VPN Address Setup: .sp For purposes of our example, our two machines will be called \fBbob.example.com\fP and \fBalice.example.com\fP\&. If you are constructing a VPN over the internet, then replace \fBbob.example.com\fP and \fBalice.example.com\fP with the internet hostname or IP address that each machine will use to contact the other over the internet. .sp Now we will choose the tunnel endpoints. Tunnel endpoints are private IP addresses that only have meaning in the context of the VPN. Each machine will use the tunnel endpoint of the other machine to access it over the VPN. In our example, the tunnel endpoint for bob.example.com will be 10.4.0.1 and for alice.example.com, 10.4.0.2. .sp Once the VPN is established, you have essentially created a secure alternate path between the two hosts which is addressed by using the tunnel endpoints. You can control which network traffic passes between the hosts (a) over the VPN or (b) independently of the VPN, by choosing whether to use (a) the VPN endpoint address or (b) the public internet address, to access the remote host. For example if you are on bob.example.com and you wish to connect to \fBalice.example.com\fP via \fBssh\fP without using the VPN (since \fBssh\fP has its own built\-in security) you would use the command \fBssh alice.example.com\fP\&. However in the same scenario, you could also use the command \fBtelnet 10.4.0.2\fP to create a telnet session with alice.example.com over the VPN, that would use the VPN to secure the session rather than \fBssh\fP\&. .sp You can use any address you wish for the tunnel endpoints but make sure that they are private addresses (such as those that begin with 10 or 192.168) and that they are not part of any existing subnet on the networks of either peer, unless you are bridging. If you use an address that is part of your local subnet for either of the tunnel endpoints, you will get a weird feedback loop. .SS Example 1: A simple tunnel without security (not recommended) .sp On bob: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openvpn \-\-remote alice.example.com \-\-dev tun1 \e \-\-ifconfig 10.4.0.1 10.4.0.2 \-\-verb 9 .ft P .fi .UNINDENT .UNINDENT .sp On alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openvpn \-\-remote bob.example.com \-\-dev tun1 \e \-\-ifconfig 10.4.0.2 10.4.0.1 \-\-verb 9 .ft P .fi .UNINDENT .UNINDENT .sp Now verify the tunnel is working by pinging across the tunnel. .sp On bob: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C ping 10.4.0.2 .ft P .fi .UNINDENT .UNINDENT .sp On alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C ping 10.4.0.1 .ft P .fi .UNINDENT .UNINDENT .sp The \fB\-\-verb 9\fP option will produce verbose output, similar to the \fBtcpdump\fP(8) program. Omit the \fB\-\-verb 9\fP option to have OpenVPN run quietly. .SS Example 2: A tunnel with self\-signed certificates and fingerprint .sp First build a self\-signed certificate on bob and display its fingerprint. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openssl req \-x509 \-newkey ec:<(openssl ecparam \-name secp384r1) \-keyout bob.pem \-out bob.pem \-nodes \-sha256 \-days 3650 \-subj \(aq/CN=bob\(aq openssl x509 \-noout \-sha256 \-fingerprint \-in bob.pem .ft P .fi .UNINDENT .UNINDENT .sp and the same on alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openssl req \-x509 \-newkey ec:<(openssl ecparam \-name secp384r1) \-keyout alice.pem \-out alice.pem \-nodes \-sha256 \-days 3650 \-subj \(aq/CN=alice\(aq openssl x509 \-noout \-sha256 \-fingerprint \-in alice.pem .ft P .fi .UNINDENT .UNINDENT .sp These commands will build a text file called \fBbob.pem\fP or \fBalice.pem\fP (in ascii format) that contain both self\-signed certificate and key and show the fingerprint of the certificates. Transfer the fingerprints over a secure medium such as by using the \fBscp\fP(1) or \fBssh\fP(1) program. .sp On bob: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openvpn \-\-ifconfig 10.4.0.1 10.4.0.2 \-\-tls\-server \-\-dev tun \-\-dh none \e \-\-cert bob.pem \-\-key bob.pem \-\-cipher AES\-256\-GCM \e \-\-peer\-fingerprint "$fingerprint_of_alices_cert" .ft P .fi .UNINDENT .UNINDENT .sp On alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openvpn \-\-remote bob.example.com \-\-tls\-client \-\-dev tun1 \e \-\-ifconfig 10.4.0.2 10.4.0.1 \-\-cipher AES\-256\-GCM \e \-\-cert alice.pem \-\-key alice.pem \e \-\-peer\-fingerprint "$fingerprint_of_bobs_cert" .ft P .fi .UNINDENT .UNINDENT .sp Now verify the tunnel is working by pinging across the tunnel. .sp On bob: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C ping 10.4.0.2 .ft P .fi .UNINDENT .UNINDENT .sp On alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C ping 10.4.0.1 .ft P .fi .UNINDENT .UNINDENT .sp Note: This example use a elliptic curve (\fIsecp384\fP), which allows \fB\-\-dh\fP to be set to \fBnone\fP\&. .SS Example 3: A tunnel with full PKI and TLS\-based security .sp For this test, we will designate \fBbob\fP as the TLS client and \fBalice\fP as the TLS server. .INDENT 0.0 .TP .B \fINote:\fP The client or server designation only has meaning for the TLS subsystem. It has no bearing on OpenVPN\(aqs peer\-to\-peer, UDP\-based communication model.* .UNINDENT .sp First, build a separate certificate/key pair for both bob and alice (see above where \fB\-\-cert\fP is discussed for more info). Then construct Diffie Hellman parameters (see above where \fB\-\-dh\fP is discussed for more info). You can also use the included test files \fBclient.crt\fP, \fBclient.key\fP, \fBserver.crt\fP, \fBserver.key\fP and \fBca.crt\fP\&. The \fB\&.crt\fP files are certificates/public\-keys, the \fB\&.key\fP files are private keys, and \fBca.crt\fP is a certification authority who has signed both \fBclient.crt\fP and \fBserver.crt\fP\&. For Diffie Hellman parameters you can use the included file \fBdh2048.pem\fP\&. .INDENT 0.0 .TP .B \fIWARNING:\fP All client, server, and certificate authority certificates and keys included in the OpenVPN distribution are totally insecure and should be used for testing only. .UNINDENT .sp On bob: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openvpn \-\-remote alice.example.com \-\-dev tun1 \e \-\-ifconfig 10.4.0.1 10.4.0.2 \e \-\-tls\-client \-\-ca ca.crt \e \-\-cert client.crt \-\-key client.key \e \-\-reneg\-sec 60 \-\-verb 5 .ft P .fi .UNINDENT .UNINDENT .sp On alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C openvpn \-\-remote bob.example.com \-\-dev tun1 \e \-\-ifconfig 10.4.0.2 10.4.0.1 \e \-\-tls\-server \-\-dh dh1024.pem \-\-ca ca.crt \e \-\-cert server.crt \-\-key server.key \e \-\-reneg\-sec 60 \-\-verb 5 .ft P .fi .UNINDENT .UNINDENT .sp Now verify the tunnel is working by pinging across the tunnel. .sp On bob: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C ping 10.4.0.2 .ft P .fi .UNINDENT .UNINDENT .sp On alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C ping 10.4.0.1 .ft P .fi .UNINDENT .UNINDENT .sp Notice the \fB\-\-reneg\-sec 60\fP option we used above. That tells OpenVPN to renegotiate the data channel keys every minute. Since we used \fB\-\-verb 5\fP above, you will see status information on each new key negotiation. .sp For production operations, a key renegotiation interval of 60 seconds is probably too frequent. Omit the \fB\-\-reneg\-sec 60\fP option to use OpenVPN\(aqs default key renegotiation interval of one hour. .SS Routing: .sp Assuming you can ping across the tunnel, the next step is to route a real subnet over the secure tunnel. Suppose that bob and alice have two network interfaces each, one connected to the internet, and the other to a private network. Our goal is to securely connect both private networks. We will assume that bob\(aqs private subnet is \fI10.0.0.0/24\fP and alice\(aqs is \fI10.0.1.0/24\fP\&. .sp First, ensure that IP forwarding is enabled on both peers. On Linux, enable routing: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C echo 1 > /proc/sys/net/ipv4/ip_forward .ft P .fi .UNINDENT .UNINDENT .sp This setting is not persistent. Please see your operating systems documentation how to properly configure IP forwarding, which is also persistent through system boots. .sp If your system is configured with a firewall. Please see your operating systems guide on how to configure the firewall. You typically want to allow traffic coming from and going to the tun/tap adapter OpenVPN is configured to use. .sp On bob: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C route add \-net 10.0.1.0 netmask 255.255.255.0 gw 10.4.0.2 .ft P .fi .UNINDENT .UNINDENT .sp On alice: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C route add \-net 10.0.0.0 netmask 255.255.255.0 gw 10.4.0.1 .ft P .fi .UNINDENT .UNINDENT .sp Now any machine on the \fI10.0.0.0/24\fP subnet can access any machine on the \fI10.0.1.0/24\fP subnet over the secure tunnel (or vice versa). .sp In a production environment, you could put the route command(s) in a script and execute with the \fB\-\-up\fP option. .\" Generated by docutils manpage writer. .