{"id":1,"date":"2021-03-16T15:38:03","date_gmt":"2021-03-16T15:38:03","guid":{"rendered":"https:\/\/honest-magic.ch\/?p=1"},"modified":"2021-08-08T16:30:06","modified_gmt":"2021-08-08T15:30:06","slug":"site-to-site-vpn-usg-ubuntu","status":"publish","type":"post","link":"https:\/\/honest-magic.ch\/index.php\/2021\/03\/16\/site-to-site-vpn-usg-ubuntu\/","title":{"rendered":"Tutorial: Setup Site-To-Site VPN with OpenVPN, Unifi Security Gateway and Ubuntu"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Here is some experience around the setup of an OpenVPN site-to-site connection from Ubuntu 20.04.2 LTS to UniFi Security Gateway (USG) written down.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparation<\/h2>\n\n\n\n<h5 class=\"wp-block-heading\">UniFi Security Gateway<\/h5>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>Public IP address<\/td><td>1.1.1.1<\/td><\/tr><tr><td>Local subnet<\/td><td>192.168.1.0\/24<\/td><\/tr><tr><td>Local IP address<\/td><td>192.168.1.1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Ubuntu 20.4.2 LTS<\/h5>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Public IP address<\/td><td>4.4.4.4<\/td><\/tr><tr><td>Local subnet <\/td><td>192.168.4.0\/24<\/td><\/tr><tr><td>Local IP address<\/td><td>192.168.4.1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To setup an OpenVPN site-to-site VPN on the UniFi Security Gateway access is needed to the UniFi Network Controller 6.0.45 console.  To generate the needed preshared key you need access to the USG using SSH. These steps are based on the UniFi Network Controller 6.0.45 and the Classic UI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Preshared Key<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Connect to the USG using SSH, e.g.<br>$ <code>ssh <\/code>user@192.168.1.1<\/li><li>Generate the OpenVPN preshared key<br><code>$ <\/code>generate vpn openvpn-key \/tmp\/ovpn<br><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"805\" class=\"wp-image-14\" style=\"width: 768px;\" src=\"https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-203935.png\" alt=\"\" srcset=\"https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-203935.png 1213w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-203935-286x300.png 286w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-203935-977x1024.png 977w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-203935-768x805.png 768w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/li><li>Copy the key between <br>&#8212;&#8211;BEGIN OpenVPN Static key V1&#8212;&#8211; <br>and <br>&#8212;&#8211;END OpenVPN Static key V1&#8212;&#8211; <br>and remove the newlines for the USG configuration.<\/li><li>Save the whole \/tmp\/ovpn file content for the Ubuntu configuration.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">UniFi Security Gateway Configuration<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Login to the UniFi Network Controller and open the Settings in the Classic UI<br><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"403\" class=\"wp-image-11\" style=\"width: 250px;\" src=\"https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-200859.png\" alt=\"\" srcset=\"https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-200859.png 738w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-200859-186x300.png 186w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/Screenshot-2021-03-16-200859-635x1024.png 635w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/li><li>Open &#8220;Networks&#8221; and press<br> <img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"39\" class=\"wp-image-12\" style=\"width: 150px;\" src=\"https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-3.png\" alt=\"\"><\/li><li>Select &#8220;Site to Site VPN&#8221; as purpose and choose OpenVPN as type. Fill in the form as showed in the <a href=\"#usg-settings\" data-type=\"URL\">picture<\/a> below. The preshared key we generated in the last section can now be entered here.<\/li><li>Save the network<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image alignwide size-large is-style-default\" id=\"usg-settings\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"691\" src=\"https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-4-1024x691.png\" alt=\"\" class=\"wp-image-16\" srcset=\"https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-4-1024x691.png 1024w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-4-300x202.png 300w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-4-768x518.png 768w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-4-1536x1037.png 1536w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-4-1568x1058.png 1568w, https:\/\/honest-magic.ch\/wp-content\/uploads\/2021\/03\/image-4.png 1855w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>USG configuration<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Ubuntu 20.04.2 LTS Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Access the Linux on a shell. You will need sudo permissions.Install OpenVPN<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install OpenVPN<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt install openvpn<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Enable IP Forwarding<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo cat &gt;&gt; \/etc\/sysctl.conf &lt;&lt; EOF\nnet.ipv4.ip_forward = 1\nnet.ipv6.conf.all.forwarding = 1\nnet.ipv4.conf.all.accept_redirects = 0\nnet.ipv4.conf.all.send_redirects = 0\nEOF\n\n$ sudo sysctl -p<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure Firewall<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following text at the beginning of the file <strong>\/etc\/ufw\/before.rules<\/strong> before everything else:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*nat\n:POSTROUTING ACCEPT &#91;0:0]\n\n-A POSTROUTING -s 192.168.1.0\/24 -d 192.168.4.0\/24 -j MASQUERADE\n\nCOMMIT<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo ufw disable\n$ sudo ufw enable<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">OpenVPN Configuration File<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create the configuration file <strong>\/etc\/openvpn\/server\/demo-vpn.conf<\/strong> with the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\n# Sample OpenVPN configuration file for\n# office using a pre-shared static key.\n#\n# '#' or ';' may be used to delimit comments.\n\n# Use a dynamic tun device.\n# For Linux 2.2 or non-Linux OSes,\n# you may want to use an explicit\n# unit number such as \"tun1\".\n# OpenVPN also supports virtual\n# ethernet \"tap\" devices.\ndev tun42\n\nremote 1.1.1.1\n\n\nifconfig 10.1.0.2 10.1.0.1\n\nscript-security 3\n\n# Our up script will establish routes\n# once the VPN is alive.\nup .\/demo-configure-routes.up\n\n# Our pre-shared static key\nsecret demo-preshared.key\n\n# OpenVPN 2.0 uses UDP port 1194 by default\n# (official port assignment by iana.org 11\/04).\n# OpenVPN 1.x uses UDP port 5000 by default.\n# Each OpenVPN tunnel must use\n# a different port number.\n# lport or rport can be used\n# to denote different ports\n# for local and remote.\n;port 1194\n\n# Downgrade UID and GID to\n# \"nobody\" after initialization\n# for extra security.\n; user nobody\n; group nobody\n\n# If you built OpenVPN with\n# LZO compression, uncomment\n# out the following line.\n; comp-lzo\n\n# Send a UDP ping to remote once\n# every 15 seconds to keep\n# stateful firewall connection\n# alive.  Uncomment this\n# out if you are using a stateful\n# firewall.\n; ping 15\n\n# Uncomment this section for a more reliable detection when a system\n# loses its connection.  For example, dial-ups or laptops that\n# travel to other locations.\nping 15\nping-restart 45\nping-timer-rem\npersist-tun\npersist-key\n\n# Verbosity level.\n# 0 -- quiet except for fatal errors.\n# 1 -- mostly quiet, but display non-fatal network errors.\n# 3 -- medium output, good for normal operation.\n# 9 -- verbose, good for troubleshooting\nverb 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save the content of the generated OpenVPN key (\/tmp\/ovpn on USG) to the file <strong>\/etc\/openvpn\/server\/demo-preshared.key<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create the file <strong>\/etc\/openvpn\/server\/demo-configure-routes.up<\/strong> with the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\nroute add -net 192.168.1.0 netmask 255.255.255.0 gw $5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and make it executable <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo chmod +x \/etc\/openvpn\/server\/demo-configure-routes.up<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Finishing up<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Start the VPN with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo systemctl start openvpn-server@<strong>demo-vpn<\/strong>.service<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To inspect the status call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo systemctl status openvpn-server@<strong>demo-vpn<\/strong>.service<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To start the site-to-site VPN at boot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo systemctl enable openvpn-server@<strong>demo-vpn<\/strong>.service<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Test the VPN<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try ping the USG through the tunnel:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ping 10.1.0.1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-openvpn wp-block-embed-openvpn\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"tqmCgNqsj5\"><a href=\"https:\/\/openvpn.net\/vpn-server-resources\/site-to-site-routing-explained-in-detail\/\">Site-to-site VPN routing explained in detail<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Site-to-site VPN routing explained in detail&#8221; &#8212; OpenVPN\" src=\"https:\/\/openvpn.net\/vpn-server-resources\/site-to-site-routing-explained-in-detail\/embed\/#?secret=tqmCgNqsj5\" data-secret=\"tqmCgNqsj5\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-openvpn wp-block-embed-openvpn\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"skBtESKyHK\"><a href=\"https:\/\/openvpn.net\/community-resources\/1xhowto\/\">1x HOW TO<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;1x HOW TO&#8221; &#8212; OpenVPN\" src=\"https:\/\/openvpn.net\/community-resources\/1xhowto\/embed\/#?secret=skBtESKyHK\" data-secret=\"skBtESKyHK\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is some experience around the setup of an OpenVPN site-to-site connection from Ubuntu 20.04.2 LTS to UniFi Security Gateway (USG) written down. Preparation UniFi Security Gateway Public IP address 1.1.1.1 Local subnet 192.168.1.0\/24 Local IP address 192.168.1.1 Ubuntu 20.4.2 LTS Public IP address 4.4.4.4 Local subnet 192.168.4.0\/24 Local IP address 192.168.4.1 To setup an&hellip; <a class=\"more-link\" href=\"https:\/\/honest-magic.ch\/index.php\/2021\/03\/16\/site-to-site-vpn-usg-ubuntu\/\">Continue reading <span class=\"screen-reader-text\">Tutorial: Setup Site-To-Site VPN with OpenVPN, Unifi Security Gateway and Ubuntu<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[3,4,5],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-openvpn","tag-ubuntu","tag-unifi","entry"],"_links":{"self":[{"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":16,"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/posts\/1\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/honest-magic.ch\/index.php\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}