L2TP over IPsec remains a widely supported VPN option for remote access due to its broad client compatibility and straightforward configuration. However, in environments where default VPN ports (UDP 500, UDP 4500 and UDP 1701) are blocked, or you need to run multiple VPN services behind the same public IP, configuring L2TP to operate with custom ports becomes necessary. This article gives a practical, security-aware, step‑by‑step guide for deploying L2TP/IPsec with custom external ports on a Linux gateway, including firewall/NAT handling and client considerations.
Design choices and security considerations
Before diving into commands and configuration files, decide on the following:
- Port strategy: whether to change daemon listening ports (rare and often complex) or to use port translation (DNAT) at the network edge. Port translation is simpler and more interoperable.
- IPsec mode: L2TP typically uses IKEv1 with a pre-shared key (PSK). For better security, prefer IKEv2 with certificates if client compatibility allows.
- Authentication: use strong PSKs or, even better, certificates. Enforce strong ciphers: AES-GCM, SHA-2 family and Diffie-Hellman groups >= 2048-bit (MODP2048 or better).
- Logging and monitoring: enable sufficient logging on IPsec and xl2tpd and monitor for repeated failures or unusual traffic patterns.
High-level approach
The recommended, robust approach is:
- Keep the VPN daemons (strongSwan/Libreswan and xl2tpd) listening on their standard internal ports.
- Use DNAT or UDP proxying to map one or more custom public ports to the internal standard ports (UDP 500, UDP 4500, UDP 1701).
- Adjust firewall rules (iptables/nftables/UFW/firewalld) to allow the chosen custom ports and to permit the translated traffic to the VPN services.
Server prerequisites (Linux)
Install these packages on a Debian/Ubuntu or RHEL/CentOS machine:
- IPsec implementation: strongSwan or Libreswan
- L2TP daemon: xl2tpd
- PPP support: ppp
- Packet manipulation: iptables or nftables (and optionally socat for UDP proxying)
Example (Debian/Ubuntu):
apt update && apt install -y strongswan xl2tpd ppp iptables-persistent socat
Core configuration overview
We will configure:
- /etc/ipsec.conf and /etc/ipsec.secrets (strongSwan)
- /etc/xl2tpd/xl2tpd.conf and /etc/ppp/options.xl2tpd
- iptables or nftables rules to map custom UDP ports to the standard VPN ports
Sample strongSwan configuration (IKEv1 PSK for compatibility)
File: /etc/ipsec.conf (minimal, adjust ciphers/policies as needed)
Note: This example shows a compatibility setup. Replace cryptographic choices with stronger profiles if you can use IKEv2/certificates.
config setup
charondebug=”knl 2, cfg 2″
conn L2TP-PSK
authby=secret
pfs=no
rekey=no
keyingtries=0
ike=aes128-sha1-modp2048
esp=aes128-sha1
keyexchange=ikev1
left=%any
leftid=@vpn.example.com
leftsubnet=0.0.0.0/0
leftfirewall=yes
right=%any
rightprotoport=17/1701
auto=add
File: /etc/ipsec.secrets
@vpn.example.com : PSK “your-very-strong-pre-shared-key”
xl2tpd and PPP configuration
/etc/xl2tpd/xl2tpd.conf (essential sections)
[global] port = 1701 [lns default] ip range = 10.10.10.10-10.10.10.50local ip = 10.10.10.1
require chap = yes
refuse pap = yes
require authentication = yes
name = L2TP-VPN
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
/etc/ppp/options.xl2tpd
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
noccp
mtu 1400
mru 1400
auth
name l2tpd
/etc/ppp/chap-secrets (user credentials):
vpnuser l2tpd “StrongPassword123” *
Mapping custom ports to standard ports (iptables DNAT)
Assume you want to expose these public ports:
- Custom UDP 5500 -> internal UDP 500 (ISAKMP)
- Custom UDP 45500 -> internal UDP 4500 (NAT-T)
- Custom UDP 11701 -> internal UDP 1701 (L2TP)
Use these iptables nat rules (replace eth0 with your public interface):
iptables -t nat -A PREROUTING -i eth0 -p udp –dport 5500 -j DNAT –to-destination :500
iptables -t nat -A PREROUTING -i eth0 -p udp –dport 45500 -j DNAT –to-destination :4500
iptables -t nat -A PREROUTING -i eth0 -p udp –dport 11701 -j DNAT –to-destination :1701
Allow forwarding and accept transformed packets:
iptables -A FORWARD -p udp –dport 500 -j ACCEPT
iptables -A FORWARD -p udp –dport 4500 -j ACCEPT
iptables -A FORWARD -p udp –dport 1701 -j ACCEPT
If the VPN services run on the same host, you may need to allow INPUT for those destination ports after DNAT:
iptables -t nat -A OUTPUT -p udp –dport 5500 -j DNAT –to-destination 127.0.0.1:500
For persistent rules, save iptables rules with iptables-save or use the distribution’s firewall persistence mechanism.
nftables example
For systems using nftables, add prerouting rules:
nft add table ip nat
nft add chain ip nat prerouting { type nat hook prerouting priority 0 ; }
nft add rule ip nat prerouting udp dport 5500 dnat to :500
nft add rule ip nat prerouting udp dport 45500 dnat to :4500
nft add rule ip nat prerouting udp dport 11701 dnat to :1701
Alternative: socat/UDP proxy for port translation
If your environment does not support kernel NAT for UDP, you can run a lightweight user‑space proxy with socat:
socat -v UDP4-RECVFROM:5500,fork UDP4-SENDTO:127.0.0.1:500 &
socat -v UDP4-RECVFROM:45500,fork UDP4-SENDTO:127.0.0.1:4500 &
socat -v UDP4-RECVFROM:11701,fork UDP4-SENDTO:127.0.0.1:1701 &
Run these under systemd to ensure they restart automatically. socat imposes some overhead but is simple and effective in constrained scenarios.
Firewall front-end examples (UFW, firewalld)
UFW (Ubuntu):
ufw allow 5500/udp
ufw allow 45500/udp
ufw allow 11701/udp
Then use iptables DNAT as above; UFW does not natively offer nat rules in its simple interface so combine UFW for allow rules and iptables for DNAT.
Firewalld (RHEL/CentOS): add rich rules to permit the ports and use direct rules for NAT if required.
Client considerations and configuring custom ports
Different clients handle custom ports differently:
- Windows built-in L2TP/IPsec: Windows expects the standard IKE and NAT‑T ports by default. It does not offer a native UI to change UDP ports for IKE. For external custom ports, either use port forwarding on the gateway (recommended) or use a third‑party client that allows custom ports (e.g., Shrew Soft VPN, or a strongSwan-based client).
- macOS: The built‑in client has similar limitations. Port translation at the server is the simplest approach for compatibility.
- Android/iOS: The strongSwan app (Android) and some third-party clients allow specifying custom ports in advanced settings. iOS third-party clients may also permit custom ports, but iOS’s built-in L2TP client usually assumes defaults.
- Linux: Using strongSwan or libreswan as the client, or NetworkManager plugins, you can often specify custom IKE ports in the configuration files or connection settings.
In practice, mapping public custom ports to the standard internal ports is the most compatible option across platforms.
Troubleshooting tips
- Check strongSwan logs (journalctl -u strongswan or /var/log/syslog) and xl2tpd logs for IKE and PPP negotiation errors.
- Use tcpdump: tcpdump -n -i eth0 udp port 500 or udp port 4500 or udp port 1701 to confirm packets reach the host and are DNATed correctly.
- Ensure kernel IP forwarding is enabled: sysctl -w net.ipv4.ip_forward=1 and persist in /etc/sysctl.conf.
- If sessions fail after establishing IKE, verify iptables forwarding/MASQUERADE rules allow traffic to/from VPN client subnets.
- If using a NAT or cloud provider security group, ensure the custom public ports are allowed there as well.
Improving security
Beyond port translation, harden your setup:
- Prefer certificates and IKEv2 if clients support it.
- Use strong cipher suites (AES-GCM, SHA-2 family) and DH groups >= 2048-bit.
- Limit allowed user accounts and use unique, strong passwords or per-user certificates.
- Monitor for brute-force attempts and consider rate-limiting or an IDS to detect suspicious traffic.
- Keep packages updated and regularly review logs for anomalous activity.
Example end-to-end checklist
- Install strongSwan, xl2tpd, ppp, socat (optional).
- Configure /etc/ipsec.conf and /etc/ipsec.secrets (or equivalent).
- Configure /etc/xl2tpd/xl2tpd.conf and /etc/ppp/options.xl2tpd, plus /etc/ppp/chap-secrets.
- Enable IP forwarding: sysctl net.ipv4.ip_forward=1.
- Add DNAT rules (iptables/nftables) mapping custom public UDP ports to 500/4500/1701.
- Open the custom public ports in your firewall and cloud security groups.
- Test with a compatible client; capture packets and inspect logs if negotiation fails.
- Harden ciphers and move to certificates/IKEv2 where feasible.
Setting up L2TP/IPsec with custom ports is best handled by leaving daemons on their default ports and performing port translation at the networking layer. This approach preserves client compatibility while allowing flexible port selection on the public interface. Follow best practices for encryption and authentication, and monitor the service actively.
For additional implementation templates, automated deployment scripts and dedicated IP guidance, visit Dedicated-IP-VPN at https://dedicated-ip-vpn.com/