User Tools

Site Tools


OpenVPN extras




Web interface

Install the necessary packages and provide instance management if you want to manage VPN instances using web interface.

# Install packages
opkg update
opkg install luci-app-openvpn

Navigate to LuCI → Services → OpenVPN to manage OpenVPN instances.

Instance management

If you need to manage multiple VPN instances or use web interface. Make sure to specify different VPN interface names for each instance.

# Provide VPN instance management
ls /etc/openvpn/*.conf \
| while read -r OVPN_CONF
OVPN_ID="$(basename ${OVPN_CONF%.*} | sed -e "s/\W/_/g")"
uci -q delete openvpn.${OVPN_ID}
uci set openvpn.${OVPN_ID}="openvpn"
uci set openvpn.${OVPN_ID}.enabled="1"
uci set openvpn.${OVPN_ID}.config="${OVPN_CONF}"
uci commit openvpn
/etc/init.d/openvpn restart

Commercial provider

If you use a commercial VPN provider. Set up credentials for username/password authentication and enforce gateway redirection.

# Configuration parameters
# Save username/password credentials
umask go=
cat << "EOF" > ${OVPN_DIR}/${OVPN_ID}.auth
# Configure VPN service
sed -i -e "
\$a auth-user-pass ${OVPN_ID}.auth
\$a redirect-gateway def1 ipv6
" ${OVPN_DIR}/${OVPN_ID}.conf
/etc/init.d/openvpn restart

Key management

Use EasyRSA to add clients or revoke their certificates via CRL.

# Configuration parameters
export EASYRSA_BATCH="1"
# Add one more client
easyrsa build-client-full client1 nopass
# Add another client encrypting its private key
easyrsa build-client-full client2
# Revoke client certificate
easyrsa revoke client
# Generate a CRL
easyrsa gen-crl
# Enable CRL verification
OVPN_CRL="$(cat ${OVPN_PKI}/crl.pem)"
sed -i -e "
\$a <crl-verify>\n${OVPN_CRL//$'\n'/\\n}\n</crl-verify>
" /etc/openvpn/server.conf
/etc/init.d/openvpn restart

Static addresses

Provide static IP address allocation on VPN server assuming that:

  • - VPN network
  • fdf1:e8a1:8d3f:8::/64 - VPN6 network
umask go=rx
mkdir -p /etc/openvpn/ccd
cat << EOF > /etc/openvpn/ccd/client
ifconfig-ipv6-push fdf1:e8a1:8d3f:8::2/64
cat << EOF >> /etc/openvpn/server.conf
client-config-dir ccd
/etc/init.d/openvpn restart


Implement plain routing between server side LAN and client side LAN assuming that:

  • - server side LAN
  • - client side LAN

Set up static address allocation on VPN server, add route to client side LAN, push route to server side LAN, selectively disable gateway redirection.

cat << EOF >> /etc/openvpn/ccd/client
push-remove redirect-gateway
cat << EOF >> /etc/openvpn/server.conf
push "route"
/etc/init.d/openvpn restart

Consider VPN network as private and assign VPN interface to LAN zone on VPN client.

uci del_list firewall.wan.device="tun+"
uci add_list firewall.lan.device="tun+"
uci commit firewall
/etc/init.d/firewall restart

Disable gateway redirection

If you do not need to push all traffic via VPN gateway. Disable gateway redirection on VPN server.

sed -i -e "
" /etc/openvpn/server.conf
/etc/init.d/openvpn restart

Or ignore it on VPN client.

sed -i -e "
\$a pull-filter ignore redirect-gateway
" /etc/openvpn/client.conf
/etc/init.d/openvpn restart

Split gateway

If VPN gateway is not your LAN gateway. Implement plain routing between LAN network and VPN network assuming that:

  • - LAN network
  • - VPN gateway
  • - VPN network

Add port forwarding for VPN server on LAN gateway.

uci -q delete firewall.ovpn
uci set firewall.ovpn="redirect"
uci set"Redirect-OpenVPN"
uci set firewall.ovpn.src="wan"
uci set firewall.ovpn.src_dport="1194"
uci set firewall.ovpn.dest="lan"
uci set firewall.ovpn.dest_ip=""
uci set"ipv4"
uci set firewall.ovpn.proto="udp"
uci set"DNAT"
uci commit firewall
/etc/init.d/firewall restart

Add route to VPN network via VPN gateway on LAN gateway.

uci -q delete network.vpn
uci set network.vpn="route"
uci set network.vpn.interface="lan"
uci set""
uci set network.vpn.gateway=""
uci commit network
/etc/init.d/network restart

IPv6 gateway

Provide IPv6 tunnel connectivity. Set up transitional connectivity or NAT6 with IPv6 masquerading if required.

Enable VPN6 network on VPN server, provide DNS6, redirect GW6. Provide default IPv6 route for VPN clients.

cat << EOF >> /etc/openvpn/server.conf
proto udp6
server-ipv6 ${OVPN_POOL6}
push "dhcp-option DNS ${OVPN_DNS6}"
push "redirect-gateway ipv6"
/etc/init.d/openvpn restart
uci set network.wan6.sourcefilter="0"
uci commit network
/etc/init.d/network restart


Use TCP for troubleshooting.

sed -i -e "
\$a proto ${OVPN_PROTO}
" /etc/openvpn/server.conf
/etc/init.d/openvpn restart
uci set firewall.ovpn.proto="${OVPN_PROTO}"
uci commit firewall
/etc/init.d/firewall restart


If you need to utilize bridging. Beware of compatibility issues.

OVPN_ADDR="$(uci get network.lan.ipaddr)"
OVPN_MASK="$(uci get network.lan.netmask)"
OVPN_POOL="${OVPN_ADDR%.*}.128 ${OVPN_ADDR%.*}.254"
NET_IF="$(uci get network.lan.ifname)"
sed -i -e "
\$a dev tap
\$a server-bridge ${OVPN_ADDR} ${OVPN_MASK} ${OVPN_POOL}
\$a push \"dhcp-option DNS ${OVPN_DNS}\"
" /etc/openvpn/server.conf
sed -i -e "
\$a dev tap
" /etc/openvpn/client.ovpn
/etc/init.d/openvpn restart
uci -q delete firewall.lan.device
uci commit firewall
/etc/init.d/firewall restart
uci set network.lan.type="bridge"
uci set network.lan.ifname="${NET_IF%% *} tap0"
uci commit network
/etc/init.d/network restart


Enable lz4 compression. Beware of compatibility and security issues.

cat << EOF >> /etc/openvpn/server.conf
compress lz4
push "compress lz4"
/etc/init.d/openvpn restart


If using OpenVPN 2.3 or older, replace tls-crypt with tls-auth.

sed -i -e "
\$a key-direction 0
" /etc/openvpn/server.conf
/etc/init.d/openvpn restart
sed -i -e "
\$a key-direction 1
" /etc/openvpn/client.conf
/etc/init.d/openvpn restart

Verbose logging

Increase log verbosity for troubleshooting.

sed -i -e "
\$a verb 5
" /etc/openvpn/*.conf
/etc/init.d/openvpn restart

Dual-stack Windows client

Fix IPv6 routing for Windows desktop client when using dual-stack mode.

NETSH_IPV6="C:\\\\Windows\\\\System32\\\\cmd.exe /c netsh interface ipv6"
cat << EOF >> /etc/openvpn/client.ovpn
script-security 2
up '${NETSH_IPV6} set privacy state=disabled store=active & echo'
ipchange '${NETSH_IPV6} set global randomizeidentifiers=disabled store=active & echo'
route-up '${NETSH_IPV6} delete route prefix=%ifconfig_ipv6_local%/%ifconfig_ipv6_netbits% interface=%dev_idx% store=active'

DNS over VPN

Utilize DNS over VPN to prevent DNS leak.

Disable peer DNS and configure a VPN-routed DNS provider on OpenWrt client.

Modify the VPN connection using NetworkManager on Linux desktop client.

nmcli connection modify id VPN_CON ipv4.dns-search ~. ipv6.dns-search ~. ipv4.dns-priority -50 ipv6.dns-priority -50

Modify the VPN client profile for Windows desktop client.

cat << EOF >> /etc/openvpn/client.ovpn

DNS and domain

Use DNS and domain provided by VPN server on OpenWrt client.

cat << "EOF" > /etc/openvpn/
env | sed -n -e "
" | sort -u > /tmp/resolv.conf.vpn
case ${script_type} in
(up) uci set dhcp.@dnsmasq[0].resolvfile="/tmp/resolv.conf.vpn" ;;
(down) uci revert dhcp ;;
/etc/init.d/dnsmasq restart &
chmod +x /etc/openvpn/
sed -i -e "
\$a script-security 2
\$a up /etc/openvpn/
\$a down /etc/openvpn/
" /etc/openvpn/client.conf
/etc/init.d/openvpn restart

Kill switch

Prevent traffic leak on OpenWrt client isolating VPN interface in a separate firewall zone.

uci rename firewall.@forwarding[0]="lan_wan"
uci set firewall.lan_wan.enabled="0"
uci -q delete firewall.vpn
uci set firewall.vpn="zone"
uci set"vpn"
uci set firewall.vpn.input="REJECT"
uci set firewall.vpn.output="ACCEPT"
uci set firewall.vpn.forward="REJECT"
uci set firewall.vpn.masq="1"
uci set firewall.vpn.mtu_fix="1"
uci add_list firewall.vpn.device="tun+"
uci del_list firewall.wan.device="tun+"
uci -q delete firewall.lan_vpn
uci set firewall.lan_vpn="forwarding"
uci set firewall.lan_vpn.src="lan"
uci set firewall.lan_vpn.dest="vpn"
uci commit firewall
/etc/init.d/firewall restart
cat << "EOF" > /etc/openvpn/
if pgrep openvpn
uci set firewall.lan_wan.enabled="1"
/etc/init.d/openvpn stop &
uci revert firewall
/etc/init.d/openvpn start &
/etc/init.d/firewall restart &
chmod +x /etc/openvpn/
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
docs/guide-user/services/vpn/openvpn/extras.txt · Last modified: 2020/10/18 06:19 by vgaetera