Log inSign up

Build an AnyConnect-compatible VPN with ocserv

Install ocserv from the distribution (EPEL on Rocky and Alma), get a certificate for vpn.example.net with certbot, and point server-cert and server-key at it in /etc/ocserv/ocserv.conf. Choose password login through ocpasswd, client certificates from your own small CA, or both; set the tunnel ranges, route = default and DNS; then add NAT and open TCP and UDP 443. Clients connect with OpenConnect or any AnyConnect-compatible app.

Topic: VPN and private DNS · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why pick ocserv?

ocserv speaks the AnyConnect protocol over HTTPS, so the VPN looks like ordinary TLS on port 443 and works where UDP ports are filtered. It also uses DTLS over UDP 443 when allowed, which gives better throughput, and falls back to TCP otherwise. Many corporate laptops already carry a compatible client, and OpenConnect is packaged for every Linux desktop. If your networks allow arbitrary UDP, a WireGuard server is simpler; ocserv shines on restrictive networks and in setups that need certificate-based login.

How do you install ocserv and certbot?

All required packages are in the standard repositories, plus EPEL on the Red Hat family. Ubuntu and Debian:

sudo apt update
sudo apt install -y ocserv gnutls-bin certbot

Rocky Linux 9 and AlmaLinux 9:

sudo dnf install -y epel-release
sudo dnf install -y ocserv gnutls-utils certbot

Check the version and stop the service until it is configured:

ocserv --version | head -1
sudo systemctl stop ocserv

How do you get a trusted server certificate?

A public certificate lets clients connect without warnings. Create A and AAAA records for vpn.example.net (see the DNS records tutorial), open port 80 briefly, and request it in standalone mode:

sudo ufw allow 80/tcp          # Rocky/Alma: sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload
sudo certbot certonly --standalone -d vpn.example.net --deploy-hook "systemctl restart ocserv"

The deploy hook restarts ocserv after every renewal so the new certificate is used. Keep port 80 open, because standalone renewal needs it again every couple of months. The certbot guide covers other validation methods.

How do you set up password and certificate login?

Password login is the quickest start; client certificates are stronger and can be combined with it. Create a password user first:

sudo ocpasswd -c /etc/ocserv/ocpasswd deploy

For certificate login, build a small CA with certtool in a private folder:

mkdir -m 700 ~/ocserv-ca && cd ~/ocserv-ca
cat > ca.tmpl <<'EOF'
cn = "Example VPN CA"
organization = "Example"
serial = 1
expiration_days = 3650
ca
signing_key
cert_signing_key
crl_signing_key
EOF
certtool --generate-privkey --outfile ca-key.pem
certtool --generate-self-signed --load-privkey ca-key.pem --template ca.tmpl --outfile ca-cert.pem
sudo cp ca-cert.pem /etc/ocserv/ca-cert.pem

Then issue one certificate per person. The common name becomes the VPN username:

cat > deploy.tmpl <<'EOF'
cn = "deploy"
expiration_days = 365
signing_key
tls_www_client
EOF
certtool --generate-privkey --outfile deploy-key.pem
certtool --generate-certificate --load-privkey deploy-key.pem --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem --template deploy.tmpl --outfile deploy-cert.pem
certtool --to-p12 --load-privkey deploy-key.pem --load-certificate deploy-cert.pem --pkcs-cipher 3des-pkcs12 --outder --outfile deploy.p12

certtool asks for an export password for the .p12 bundle; phones and Windows clients import that file. The legacy 3des-pkcs12 cipher keeps it readable by older mobile apps.

Which lines of ocserv.conf matter?

Edit /etc/ocserv/ocserv.conf and set these values, replacing the existing lines of the same name:

auth = "plain[passwd=/etc/ocserv/ocpasswd]"
enable-auth = "certificate"
tcp-port = 443
udp-port = 443
server-cert = /etc/letsencrypt/live/vpn.example.net/fullchain.pem
server-key = /etc/letsencrypt/live/vpn.example.net/privkey.pem
ca-cert = /etc/ocserv/ca-cert.pem
cert-user-oid = 2.5.4.3
isolate-workers = true
max-clients = 32
max-same-clients = 2
try-mtu-discovery = true
default-domain = vpn.example.net
ipv4-network = 10.10.10.0/24
ipv6-network = fd42:10:10::/48
ipv6-subnet-prefix = 64
tunnel-all-dns = true
dns = 9.9.9.9
dns = 2620:fe::fe
route = default

The packaged file ships sample route and no-route lines for private ranges; comment them out so only route = default remains, which sends all traffic through the tunnel:

sudo sed -i -E 's/^(route|no-route) = [0-9]/#&/' /etc/ocserv/ocserv.conf
grep -E '^(route|no-route)' /etc/ocserv/ocserv.conf

For password-only login, drop the enable-auth line. For certificate-only login, set auth = "certificate" instead.

How do you add forwarding, NAT and firewall rules?

Enable forwarding, then masquerade the tunnel ranges. The forwarding part is identical on all four systems:

printf 'net.ipv4.ip_forward = 1\nnet.ipv6.conf.all.forwarding = 1\nnet.ipv6.conf.eth0.accept_ra = 2\n' | sudo tee /etc/sysctl.d/99-ocserv.conf
sudo sysctl --system

On Ubuntu and Debian with ufw, place this block above *filter in /etc/ufw/before.rules (and the IPv6 version with -s fd42:10:10::/48 in before6.rules), using your public interface name:

*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE
COMMIT
sudo ufw allow 443/tcp
sudo ufw allow 443/udp
sudo ufw route allow out on eth0 from 10.10.10.0/24
sudo ufw route allow out on eth0 from fd42:10:10::/48
sudo ufw reload

On Rocky and Alma, firewalld does the translation:

sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-port=443/udp
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --reload

How do you start the server and connect a client?

Start the service, confirm it listens on both protocols, and watch sessions with occtl:

sudo systemctl enable --now ocserv
ss -ltnup | grep ':443'
sudo occtl show status
sudo occtl show users

On a Linux desktop, connect with OpenConnect using a password or the certificate bundle:

sudo openconnect vpn.example.net
sudo openconnect -c deploy.p12 vpn.example.net

On phones and Windows, install an AnyConnect-compatible app, add vpn.example.net as the server, and import deploy.p12 if you use certificates. A working session shows up in occtl show users with a 10.10.10.x address and the DTLS state.

How do you troubleshoot and maintain ocserv?

The journal explains almost every failed login. Follow it while a client connects:

sudo journalctl -u ocserv -f
  • Certificate errors on the client: server-cert must point to fullchain.pem, not cert.pem.
  • Connected but no browsing: check forwarding and the NAT block; for ufw, the two route rules must name the tunnel ranges and your public interface.
  • Only TCP, never DTLS: UDP 443 is closed somewhere on the path; it still works, just slower.
  • SELinux denials on Rocky or Alma: run sudo ausearch -m avc -ts recent to see what was blocked.

Disconnect a user with sudo occtl disconnect user deploy, and delete a password account with sudo ocpasswd -c /etc/ocserv/ocpasswd -d deploy. Updates arrive with the normal package upgrade; restart ocserv afterwards.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
F1-2quick core for TLS sessions1 vCPU, 2 GB RAM, 64 GB NVMe$7.20/mo$43.20 for 6 months
F2-4larger groups on DTLS2 vCPU, 4 GB RAM, 128 GB NVMe$14.40/mo$43.20 for 3 months

Questions about OpenConnect VPN server (ocserv)

Is ocserv compatible with Cisco AnyConnect clients?

Yes, ocserv implements the AnyConnect protocol, so the Cisco Secure Client and other AnyConnect-compatible apps connect to it, as does the open source OpenConnect client.

Can ocserv share port 443 with a website?

Not directly on the same address. Either run the website elsewhere, or use ocserv camouflage and a front proxy that routes by SNI. A dedicated VPN server keeps it simple.

How do I revoke a client certificate?

Create a CRL with certtool (--generate-crl with the revoked certificate listed), point crl = /etc/ocserv/crl.pem at it in ocserv.conf and restart the service.

Does ocserv support IPv6 inside the tunnel?

Yes. The ipv6-network and ipv6-subnet-prefix settings hand every client its own IPv6 range, and the server routes it out through its public IPv6 address.

Which plan suits ocserv?

TLS encryption favours a quick core; F1-2 with 1 vCPU, 2 GB RAM, 64 GB NVMe handles a small team comfortably.