Quick Start: Set Up an ocserv SSL VPN in 5 Minutes
Need to quickly set up a secure VPN gateway that smoothly bypasses corporate firewalls and works seamlessly across devices—from iPhones and Android phones to Windows and macOS laptops? OpenConnect Server (ocserv) is the ideal solution. It implements Cisco’s AnyConnect protocol, allowing users to connect directly using official client apps without complex profile setups.
Here is a quick setup guide for Ubuntu 22.04 / 24.04 using a self-signed certificate:
# 1. Install ocserv and certificate generation tools
sudo apt update && sudo apt install -y ocserv gnutls-bin iptables
# 2. Create directory and generate CA certificate
mkdir -p /etc/ocserv/certs && cd /etc/ocserv/certs
certtool --generate-privkey --outfile ca-key.pem
cat << 'EOF' > ca.tmpl
cn = "My VPN CA"
organization = "IT From Zero"
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
# 3. Generate Server Certificate (replace 203.0.113.10 with your VPS's public IP)
certtool --generate-privkey --outfile server-key.pem
cat << 'EOF' > server.tmpl
cn = "203.0.113.10"
ip_address = "203.0.113.10"
organization = "IT From Zero"
expiration_days = 3650
signing_key
encryption_key
tls_www_server
EOF
certtool --generate-certificate --load-privkey server-key.pem --load-ca-certificate ca-cert.pem --load-ca-privkey ca-key.pem --template server.tmpl --outfile server-cert.pem
Open the main configuration file /etc/ocserv/ocserv.conf and update the essential parameters:
# Paths to the generated certificates
server-cert = /etc/ocserv/certs/server-cert.pem
server-key = /etc/ocserv/certs/server-key.pem
# Authenticate users via the local ocpasswd file
auth = "plain[passwd=/etc/ocserv/ocpasswd]"
# IP address pool assigned to clients via the tun interface
ipv4-network = 192.168.100.0/24
# DNS servers assigned to client devices
dns = 1.1.1.1
dns = 8.8.8.8
Create the first VPN user account and enable NAT routing:
# Create user vpnuser and set a password
sudo ocpasswd -c /etc/ocserv/ocpasswd vpnuser
# Enable IP forwarding in the Linux kernel
sudo sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
# Configure iptables NAT (replace eth0 with your primary network interface: run 'ip route get 1.1.1.1' to check)
sudo iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE
# Enable and start the service
sudo systemctl restart ocserv
sudo systemctl enable ocserv
Installation is complete. Open Cisco Secure Client (AnyConnect) or OpenConnect on your client device, enter your VPS’s IP address, and log in with the vpnuser credentials to start connecting.
Under the Hood: How Does ocserv Work?
1. Dual-Channel Architecture: Parallel TCP (TLS) and UDP (DTLS)
The biggest advantage of ocserv over traditional OpenVPN lies in its dual-channel connection architecture:
- TCP Port 443 (TLS): Handles the authentication handshake, encryption key exchange, and routing synchronization. Traffic mimics standard HTTPS web browsing, making it virtually impossible for coffee shop or hotel firewalls to detect and block.
- UDP Port 443 (DTLS): Handles all payload data transmission as soon as the handshake completes. Switching to UDP eliminates the TCP-over-TCP meltdown problem. Latency drops significantly, keeping ping times stable under 35ms and ensuring crystal-clear audio/video calls on Google Meet or Zoom.
2. Subnet Planning and Preventing IP Overlaps
The directive ipv4-network = 192.168.100.0/24 provides up to 253 usable IP addresses for concurrently connected clients. Before assigning this subnet, carefully review your existing routing tables. If your office or home LAN also uses the 192.168.100.x range, clients will immediately run into routing conflicts.
To subnet safely and accurately calculate usable host ranges or broadcast addresses, consider using a Subnet Calculator. This tool quickly calculates subnets from CIDR notation, making it easy to isolate your VPN address pool (such as 172.16.50.0/24) from your corporate LAN ranges.
Advanced Production Configurations
Replacing with Let’s Encrypt Certificates (Eliminate AnyConnect Security Warnings)
By default, the Cisco AnyConnect client rejects self-signed certificates unless users manually disable security checks in Settings. For a professional deployment, point a domain (e.g., vpn.company.com) to your server’s IP and obtain a free TLS certificate via Certbot:
# Install Certbot
sudo apt install -y certbot
# Temporarily stop ocserv so Certbot can bind to ports 80/443 for HTTP-01 challenge
sudo systemctl stop ocserv
sudo certbot certonly --standalone -d vpn.company.com
# Update certificate paths in /etc/ocserv/ocserv.conf:
server-cert = /etc/letsencrypt/live/vpn.company.com/fullchain.pem
server-key = /etc/letsencrypt/live/vpn.company.com/privkey.pem
sudo systemctl start ocserv
Configuring Split Tunneling: Conserve Bandwidth and Boost Speed
A full tunnel forces all client traffic—from 4K YouTube streaming to large downloads—through the VPS, rapidly exhausting server bandwidth. Instead, use Split Tunneling: route only traffic destined for internal subnets through the VPN while keeping regular internet traffic on the user’s local connection.
Locate and configure the route directives in /etc/ocserv/ocserv.conf:
# Route only traffic for these two internal subnets through the VPN tunnel
route = 10.10.0.0/255.255.0.0
route = 172.16.0.0/255.240.0.0
# Comment out any 'route = default' lines to avoid overriding the client's default gateway
Managing Session Load and Concurrent Devices
Prevent users from sharing a single account across multiple devices or leaving idle connections open that consume server RAM:
# Limit the server to a maximum of 64 concurrent clients
max-clients = 64
# Allow a maximum of 2 concurrent devices per user account (e.g., laptop + phone)
max-same-clients = 2
# Clean up stale sessions after unexpected disconnections
tcp-keepalive-time = 300
mobile-dpd-idle = 60
Practical Troubleshooting Tips for Production
- Fixing Packet Drops When UFW Firewall Is Enabled: On Ubuntu, enabling UFW drops forwarded packets by default (FORWARD drop). First, open
/etc/default/ufwand setDEFAULT_FORWARD_POLICY="ACCEPT". Next, open/etc/ufw/before.rulesand insert the following NAT configuration near the top of the file (before the*filterline):*nat :POSTROUTING ACCEPT [0:0] -A POSTROUTING -s 192.168.100.0/24 -o eth0 -j MASQUERADE COMMITSave the file and reload the firewall rules with
sudo ufw reload. - Handling Unstable Connections on 4G/5G Mobile Networks: Many mobile carriers aggressively filter UDP packets or cause DTLS packet fragmentation. If clients frequently drop connections and reconnect, enable automatic MTU discovery and lower the MTU size to 1380 bytes in
ocserv.conf:try-mtu-discovery = true mtu = 1380 - Monitoring and Terminating Active Sessions with occtl: The built-in CLI utility allows sysadmins to manage the server without installing third-party tools:
# View currently connected users along with their IP addresses and connection times sudo occtl show users # Check bandwidth usage and daemon status sudo occtl show status # Disconnect a suspicious user session sudo occtl disconnect user vpnuser - Persisting iptables Routing Rules Across Reboots: Don’t forget to persist your MASQUERADE rule so clients don’t lose internet connectivity after a VPS reboot:
sudo apt install -y iptables-persistent sudo netfilter-persistent save
Configuring ocserv requires a solid understanding of TLS/DTLS traffic separation and packet routing. Once properly tuned, you get an SSL VPN infrastructure that is both robustly secure and capable of delivering performance rivaling dedicated enterprise hardware appliances.
