AnyConnect-Compatible VPN Server (ocserv) with Certificate Login on AlmaLinux 9
Set up the OpenConnect VPN server (ocserv) on AlmaLinux 9 or Rocky Linux 9 with client-certificate authentication for AnyConnect and OpenConnect clients.
This guide sets up ocserv, the OpenConnect VPN server, on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9. It speaks the protocol used by Cisco AnyConnect (now sold as Cisco Secure Client), so those clients and the open-source OpenConnect client can both connect. Users log in with a client certificate issued by your own certificate authority instead of a password: you import one .p12 file on each device and that is the whole client setup.
CentOS 7 reached end of life in June 2024 and no longer receives updates. The RHEL 9 family replaces it, and ocserv is available there from the EPEL repository.
Requirements
- A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, with a user that has
sudorights. - A DNS name pointing at the server, for example
vpn.example.com, so you can use a publicly trusted server certificate. - TCP and UDP port 443 free on the server (no web server listening on 443), and TCP port 80 reachable while the server certificate is issued.
- Ideally a second, trusted machine to hold the CA key. You can create the CA on the server, but whoever obtains that key can issue valid VPN logins.
Install ocserv
Enable EPEL and install the server together with certtool (from gnutls-utils) and Certbot:
sudo dnf install epel-release
sudo dnf install ocserv gnutls-utils certbot
Get a server certificate
The server certificate proves the server's identity to clients. With a domain name, Let's Encrypt is the simplest source. Open port 80 for the validation and request the certificate with Certbot's standalone mode:
sudo firewall-cmd --add-service=http
sudo certbot certonly --standalone -d vpn.example.com
The certificate and key are stored in /etc/letsencrypt/live/vpn.example.com/ as fullchain.pem and privkey.pem. The firewall rule above is not permanent and disappears at the next firewall reload; add it with --permanent if you want unattended renewals over port 80 to keep working.
Without a domain name you can sign a server certificate with your own CA instead (see the FAQ), but every client must then be told to trust that CA.
Create the client certificate authority
Run these commands on the machine that will keep the CA key. They use certtool, as in the ocserv manual. Work in a directory only you can read:
mkdir -m 700 ~/vpn-ca
cd ~/vpn-ca
Generate the CA key and a self-signed CA certificate:
certtool --generate-privkey --outfile ca-key.pem
cat > ca.tmpl <<EOF
cn = "VPN CA"
organization = "Example"
serial = 1
expiration_days = 3650
ca
signing_key
cert_signing_key
crl_signing_key
EOF
certtool --generate-self-signed --load-privkey ca-key.pem --template ca.tmpl --outfile ca-cert.pem
Keep ca-key.pem private and backed up. Only ca-cert.pem goes to the VPN server.
Issue a client certificate
Each user or device gets its own key and certificate. The cn value becomes the user name that ocserv logs and that you use to manage the session.
certtool --generate-privkey --outfile deploy-key.pem
cat > deploy.tmpl <<EOF
cn = "deploy"
unit = "vpnusers"
expiration_days = 365
signing_key
tls_www_client
EOF
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
Bundle key and certificate into a PKCS#12 file for import on phones and desktops. You are asked for a key name and an export password; the password is requested once when the file is imported.
certtool --to-p12 --load-privkey deploy-key.pem --pkcs-cipher 3des-pkcs12 --load-certificate deploy-cert.pem --outfile deploy.p12 --outder
Transfer deploy.p12 to the device over a secure channel and delete it afterwards.
Configure ocserv
Copy the CA certificate to the server if you created it elsewhere, and place it where ocserv can read it:
sudo install -m 644 ca-cert.pem /etc/ocserv/ca-cert.pem
Open /etc/ocserv/ocserv.conf in an editor. Find each of the following options, set it as shown, and comment out every other auth line so that certificates are the only login method:
auth = "certificate"
tcp-port = 443
udp-port = 443
server-cert = /etc/letsencrypt/live/vpn.example.com/fullchain.pem
server-key = /etc/letsencrypt/live/vpn.example.com/privkey.pem
ca-cert = /etc/ocserv/ca-cert.pem
# read the user name from the certificate's CN field
cert-user-oid = 2.5.4.3
ipv4-network = 10.10.10.0
ipv4-netmask = 255.255.255.0
dns = 9.9.9.9
dns = 1.1.1.1
route = default
cisco-client-compat = true
Notes on these settings:
ca-certis the authority that client certificates are checked against. Any certificate it signed, and that is not revoked, can log in.ipv4-networkis the address pool for clients. Pick a private range that does not collide with the networks your users connect from.route = defaultsends all client traffic through the VPN. Make sure no otherroutelines remain active, or list specific networks instead for a split tunnel.cisco-client-compat = trueis required for Cisco clients and for OpenConnect older than 7.08.- If your server has a routed IPv6 prefix, you can also hand out IPv6 addresses with
ipv6-networkandipv6-subnet-prefix.
Open the firewall and enable routing
Allow the VPN ports, enable forwarding and masquerading in firewalld, and reload:
sudo firewall-cmd --permanent --add-port=443/tcp --add-port=443/udp
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --permanent --add-forward
sudo firewall-cmd --reload
--add-masquerade covers IPv4 only; IPv6 masquerading needs a rich rule, and is unnecessary when you hand out routed IPv6 addresses. Then make sure the kernel forwards packets:
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/60-ocserv.conf
sudo sysctl -p /etc/sysctl.d/60-ocserv.conf
Start the server and connect
sudo systemctl enable --now ocserv
sudo systemctl status ocserv
On a Cisco client, import deploy.p12, add vpn.example.com as the server and connect. With the OpenConnect command-line client on Linux:
sudo openconnect --certificate deploy.p12 https://vpn.example.com
On the server, list the connected sessions and follow the log:
sudo occtl show users
sudo journalctl -fu ocserv
Revoke a client certificate
A certificate stays valid until it expires unless you publish a certificate revocation list (CRL). On the CA machine, append the certificate to a list of revoked ones and generate the CRL:
cat > crl.tmpl <<EOF
crl_next_update = 365
crl_number = 1
EOF
cat deploy-cert.pem >> revoked.pem
certtool --generate-crl --load-ca-privkey ca-key.pem --load-ca-certificate ca-cert.pem --load-certificate revoked.pem --template crl.tmpl --outfile crl.pem
Copy crl.pem to /etc/ocserv/crl.pem on the server and, the first time, add this line to ocserv.conf and restart the service:
crl = /etc/ocserv/crl.pem
sudo systemctl restart ocserv
Afterwards ocserv picks up a changed CRL file by itself. Increase crl_number each time you generate a new list, and regenerate it before the crl_next_update period (in days) runs out.
Troubleshooting
- "The certificate issuer is unknown" in the log:
ca-certdoes not point at the CA that signed the client certificate. - The service fails to start: another program already listens on port 443, or a certificate path is wrong.
journalctl -u ocserv -enames the cause. - Connected, but no traffic passes: check
sudo firewall-cmd --list-allformasquerade: yesand confirmsysctl net.ipv4.ip_forwardprints1. - Connection is slow: UDP 443 is probably blocked, so the client falls back from DTLS to TCP. Open the UDP port in your provider's firewall too.
Frequently asked questions
Can I use a self-signed server certificate instead of Let's Encrypt?
Yes. Generate a server key, write a template with dns_name or ip_address and the tls_www_server flag, and sign it with your CA using certtool --generate-certificate, as the ocserv manual shows. Point server-cert and server-key at the result. Clients will warn about an untrusted server until you import ca-cert.pem on them.
Can I require a password as well as a certificate?
Yes. Keep a password method such as auth = "plain[passwd=/etc/ocserv/ocpasswd]" active and still set ca-cert and cert-user-oid; ocserv then checks both, and the user name in the certificate must match the login name. The ocserv manual describes the supported combinations.
Is this the same software as Cisco's VPN?
No. ocserv is an independent open-source server that is compatible with the AnyConnect protocol. Cisco's client connects to it, but Cisco-only features that depend on their own appliances are not part of it.
What happens when the Let's Encrypt certificate renews?
Certbot renews it automatically if port 80 is reachable at that time. The ocserv manual states that the server certificate and key are reloaded when the files change; if clients still see the old certificate, run sudo systemctl restart ocserv.