Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:services:vpn:openvpn:server [2022/03/11 21:35] – Clarified how to add another .ovpn rseilerdocs:guide-user:services:vpn:openvpn:server [2023/10/14 05:14] – use service invocation vgaetera
Line 1: Line 1:
 ====== OpenVPN server ====== ====== OpenVPN server ======
-{{section>meta:infobox:howto_links#cli_skills&noheader&nofooter&noeditbutton}}+{{section>meta:infobox:howto_links#basic_skills&noheader&nofooter&noeditbutton}}
  
 ===== Introduction ===== ===== Introduction =====
   * This how-to describes the method for setting up [[wp>OpenVPN|OpenVPN]] server on OpenWrt.   * This how-to describes the method for setting up [[wp>OpenVPN|OpenVPN]] server on OpenWrt.
   * Follow [[docs:guide-user:services:vpn:openvpn:client|OpenVPN client]] for client setup and [[docs:guide-user:services:vpn:openvpn:extras|OpenVPN extras]] for additional tuning.   * Follow [[docs:guide-user:services:vpn:openvpn:client|OpenVPN client]] for client setup and [[docs:guide-user:services:vpn:openvpn:extras|OpenVPN extras]] for additional tuning.
 +  * It requires OpenWrt 21.02+ with OpenVPN 2.5+ supporting ''tls-crypt-v2''.
 +    * OpenWrt 19.07 users with OpenVPN 2.4 should refer to an [[docs:guide-user:services:vpn:openvpn:server?rev=1632708683|older revision]].
  
 ===== Goals ===== ===== Goals =====
Line 10: Line 12:
  
 ===== Command-line instructions ===== ===== Command-line instructions =====
- 
-The instructions below have been tested with OpenWrt **21.02**. 
- 
-If you wish to install OpenVPN server on a **previous version of OpenWrt**, please refer to an older revision of this article, to avoid tls-crypt generation errors. 
-eg.[[https://forum.openwrt.org/t/problem-setting-up-openvpn-server/109953/5|forum post]] 
 ==== 1. Preparation ==== ==== 1. Preparation ====
 Install the required packages. Install the required packages.
-Specify the VPN server configuration parameters.+Specify configuration parameters for VPN server.
  
 <code bash> <code bash>
Line 24: Line 21:
 opkg install openvpn-openssl openvpn-easy-rsa opkg install openvpn-openssl openvpn-easy-rsa
  
-# Configuration parameters  # OVPN_POOL config any network are OK except your local network +# Configuration parameters 
-OVPN_DIR="/etc/openvpn" +VPN_DIR="/etc/openvpn" 
-OVPN_PKI="/etc/easy-rsa/pki" +VPN_PKI="/etc/easy-rsa/pki" 
-OVPN_PORT="1194" +VPN_PORT="1194" 
-OVPN_PROTO="udp" +VPN_PROTO="udp" 
-OVPN_POOL="192.168.8.0 255.255.255.0" +VPN_POOL="192.168.8.0 255.255.255.0" 
-OVPN_DNS="${OVPN_POOL%.* *}.1" +VPN_DNS="${VPN_POOL%.* *}.1" 
-OVPN_DOMAIN="$(uci get dhcp.@dnsmasq[0].domain)"+VPN_DN="$(uci -q get dhcp.@dnsmasq[0].domain)"
  
-# Fetch WAN IP address+# Fetch server address 
 +NET_FQDN="$(uci -q get ddns.@service[0].lookup_host)"
 . /lib/functions/network.sh . /lib/functions/network.sh
 network_flush_cache network_flush_cache
 network_find_wan NET_IF network_find_wan NET_IF
 network_get_ipaddr NET_ADDR "${NET_IF}" network_get_ipaddr NET_ADDR "${NET_IF}"
-OVPN_SERV="${NET_ADDR}" 
- 
-# Fetch FQDN from DDNS client 
-NET_FQDN="$(uci -q get ddns.@service[0].lookup_host)" 
 if [ -n "${NET_FQDN}" ] if [ -n "${NET_FQDN}" ]
-then OVPN_SERV="${NET_FQDN}"+then VPN_SERV="${NET_FQDN}" 
 +else VPN_SERV="${NET_ADDR}"
 fi fi
 </code> </code>
Line 53: Line 48:
 <code bash> <code bash>
 # Configuration parameters # Configuration parameters
-export EASYRSA_PKI="${OVPN_PKI}" +cat << EOF > /etc/profile.d/easy-rsa.sh 
-export EASYRSA_REQ_CN="ovpnca"+export EASYRSA_PKI="${VPN_PKI}" 
 +export EASYRSA_TEMP_DIR="/tmp" 
 +export EASYRSA_CERT_EXPIRE="3650"
 export EASYRSA_BATCH="1" export EASYRSA_BATCH="1"
 +EOF
 +. /etc/profile.d/easy-rsa.sh
  
 # Remove and re-initialize PKI directory # Remove and re-initialize PKI directory
Line 91: Line 90:
 uci set firewall.ovpn.name="Allow-OpenVPN" uci set firewall.ovpn.name="Allow-OpenVPN"
 uci set firewall.ovpn.src="wan" uci set firewall.ovpn.src="wan"
-uci set firewall.ovpn.dest_port="${OVPN_PORT}" +uci set firewall.ovpn.dest_port="${VPN_PORT}" 
-uci set firewall.ovpn.proto="${OVPN_PROTO}"+uci set firewall.ovpn.proto="${VPN_PROTO}"
 uci set firewall.ovpn.target="ACCEPT" uci set firewall.ovpn.target="ACCEPT"
 uci commit firewall uci commit firewall
-/etc/init.d/firewall restart+service firewall restart
 </code> </code>
  
Line 104: Line 103:
 # Configure VPN service and generate client profiles # Configure VPN service and generate client profiles
 umask go= umask go=
-OVPN_DH="$(cat ${OVPN_PKI}/dh.pem)" +VPN_DH="$(cat ${VPN_PKI}/dh.pem)" 
-OVPN_CA="$(openssl x509 -in ${OVPN_PKI}/ca.crt)" +VPN_CA="$(openssl x509 -in ${VPN_PKI}/ca.crt)" 
-ls ${OVPN_PKI}/issued \+ls ${VPN_PKI}/issued \
 | sed -e "s/\.\w*$//" \ | sed -e "s/\.\w*$//" \
-| while read -r OVPN_ID+| while read -r VPN_ID
 do do
-OVPN_TC="$(cat ${OVPN_PKI}/private/${OVPN_ID}.pem)" +VPN_TC="$(cat ${VPN_PKI}/private/${VPN_ID}.pem)" 
-OVPN_KEY="$(cat ${OVPN_PKI}/private/${OVPN_ID}.key)" +VPN_KEY="$(cat ${VPN_PKI}/private/${VPN_ID}.key)" 
-OVPN_CERT="$(openssl x509 -in ${OVPN_PKI}/issued/${OVPN_ID}.crt)" +VPN_CERT="$(openssl x509 -in ${VPN_PKI}/issued/${VPN_ID}.crt)" 
-OVPN_EKU="$(echo "${OVPN_CERT}" | openssl x509 -noout -purpose)" +VPN_EKU="$(echo "${VPN_CERT}" | openssl x509 -noout -purpose)" 
-case ${OVPN_EKU} in+case ${VPN_EKU} in
 (*"SSL server : Yes"*) (*"SSL server : Yes"*)
-OVPN_CONF="${OVPN_DIR}/${OVPN_ID}.conf" +VPN_CONF="${VPN_DIR}/${VPN_ID}.conf" 
-cat << EOF > ${OVPN_CONF} ;;+cat << EOF > ${VPN_CONF} ;;
 user nobody user nobody
 group nogroup group nogroup
 dev tun dev tun
-port ${OVPN_PORT+port ${VPN_PORT
-proto ${OVPN_PROTO+proto ${VPN_PROTO
-server ${OVPN_POOL}+server ${VPN_POOL}
 topology subnet topology subnet
 client-to-client client-to-client
Line 129: Line 128:
 persist-tun persist-tun
 persist-key persist-key
-push "dhcp-option DNS ${OVPN_DNS}" +push "dhcp-option DNS ${VPN_DNS}" 
-push "dhcp-option DOMAIN ${OVPN_DOMAIN}"+push "dhcp-option DOMAIN ${VPN_DN}"
 push "redirect-gateway def1" push "redirect-gateway def1"
 push "persist-tun" push "persist-tun"
 push "persist-key" push "persist-key"
 <dh> <dh>
-${OVPN_DH}+${VPN_DH}
 </dh> </dh>
 EOF EOF
 (*"SSL client : Yes"*) (*"SSL client : Yes"*)
-OVPN_CONF="${OVPN_DIR}/${OVPN_ID}.ovpn" +VPN_CONF="${VPN_DIR}/${VPN_ID}.ovpn" 
-cat << EOF > ${OVPN_CONF} ;;+cat << EOF > ${VPN_CONF} ;;
 user nobody user nobody
 group nogroup group nogroup
Line 146: Line 145:
 nobind nobind
 client client
-remote ${OVPN_SERV} ${OVPN_PORT} ${OVPN_PROTO}+remote ${VPN_SERV} ${VPN_PORT} ${VPN_PROTO}
 auth-nocache auth-nocache
 remote-cert-tls server remote-cert-tls server
 EOF EOF
 esac esac
-cat << EOF >> ${OVPN_CONF}+cat << EOF >> ${VPN_CONF}
 <tls-crypt-v2> <tls-crypt-v2>
-${OVPN_TC}+${VPN_TC}
 </tls-crypt-v2> </tls-crypt-v2>
 <key> <key>
-${OVPN_KEY}+${VPN_KEY}
 </key> </key>
 <cert> <cert>
-${OVPN_CERT}+${VPN_CERT}
 </cert> </cert>
 <ca> <ca>
-${OVPN_CA}+${VPN_CA}
 </ca> </ca>
 EOF EOF
 done done
-/etc/init.d/openvpn restart +service openvpn restart 
-ls ${OVPN_DIR}/*.ovpn+ls ${VPN_DIR}/*.ovpn
 </code> </code>
  
-Perform OpenWrt [[docs:guide-user:troubleshooting:backup_restore|backup]]. +Basic openvpn server configuration is now complete. 
-Extract client profiles from the archive and import them to your clients.+  - Perform OpenWrt [[docs:guide-user:troubleshooting:backup_restore|backup]]. 
 +  - Either extract client profile from the archive file, or use SCP to retrieve the <color #ed1c24>/etc/openvpn/client.ovpn</color> file from the router.   
 +  - Review/edit the IP address for the 'remote' line contained within the <color #ed1c24>client.ovpn</color> file.   
 +  - Import the client.ovpn profile into your clients.
  
 For an additional .ovpn after completing the above: For an additional .ovpn after completing the above:
Line 184: Line 186:
 <code bash> <code bash>
 # Restart services # Restart services
-/etc/init.d/log restart; /etc/init.d/openvpn restart; sleep 10+service log restart; service openvpn restart; sleep 10
  
 # Log and status # Log and status
Line 192: Line 194:
 pgrep -f -a openvpn pgrep -f -a openvpn
 ip address show; ip route show table all ip address show; ip route show table all
-ip rule show; iptables-save -c +ip rule show; ip -6 rule show; nft list ruleset
-ip -6 rule show; ip6tables-save -c+
  
 # Persistent configuration # Persistent configuration
Line 199: Line 200:
 head -v -n -0 /etc/openvpn/*.conf head -v -n -0 /etc/openvpn/*.conf
 </code> </code>
 +
 +===== Notes =====
 +For beginners to OpenVPN server, this PDF guide may be helpful.  It is based on above cli instructions with additional note and tips.
 +[[https://www.dropbox.com/s/idjzqs3cyyb1zai/7-OpenVPN%20Server%20for%20HH5A.pdf?dl=0|OpenVPN server setup guide for BT Home Hub 5A]]
  
  • Last modified: 2023/10/29 01:00
  • by vgaetera