{"id":917,"date":"2025-08-29T18:03:09","date_gmt":"2025-08-29T10:03:09","guid":{"rendered":"https:\/\/vm1.go2see.me\/?p=917"},"modified":"2025-08-29T18:03:09","modified_gmt":"2025-08-29T10:03:09","slug":"openvpn-server-openwrt","status":"publish","type":"post","link":"https:\/\/vm1.go2see.me\/?p=917","title":{"rendered":"OpenVPN server &#8211; OpenWRT"},"content":{"rendered":"<h1>OpenVPN server &#8211; OpenWRT<\/h1>\n<p>This article relies on the following:<\/p>\n<ul>\n<li>Accessing <a href=\"https:\/\/openwrt.org\/docs\/guide-quick-start\/walkthrough_login\">web interface<\/a> \/ <a href=\"https:\/\/openwrt.org\/docs\/guide-quick-start\/sshadministration\">command-line interface<\/a><\/li>\n<li>Managing <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/base-system\/uci\">configs<\/a> \/ <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/additional-software\/managing_packages\">packages<\/a> \/ <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/base-system\/managing_services\">services<\/a> \/ <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/base-system\/log.essentials\">logs<\/a><\/li>\n<\/ul>\n<h2>Introduction<\/h2>\n<ul>\n<li>This how-to describes the method for setting up <a href=\"https:\/\/en.wikipedia.org\/wiki\/OpenVPN\">OpenVPN<\/a> server on OpenWrt.<\/li>\n<li>Follow <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/services\/vpn\/openvpn\/client\">OpenVPN client<\/a> for client setup and <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/services\/vpn\/openvpn\/extras\">OpenVPN extras<\/a> for additional tuning.<\/li>\n<li>It requires OpenWrt 21.02+ with OpenVPN 2.5+ supporting <code>tls-crypt-v2<\/code>.\n<ul>\n<li>OpenWrt 19.07 users with OpenVPN 2.4 should refer to an <strong><a href=\"https:\/\/openwrt.org\/docs\/guide-user\/services\/vpn\/openvpn\/server?rev=1632708683\">older revision<\/a><\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Goals<\/h2>\n<ul>\n<li>Encrypt your internet connection to enforce security and privacy.\n<ul>\n<li>Prevent traffic leaks and spoofing on the client side.<\/li>\n<\/ul>\n<\/li>\n<li>Bypass regional restrictions using commercial providers.\n<ul>\n<li>Escape client side content filters and internet censorship.<\/li>\n<\/ul>\n<\/li>\n<li>Access your LAN services remotely without port forwarding.<\/li>\n<\/ul>\n<h2>Command-line instructions<\/h2>\n<h3>1. Preparation<\/h3>\n<pre><code class=\"language-bash\"># for iStoreOS:\necho \"src\/gz openwrt_base https:\/\/downloads.openwrt.org\/releases\/24.10.2\/packages\/aarch64_cortex-a53\/base\"  > \/etc\/opkg\/compatfeeds.conf\necho \"src\/gz openwrt_luci https:\/\/downloads.openwrt.org\/releases\/24.10.2\/packages\/aarch64_cortex-a53\/luci\"  >> \/etc\/opkg\/compatfeeds.conf\necho \"src\/gz openwrt_packages https:\/\/downloads.openwrt.org\/releases\/24.10.2\/packages\/aarch64_cortex-a53\/packages\"  >> \/etc\/opkg\/compatfeeds.conf\necho \"src\/gz openwrt_routing https:\/\/downloads.openwrt.org\/releases\/24.10.2\/packages\/aarch64_cortex-a53\/routing\"  >> \/etc\/opkg\/compatfeeds.conf\necho \"src\/gz openwrt_telephony https:\/\/downloads.openwrt.org\/releases\/24.10.2\/packages\/aarch64_cortex-a53\/telephony\"  >> \/etc\/opkg\/compatfeeds.conf\n\n#opkg update\n#opkg install openvpn-easy-rsa openvpn-mbedtls luci-app-openvpn <\/code><\/pre>\n<p>Install the required packages. Specify configuration parameters for VPN server.<\/p>\n<pre><code class=\"language-bash\"># Install packages\nopkg update\nopkg install openvpn-openssl openvpn-easy-rsa\n\n# Configuration parameters\nexport VPN_DIR=\"\/etc\/openvpn\"\nexport VPN_PKI=\"\/etc\/easy-rsa\/pki\"\nexport VPN_PORT=\"8100\"\nexport VPN_PROTO=\"udp\"\nexport VPN_POOL=\"192.168.9.0 255.255.255.0\"\nexport VPN_DNS=\"${VPN_POOL%.* *}.1\"\nexport VPN_DN=\"$(uci -q get dhcp.@dnsmasq[0].domain)\"\n\n# Fetch server address\nexport NET_FQDN=\"$(uci -q get ddns.@service[0].lookup_host)\"\n. \/lib\/functions\/network.sh\nnetwork_flush_cache\nnetwork_find_wan NET_IF\nnetwork_get_ipaddr NET_ADDR \"${NET_IF}\"\nif [ -n \"${NET_FQDN}\" ]\nthen export VPN_SERV=\"${NET_FQDN}\"\nelse export VPN_SERV=\"${NET_ADDR}\"\nfi<\/code><\/pre>\n<h3>2. Key management<\/h3>\n<p>Use <a href=\"https:\/\/github.com\/OpenVPN\/easy-rsa#overview\">EasyRSA<\/a> to manage the PKI. Utilize private key password protection if necessary.<\/p>\n<pre><code class=\"language-bash\"># Configuration parameters\nexport EASYRSA_PKI=\"${VPN_PKI}\"\nexport EASYRSA_TEMP_DIR=\"\/tmp\"\nexport EASYRSA_CERT_EXPIRE=\"36500\"\nexport EASYRSA_BATCH=\"1\"\n\n# Remove and re-initialize PKI directory\neasyrsa init-pki\n\n# Generate DH parameters\neasyrsa gen-dh\n\n# Create a new CA\neasyrsa build-ca nopass\n\n# Generate server keys and certificate\neasyrsa build-server-full server nopass\nopenvpn --genkey tls-crypt-v2-server ${EASYRSA_PKI}\/private\/server.pem\n\n#  Generate client keys and certificate\n#easyrsa build-client-full client nopass\n#openvpn --tls-crypt-v2 ${EASYRSA_PKI}\/private\/server.pem \\\n#--genkey tls-crypt-v2-client ${EASYRSA_PKI}\/private\/client.pem\n\nfor u in cyue cyuePC cyueMBP cyuePad  tony tonyPC tonyMBP tonyPad shari shariPC shariPad shariMBP johnny johnnyPC johnnyPad johnnyMBP yezi yuhan yuhanPC yuhanSamsung yuchen yuchenPC yuchenPad\ndo\n    echo $u\n    #easyrsa build-client-full $u nopass\n    easyrsa build-client-full $u nopass\n    openvpn --tls-crypt-v2 ${EASYRSA_PKI}\/private\/server.pem \\\n    --genkey tls-crypt-v2-client ${EASYRSA_PKI}\/private\/$u.pem\ndone<\/code><\/pre>\n<h3>3. Firewall<\/h3>\n<p>Consider VPN network as private. Assign VPN interface to LAN zone to minimize firewall setup. Allow access to VPN server from WAN zone.<\/p>\n<pre><code class=\"language-bash\"># Configure firewall\nuci rename firewall.@zone[0]=\"lan\"\nuci rename firewall.@zone[1]=\"wan\"\nuci del_list firewall.lan.device=\"tun+\"\nuci add_list firewall.lan.device=\"tun+\"\nuci -q delete firewall.ovpn\nuci set firewall.ovpn=\"rule\"\nuci set firewall.ovpn.name=\"Allow-OpenVPN\"\nuci set firewall.ovpn.src=\"wan\"\nuci set firewall.ovpn.dest_port=\"${VPN_PORT}\"\nuci set firewall.ovpn.proto=\"${VPN_PROTO}\"\nuci set firewall.ovpn.target=\"ACCEPT\"\nuci commit firewall\nservice firewall restart<\/code><\/pre>\n<h3>4. VPN service<\/h3>\n<p>Configure VPN service and generate client profiles.<\/p>\n<pre><code class=\"language-bash\"># Configure VPN service and generate client profiles\numask go=\nVPN_DH=\"$(cat ${VPN_PKI}\/dh.pem)\"\nVPN_CA=\"$(openssl x509 -in ${VPN_PKI}\/ca.crt)\"\nls ${VPN_PKI}\/issued \\\n| sed -e \"s\/\\.\\w*$\/\/\" \\\n| while read -r VPN_ID\ndo\nVPN_TC=\"$(cat ${VPN_PKI}\/private\/${VPN_ID}.pem)\"\nVPN_KEY=\"$(cat ${VPN_PKI}\/private\/${VPN_ID}.key)\"\nVPN_CERT=\"$(openssl x509 -in ${VPN_PKI}\/issued\/${VPN_ID}.crt)\"\nVPN_EKU=\"$(echo \"${VPN_CERT}\" | openssl x509 -noout -purpose)\"\ncase ${VPN_EKU} in\n(*\"SSL server : Yes\"*)\nVPN_CONF=\"${VPN_DIR}\/${VPN_ID}.conf\"\ncat << EOF > ${VPN_CONF} ;;\nuser nobody\ngroup nogroup\ndev tun\nport ${VPN_PORT}\nproto ${VPN_PROTO}\nserver ${VPN_POOL}\ntopology subnet\nclient-to-client\nkeepalive 10 60\npersist-tun\npersist-key\npush \"dhcp-option DNS ${VPN_DNS}\"\npush \"dhcp-option DOMAIN ${VPN_DN}\"\npush \"redirect-gateway def1\"\npush \"persist-tun\"\npush \"persist-key\"\n<dh>\n${VPN_DH}\n<\/dh>\nEOF\n(*\"SSL client : Yes\"*)\nVPN_CONF=\"${VPN_DIR}\/${VPN_ID}.ovpn\"\ncat << EOF > ${VPN_CONF} ;;\nuser nobody\ngroup nogroup\ndev tun\nnobind\nclient\nremote ${VPN_SERV} ${VPN_PORT} ${VPN_PROTO}\nauth-nocache\nremote-cert-tls server\nEOF\nesac\ncat << EOF >> ${VPN_CONF}\n<tls-crypt-v2>\n${VPN_TC}\n<\/tls-crypt-v2>\n<key>\n${VPN_KEY}\n<\/key>\n<cert>\n${VPN_CERT}\n<\/cert>\n<ca>\n${VPN_CA}\n<\/ca>\nEOF\ndone\nservice openvpn restart\nls ${VPN_DIR}\/*.ovpn<\/code><\/pre>\n<pre><code class=\"language-bash\"># \u6539ovpn\u4e2d\u7684 server addr \u5230\u771f\u6b63\u7684 addr\uff1a\ncd \/etc\/openvpn\nsed -i 's|yourhost.example.com|192.168.0.100|g' $(find . -name \\*.ovpn)\n<\/code><\/pre>\n<p>Basic openvpn server configuration is now complete.<\/p>\n<ol>\n<li>Perform OpenWrt <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/troubleshooting\/backup_restore\">backup<\/a>.<\/li>\n<li>Either extract client profile from the archive file, or use SCP to retrieve the \/etc\/openvpn\/client.ovpn file from the router.<\/li>\n<li>Review\/edit the IP address for the &#8216;remote&#8217; line contained within the client.ovpn file.<\/li>\n<li>Import the client.ovpn profile into your clients.<\/li>\n<\/ol>\n<p>For an additional .ovpn after completing the above:<\/p>\n<ol>\n<li>Run this <a href=\"https:\/\/openwrt.org\/docs\/guide-user\/services\/vpn\/openvpn\/extras#multi-client\">multi-client<\/a> script.<\/li>\n<li>Now make a script consisting of the \u201cConfiguration parameters\u201d of Part 1 above and all of Part 4 above and run it. Note that the \u201cremote\u201d line may be missing in the new ovpn (use the original as a reference for that).<\/li>\n<\/ol>\n<h2>Testing<\/h2>\n<p>Establish the VPN connection. Verify your routing with <a href=\"http:\/\/man.cx\/traceroute(8)\">traceroute<\/a> and <a href=\"http:\/\/man.cx\/traceroute6(8)\">traceroute6<\/a>.<\/p>\n<pre><code><\/code><\/pre>\n<p>Check your IP and DNS provider.<\/p>\n<ul>\n<li><a href=\"https:\/\/ipleak.net\/\">ipleak.net<\/a><\/li>\n<li><a href=\"https:\/\/www.dnsleaktest.com\/\">dnsleaktest.com<\/a><\/li>\n<\/ul>\n<p>On router:<\/p>\n<ul>\n<li>Go to <strong>LuCI &gt; Status &gt; Wireguard<\/strong> and look for peer device connected with an IPv4 or IPv6 address and with a recent handshake time<\/li>\n<li>Go to <strong>LuCI &gt; Network &gt; Diagnostics<\/strong> and <strong>ipv4 ping<\/strong> client device IP eg. 10.0.0.10<\/li>\n<\/ul>\n<p>On client device depending on wireguard software:<\/p>\n<ul>\n<li>Check transfer traffic for tx &amp; rx<\/li>\n<li>Ping router internal lan IP<\/li>\n<li>Check public IP address in a browser \u2013 <a href=\"https:\/\/whatsmyip.com\/\">https:\/\/whatsmyip.com<\/a> \u2013 should see public IP address of ISP for the router<\/li>\n<\/ul>\n<h2>Troubleshooting<\/h2>\n<p>Collect and analyze the following information.<\/p>\n<pre><code># Restart services\nservice log restart; service openvpn restart; sleep 10\n\n# Log and status\nlogread -e openvpn; netstat -l -n -p | grep -e openvpn\n\n# Runtime configuration\npgrep -f -a openvpn\nip address show; ip route show table all\nip rule show; ip -6 rule show; nft list ruleset\n\n# Persistent configuration\nuci show network; uci show firewall; uci show openvpn\nhead -v -n -0 \/etc\/openvpn\/*.conf<\/code><\/pre>\n<h2>Notes<\/h2>\n<p>For beginners to OpenVPN server, this PDF guide may be helpful. It is based on above cli instructions with additional note and tips. [OpenVPN server setup guide for BT Home Hub 5A](<a href=\"https:\/\/www.dropbox.com\/s\/idjzqs3cyyb1zai\/7-OpenVPN\">https:\/\/www.dropbox.com\/s\/idjzqs3cyyb1zai\/7-OpenVPN<\/a> Server for HH5A.pdf?dl=0)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenVPN server &#8211; OpenWRT This article relies on t&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/vm1.go2see.me\/?p=917\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,15],"tags":[],"class_list":["post-917","post","type-post","status-publish","format-standard","hentry","category-linux","category-server"],"_links":{"self":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=917"}],"version-history":[{"count":2,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/917\/revisions"}],"predecessor-version":[{"id":919,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=\/wp\/v2\/posts\/917\/revisions\/919"}],"wp:attachment":[{"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vm1.go2see.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}