OpenVPN commands cheat sheet

This cheat sheet brings together the commands and settings for setting up a VPN server with OpenVPN on a Linux server: installation, a certificate authority and certificates with easy-rsa, a tls-crypt key, the server.conf configuration file, routing and NAT, the firewall, running it as a service, client files, certificate revocation, logs, and troubleshooting. You can search for a command, filter by topic, and copy it with one click.

Installation

  • sudo apt install openvpn easy-rsa

    Installs OpenVPN and easy-rsa, the tool for creating a certificate authority and certificates, on Ubuntu and Debian. A computer used only as a client needs just the openvpn package.

  • sudo dnf install epel-release sudo dnf install openvpn easy-rsa

    Installs OpenVPN and easy-rsa on AlmaLinux, Rocky Linux, and other RHEL-family distributions. The packages are in the EPEL repository, so enable it first.

  • openvpn --version

    Shows the OpenVPN version and the cryptographic library it was built with. The server and the clients can run different versions, but some settings exist only in recent versions.

Certificate authority and certificates with easy-rsa

  • make-cadir ~/easy-rsa cd ~/easy-rsa

    Creates an easy-rsa working folder on Ubuntu and Debian, with the easyrsa script and its configuration files. Run the rest of the commands in this section from this folder.

  • mkdir ~/easy-rsa ln -s /usr/share/easy-rsa/3/* ~/easy-rsa/ cd ~/easy-rsa

    Creates a working folder on RHEL-family distributions, with links to the easy-rsa files installed from the repository.

  • ./easyrsa init-pki

    Creates the pki folder, which holds the certificate authority (CA), the requests, the certificates, and the keys. Running it again in an existing folder deletes, after you confirm, all the certificates in it.

  • ./easyrsa build-ca

    Creates the certificate authority: the private key pki/private/ca.key and the certificate pki/ca.crt. The command asks for a key password, which you need every time you sign a certificate. Copy the ca.crt file to the server and to every client.

  • ./easyrsa build-ca nopass

    Creates a certificate authority whose key has no password. Caution: anyone who gets the ca.key file can issue certificates the server will accept. Use this option only when the key is kept on a protected computer, preferably one separate from the VPN server.

  • ./easyrsa gen-req server nopass

    Creates a private key and a certificate signing request (CSR) named server for the server. The server key has no password, so the service starts automatically after a reboot.

  • ./easyrsa sign-req server server

    Signs the request as a server certificate. The server type adds the serverAuth usage to the certificate, which clients check with remote-cert-tls server. The certificate is saved as pki/issued/server.crt.

  • ./easyrsa build-client-full client1 nopass

    Creates a key and a signed certificate for the client client1 in one command. Create a separate certificate for each user or device. Without nopass, the key is password-protected, and the user types the password on every connection.

  • ./easyrsa gen-req client1 nopass ./easyrsa sign-req client client1

    The same client certificate in two steps: creating a request and signing it as a client certificate. Use this method when the request is created on another computer and imported into the certificate authority with ./easyrsa import-req.

  • ./easyrsa gen-dh

    Creates a Diffie-Hellman parameters file at pki/dh.pem. With the dh none setting, the server uses ECDH and the file isn't needed, and this is the default in recent versions of OpenVPN. Generating the file can take a few minutes.

  • sudo cp pki/ca.crt pki/issued/server.crt pki/private/server.key /etc/openvpn/server/

    Copies the certificate authority's certificate, the server certificate, and the server key to the server's configuration folder. The certificate authority's private key, ca.key, isn't needed on the VPN server.

tls-crypt key

  • sudo openvpn --genkey secret /etc/openvpn/server/ta.key

    Creates a shared key for use with tls-crypt or tls-auth. The same file is needed on the server and on every client, so protect it like a private key.

  • tls-crypt ta.key

    A setting for the server file and the client files: encrypts and authenticates all TLS control packets with the shared key. The server ignores packets that weren't signed with the key, so this setting protects against scans and denial-of-service attacks. It takes no direction parameter.

  • tls-auth ta.key 0

    The older method: adds an HMAC signature to the control packets without encrypting them. The direction is 0 on the server and 1 on the clients. New setups should prefer tls-crypt, and both sides must use the same method.

  • openvpn --genkey tls-crypt-v2-server v2-server.key openvpn --tls-crypt-v2 v2-server.key --genkey tls-crypt-v2-client client1-v2.key

    The tls-crypt-v2 method: each client gets its own key, wrapped with the server's key. On the server, set tls-crypt-v2 v2-server.key, and in each client file, that client's key. This way, a key leaked from one device doesn't expose the other clients.

Server configuration file

  • port 1194 proto udp dev tun ca ca.crt cert server.crt key server.key dh none tls-crypt ta.key crl-verify crl.pem topology subnet server 10.8.0.0 255.255.255.0 push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 1.1.1.1" keepalive 10 120 data-ciphers AES-256-GCM:AES-128-GCM:CHACHA20-POLY1305 user nobody group nogroup persist-key persist-tun verb 3 explicit-exit-notify 1

    A basic server file, saved as /etc/openvpn/server/server.conf. File paths are relative to this folder. The crl-verify line requires the crl.pem file, which you create with ./easyrsa gen-crl. On AlmaLinux and Rocky Linux, write group nobody instead of group nogroup.

  • port 1194

    The port the server listens on. 1194 is the official OpenVPN port, and you can choose any other port. When you change it, also update the remote line in the client files and the firewall.

  • proto udp

    The tunnel protocol. UDP is the default and is better suited to carrying traffic through a tunnel. Write proto tcp on networks that block UDP. The server and the clients must be set to the same protocol.

  • dev tun

    A tun interface carries IP packets in routed mode, the usual setting. A tap interface carries Ethernet frames and is used for bridging networks, for example when broadcast traffic is needed.

  • server 10.8.0.0 255.255.255.0

    Enables server mode and assigns clients addresses from the 10.8.0.0/24 range, with 10.8.0.1 for the server itself. Choose a private range that isn't used on the networks your clients connect from.

  • topology subnet

    All clients are on one network, and each client gets a single address from the range. This is the default in recent versions; in older versions, the default was net30, which assigns a small network to each client. Write this line explicitly so the setting works in every version.

  • push "redirect-gateway def1 bypass-dhcp"

    Full tunnel: sends all client traffic through the server. def1 adds the routes 0.0.0.0/1 and 128.0.0.0/1, which override the default route without deleting it, and bypass-dhcp keeps requests to the local DHCP server outside the tunnel. IP forwarding and NAT are also needed on the server.

  • push "route 192.168.10.0 255.255.255.0"

    Split tunnel: instead of redirect-gateway, sends clients a route to one specific network only, for example an internal network behind the server. The rest of the browsing goes out directly from the device.

  • push "dhcp-option DNS 1.1.1.1"

    Sends clients a DNS server to use while connected. The client apps for Windows and phones apply it automatically. On a Linux client, it's applied by a DNS update script: recent versions include a built-in script, and in older versions you set up a script such as update-resolv-conf.

  • keepalive 10 120

    Sends a ping packet every 10 seconds. In server mode, the setting is also pushed to clients: a client restarts the connection when no reply arrives for 120 seconds, and the server disconnects a client after twice that time, 240 seconds.

  • data-ciphers AES-256-GCM:AES-128-GCM:CHACHA20-POLY1305

    The list of encryption algorithms for tunnel traffic, from which the server and the client choose a common one. This is also the default list when the cryptographic library supports ChaCha20. The old cipher setting is no longer used to choose the algorithm.

  • data-ciphers-fallback AES-256-CBC

    The algorithm the server uses with very old clients that don't announce which algorithms they support. It's needed only when such clients connect, and updating the client is the better choice.

  • user nobody group nogroup

    After startup, the server keeps running with the permissions of an unprivileged user. On AlmaLinux and Rocky Linux, the group name is nobody. Files the server reads later, such as crl.pem, must be readable by this user. The official documentation recommends creating a dedicated user for OpenVPN, because the nobody user is also used by other services.

  • persist-key persist-tun

    Keep the keys in memory and the tun interface open during an internal restart of the connection. Combined with user nobody, they're required, because after dropping privileges the server can no longer read the keys or reopen the interface.

  • client-to-client

    Lets clients communicate with each other through the server. Without this setting, each client reaches only the server and the networks set up for it.

  • client-config-dir ccd

    A folder with a configuration file for each client, named after its certificate. For example, the line ifconfig-push 10.8.0.50 255.255.255.0 in the ccd/client1 file gives client1 a fixed address in the tunnel.

  • explicit-exit-notify 1

    Notifies clients when the server stops or restarts, so they reconnect right away. The setting works with UDP only, so remove it on a server set to proto tcp.

Routing and NAT on the server

  • echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/70-openvpn-forward.conf sudo sysctl --system

    Enables packet forwarding between interfaces (IP forwarding) permanently, and applies the change right away. Without this setting, clients connect to the server, but they can't reach the internet through it.

  • ip route show default

    Shows the network interface that traffic to the internet goes out through, for example eth0 or ens18. Use this name in the NAT rules below.

  • sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

    Applies NAT (MASQUERADE) to client traffic going out to the internet, so it leaves with the server's address. A rule added with this command is deleted when the server reboots, so save it in the tool that manages your firewall.

  • sudo iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT sudo iptables -A FORWARD -i eth0 -o tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

    Allow traffic to pass from the tunnel to the internet and the replies to come back, when the FORWARD chain blocks by default. The OpenVPN interface is usually named tun0.

  • sudo nft add table ip ovpn-nat sudo nft add chain ip ovpn-nat postrouting '{ type nat hook postrouting priority srcnat; }' sudo nft add rule ip ovpn-nat postrouting ip saddr 10.8.0.0/24 oifname "eth0" masquerade

    The same NAT with nftables, in a separate table. As with iptables, the rules are deleted when the server reboots, and when the forward chain blocks traffic by default, you also need to allow tun0 traffic in it.

Firewall

  • sudo ufw allow 1194/udp

    Opens the OpenVPN port in UFW. On a server set to proto tcp, open the port over TCP.

  • sudo ufw route allow in on tun0 out on eth0

    Allows UFW to forward traffic from the tunnel to the outgoing interface, when clients browse the internet through the server.

  • *nat :POSTROUTING ACCEPT [0:0] -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE COMMIT

    NAT on the server with UFW: add these lines at the top of the /etc/ufw/before.rules file, before the *filter line, and run sudo ufw reload. Replace eth0 with the name of the outgoing interface.

  • sudo firewall-cmd --permanent --add-service=openvpn

    Opens the openvpn service in firewalld, which includes port 1194 over UDP. For another port or protocol, use the add-port option, for example --add-port=443/tcp.

  • sudo firewall-cmd --permanent --zone=internal --add-interface=tun0

    Adds the tun0 interface to the internal zone, so the rules for VPN clients are separate from the rules of the public zone.

  • sudo firewall-cmd --permanent --zone=public --add-masquerade sudo firewall-cmd --reload

    Enables NAT (Masquerade) for traffic going out through the public zone, so clients reach the internet with the server's address, and loads the permanent settings.

Running as a service

  • sudo systemctl enable --now openvpn-server@server

    Starts the server as a systemd service, now and on every boot, using the /etc/openvpn/server/server.conf file. The part after @ is the configuration file name without the conf extension.

  • sudo systemctl enable --now openvpn@server

    Ubuntu and Debian also have the openvpn@ unit, which reads the /etc/openvpn/server.conf file. Unit and folder names vary between distributions, and the systemctl list-unit-files 'openvpn*' command lists the available units.

  • sudo systemctl status openvpn-server@server

    Shows whether the service is running, along with its latest messages, including errors in the configuration file.

  • sudo systemctl restart openvpn-server@server

    Restarts the service after a change to the configuration file. Connected clients are disconnected and reconnect automatically.

  • sudo ss -ulnp | grep openvpn

    Checks that the server is listening, and on which port. On a server set to TCP, run ss -tlnp.

Client ovpn file

  • client dev tun proto udp remote vpn.example.com 1194 resolv-retry infinite nobind persist-key persist-tun remote-cert-tls server verb 3 <ca> CA_CERTIFICATE </ca> <cert> CLIENT1_CERTIFICATE </cert> <key> CLIENT1_PRIVATE_KEY </key> <tls-crypt> TA_KEY </tls-crypt>

    A single client file with the certificates and keys inside it (inline). Between the tags, paste the contents of ca.crt, the client's certificate and key, and ta.key. Save the file with a name like client1.ovpn and send it to the user securely, because it contains a private key.

  • remote vpn.example.com 1194

    The server's address and port. You can add several remote lines for backup servers, and the client moves on to the next line when the connection fails.

  • remote-cert-tls server

    Checks that the server's certificate was issued as a server certificate. This setting prevents another client, with a client certificate from the same certificate authority, from impersonating the server.

  • nobind

    The client doesn't bind to a fixed local port, and the operating system picks a free one. This setting suits almost every client.

  • { cat client-base.conf echo "<ca>"; cat pki/ca.crt; echo "</ca>" echo "<cert>"; openssl x509 -in pki/issued/client1.crt; echo "</cert>" echo "<key>"; cat pki/private/client1.key; echo "</key>" echo "<tls-crypt>"; sudo cat /etc/openvpn/server/ta.key; echo "</tls-crypt>"; } > client1.ovpn

    Builds client1.ovpn from a base file with the client settings and from the client's certificates. The openssl x509 command copies only the certificate block, without the details before it. Run it in the easy-rsa folder.

  • ./easyrsa inline client1

    In recent versions of easy-rsa: creates a file with the client's certificates and key in inline format, in the pki/inline/private folder, because the file includes the private key. Add its contents to the client's base file.

  • sudo openvpn --config client1.ovpn

    Connects to the server from a Linux computer and shows the log in the terminal. The message Initialization Sequence Completed means the connection succeeded, and Ctrl+C disconnects it.

  • sudo cp client1.ovpn /etc/openvpn/client/client1.conf sudo systemctl enable --now openvpn-client@client1

    Runs the connection as a service on a Linux computer, so it also comes up after a reboot. In this folder, the file extension must be conf.

  • auth-user-pass

    Adds username and password authentication to the connection, on top of the certificate. The server needs a plugin or script that checks the credentials, and when the check fails, the client gets the AUTH_FAILED message.

Certificate revocation and renewal

  • ./easyrsa revoke-issued client1

    Revokes the client1 certificate, for example when a device is lost or an employee leaves. In older versions of easy-rsa, the command is named revoke. The revocation takes effect only after you create a new revocation list.

  • ./easyrsa gen-crl

    Creates a certificate revocation list (CRL) at pki/crl.pem. By default, the list is valid for 180 days, and when it expires, the server rejects connections. So create a new list and copy it to the server before it expires, even when no certificate was revoked.

  • sudo cp pki/crl.pem /etc/openvpn/server/crl.pem sudo chmod 644 /etc/openvpn/server/crl.pem

    Copies the list to the server and makes it readable by all users, because the server reads it after dropping privileges. The server reads the file on every new connection, so there's no need to restart it.

  • crl-verify crl.pem

    The server file setting that checks every client certificate against the revocation list. A client whose certificate was revoked is rejected already at the TLS stage.

  • ./easyrsa show-expire

    Lists the certificates that expire within the next 90 days. By default, an easy-rsa certificate is valid for 825 days, and the certificate authority's certificate for 3650 days.

  • ./easyrsa renew client1

    Renews a certificate that's about to expire. The behavior of renew has changed between easy-rsa versions, so check first with ./easyrsa help renew. A simple alternative is to create a new certificate for the client under another name and revoke the old one.

Logs and connection status

  • sudo journalctl -u openvpn-server@server -f

    Shows the service log in real time, including client connections and errors.

  • sudo cat /run/openvpn-server/status-server.log

    Shows the status file written by the openvpn-server@ service: the connected clients, the address they connected from, the address they got in the tunnel, and the traffic volume. The file is updated every minute.

  • status openvpn-status.log 30

    A server file setting for when the server runs under another unit: writes the status file every 30 seconds. With the openvpn-server@ unit, this line isn't needed, because the status file is set in the unit itself.

  • verb 3

    The log verbosity level: 0 shows only fatal errors, 1 to 4 suit normal use, and 5 and 6 help diagnose connection problems. Higher levels are meant for in-depth debugging and produce a very large log.

  • log-append /var/log/openvpn/server.log

    Writes the log to a file instead of the system log. log-append appends to an existing file, and log overwrites it on every start. The folder must already exist.

Testing and troubleshooting

  • sudo tcpdump -ni eth0 udp port 1194

    Shows on the server whether packets from the client reach the port. When no packet arrives, the port is blocked along the way, in the server's firewall or on the client's network, or the remote line in the client file is wrong.

  • sudo openvpn --cd /etc/openvpn/server --config server.conf --verb 4

    Runs the server in the terminal, outside the service, and immediately shows errors such as Options error. Stop the service before you run it, so the port is free.

  • openvpn --show-ciphers

    Lists the encryption algorithms the installed version supports, so you can check that data-ciphers matches between the server and the client.

  • ping -c 3 10.8.0.1

    Checks from the client that the tunnel works, by pinging the server's address inside the tunnel.

  • ip route get 1.1.1.1

    Shows which interface traffic to a given address goes out through. In a full tunnel, the answer on the client should be the tunnel interface, for example tun0.

  • cat /proc/sys/net/ipv4/ip_forward

    Shows 1 when packet forwarding is enabled on the server. A value of 0 explains a client that connects but can't browse the internet through the tunnel.

  • sudo iptables -t nat -L POSTROUTING -n -v

    Shows the NAT rules and their packet counters. A counter that stays at 0 while the client browses points to a rule with a wrong interface name or address range.

  • resolvectl status tun0

    Shows the DNS server of the tunnel interface on a Linux client with systemd-resolved. When no server appears, the DNS setting pushed by the server wasn't applied, and queries are sent outside the tunnel.

  • mssfix 1360

    Reduces the size of TCP packets going through the tunnel. Use it when websites load partially or connections hang, a common sign of an MTU problem along the way, and add it to the server file and the client files.

What OpenVPN is and how it works

OpenVPN is VPN software that creates an encrypted tunnel between computers, phones, and servers. Authentication and key exchange are based on TLS and certificates: the server and every client have a certificate signed by a shared certificate authority (CA), and the server accepts only clients with a valid certificate. The traffic goes through a single port, over UDP by default, and you can also set it to TCP.

The official OpenVPN port is 1194, but you can choose any other port. The Port 1194 page has details about the port and how to protect it.

OpenVPN and WireGuard are two common VPN solutions on Linux servers. WireGuard is based on key pairs and a short configuration file, and works over UDP only. OpenVPN is based on certificates and TLS, also works over TCP, and gives you broad control over the settings, such as certificate revocation, username and password authentication, and pushing routes and DNS settings to clients. The commands for setting up a WireGuard server are on the WireGuard commands page.

When the connection doesn't work

  • TLS key negotiation failed or TLS handshake failed: the packets don't reach the server, or the replies don't come back. Check that the port and protocol are the same on the server and the client, that the port is open in the firewall, and that the service is running. When the server log shows the error tls-crypt unwrap error, the ta.key file on the client doesn't match the file on the server, or one side is set to tls-crypt and the other to tls-auth.
  • Certificate verification error (VERIFY ERROR): the certificate was signed by a different certificate authority, it expired or was revoked, or the clock on one of the sides is wrong. The server also rejects clients when the certificate revocation list (CRL) has expired.
  • AUTH_FAILED: with auth-user-pass, the server rejected the username or password, or the plugin or script that checks authentication rejected the client. The exact reason appears in the server log.
  • OPTIONS ERROR: failed to negotiate cipher: the data-ciphers lists on the two sides have no encryption algorithm in common, usually with an old client. Updating the client is the better choice; alternatively, add the client's algorithm to data-ciphers or data-ciphers-fallback on the server.
  • Options error: the configuration file has an unknown line, a line with a missing or extra parameter, or a setting that was removed in the installed version. The message names the file and the line number.
  • The connection comes up, but there's no browsing: check that packet forwarding is enabled on the server, that the NAT rule has the right interface name and range, and that the server pushes redirect-gateway to the clients.
  • DNS leak: in a full tunnel, the server pushes a DNS server to the clients, and on a Linux client, check that the setting was applied to the tunnel interface. Otherwise, queries go out to the DNS server of the local network.
  • Websites load partially: this is a common sign of an MTU problem. Add mssfix with a lower value on the server and the clients, and test again.

Keeping the server secure

  • The certificate authority key: keep ca.key off the VPN server and protect it with a password, because whoever holds it can issue certificates.
  • A certificate for each device: this way, you can revoke access for a lost device or an employee who left, without affecting the other clients.
  • An up-to-date revocation list: set crl-verify, and create a new list before the current one expires.
  • tls-crypt: the server ignores packets that weren't signed with the shared key, which reduces its exposure to scans and attacks.
  • No compression: the compress and comp-lzo settings are deprecated, because compressing encrypted traffic exposes it to an attack called VORACLE. Don't add them to the server and client files.

The address vpn.example.com on this page is reserved for documentation, so replace it with your server's address. The 10.8.0.0/24 range is an example private range, and notation such as /24 is explained in the CIDR table. More commands for certificates and keys are on the OpenSSL commands page, and certificate formats are explained on the SSL certificate types page. More firewall commands are on the UFW commands, firewalld commands, and iptables and nftables commands pages.

Did you know?

On a Linux VPS, you connect over SSH with full root access, and set up an OpenVPN server with all the commands on this page: a certificate authority and certificates, the server.conf file, routing and NAT, the firewall, and client files for computers and phones.

Linux VPS plans

OpenVPN FAQ

What is OpenVPN, and why is it so popular?

OpenVPN is open-source VPN software that creates an encrypted tunnel between computers, phones, and servers. Authentication is based on TLS and certificates, and the traffic goes through a single port, over UDP or TCP. It has apps for every operating system and is supported by many routers, so it suits remote access for employees and connecting offices and servers.

How do I connect a Windows computer, a Mac, or a phone to an OpenVPN server?

Create a certificate for the client on the server, and build from it a single ovpn file that includes the settings, certificates, and keys. On the computer or phone, install an OpenVPN app, such as OpenVPN Connect, or OpenVPN GUI for Windows, import the file, and connect with one click. On a Linux computer, connect with the sudo openvpn --config client1.ovpn command.

Can I run OpenVPN over TCP or on port 443?

Yes. Set proto tcp and the port you want in the server file, and update the proto and remote lines in the client files. Running over TCP on port 443 lets you connect even from networks that allow only regular web browsing. When a website also runs on port 443 on the server, choose another port or use the port-share option, which passes non-VPN connections on to the web server.

How do I add a new OpenVPN user without disconnecting connected users?

Create a new certificate for the user with ./easyrsa build-client-full and the username, and build an ovpn file for them. The server accepts any certificate signed by its certificate authority, so there's no need to change the server file or restart it, and connected users keep working as usual.

What's the difference between OpenVPN and WireGuard?

Both create an encrypted tunnel and are suited to setting up on a Linux server. WireGuard is based on key pairs and a short configuration file, and works over UDP only. OpenVPN is based on certificates and TLS, also works over TCP, and gives you broad control over the settings, such as certificate revocation, username and password authentication, and pushing routes to clients. You can also run both on the same server, on different ports. The WireGuard commands are on the WireGuard commands page.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement