<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>VPN on Zet’s playground</title>
    <link>https://blog.zet.tw/en/tags/vpn/</link>
    <description>Recent content in VPN on Zet’s playground</description>
    <image>
      <title>Zet’s playground</title>
      <url>https://blog.zet.tw/img/blog-cover.png</url>
      <link>https://blog.zet.tw/img/blog-cover.png</link>
    </image>
    <generator>Hugo -- 0.139.0</generator>
    <language>en</language>
    <lastBuildDate>Sun, 03 Apr 2022 00:00:00 +0800</lastBuildDate>
    <atom:link href="https://blog.zet.tw/en/tags/vpn/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Setting up a WireGuard VPN on a Raspberry Pi</title>
      <link>https://blog.zet.tw/en/posts/2022-04-03-raspberry-pi-wireguard-vpn/</link>
      <pubDate>Sun, 03 Apr 2022 00:00:00 +0800</pubDate>
      <guid>https://blog.zet.tw/en/posts/2022-04-03-raspberry-pi-wireguard-vpn/</guid>
      <description>&lt;p&gt;I used to run Synology&amp;rsquo;s built-in OpenVPN Server. I had a spare Raspberry Pi lying around and figured I&amp;rsquo;d stand up another VPN server and play with WireGuard at the same time. After trying it out, the speed, configuration and chaining to an internal DNS server all worked without issue, so I&amp;rsquo;ll probably make it my main connection method going forward.&lt;/p&gt;
&lt;h1 id=&#34;raspberry-pi-setting&#34;&gt;Raspberry Pi Setting&lt;/h1&gt;
&lt;p&gt;Flash &lt;code&gt;lite x64&lt;/code&gt; with the official tool and drop in an &lt;code&gt;ssh&lt;/code&gt; file so the SSH server starts automatically:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>I used to run Synology&rsquo;s built-in OpenVPN Server. I had a spare Raspberry Pi lying around and figured I&rsquo;d stand up another VPN server and play with WireGuard at the same time. After trying it out, the speed, configuration and chaining to an internal DNS server all worked without issue, so I&rsquo;ll probably make it my main connection method going forward.</p>
<h1 id="raspberry-pi-setting">Raspberry Pi Setting</h1>
<p>Flash <code>lite x64</code> with the official tool and drop in an <code>ssh</code> file so the SSH server starts automatically:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>scoop install raspberry-pi-imager
</span></span></code></pre></div><p>A round of updates:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>sudo apt-get update
</span></span><span style="display:flex;"><span>sudo apt-get dist-upgrade
</span></span><span style="display:flex;"><span>sudo apt-get autoremove
</span></span><span style="display:flex;"><span>sudo apt-get autoclean
</span></span></code></pre></div><p>Use <code>raspi-config</code> to update the <strong>hostname, password</strong> + <strong>expand filesystem</strong> + <strong>time zone</strong>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>sudo raspi-config
</span></span></code></pre></div><p>Update the firmware:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>sudo rpi-update
</span></span></code></pre></div><h1 id="install-pivpn">Install PiVPN</h1>
<p>Install pivpn and choose <strong>WireGuard</strong> as the VPN server. Just fill in the prompts; for DNS you can set a custom IP. I use AdGuard Home for DNS rewrites of my internal services, so I fill in AdGuard&rsquo;s IP.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>curl -L https://install.pivpn.io | bash
</span></span></code></pre></div><p>Here you can check whether the service and the port you configured are up:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>$ sudo wg
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>interface: wg0
</span></span><span style="display:flex;"><span>  public key: ***********
</span></span><span style="display:flex;"><span>  private key: <span style="color:#f92672">(</span>hidden<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span>  listening port: <span style="color:#ae81ff">5487</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>$ sudo netstat -tunlp
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>udp        <span style="color:#ae81ff">0</span>      <span style="color:#ae81ff">0</span> 0.0.0.0:5487           0.0.0.0:*                           -
</span></span><span style="display:flex;"><span>udp6       <span style="color:#ae81ff">0</span>      <span style="color:#ae81ff">0</span> :::5487                :::*                                -
</span></span></code></pre></div><p>To add a user, just run <code>pivpn -a -n &lt;name&gt;</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-gdscript3" data-lang="gdscript3"><span style="display:flex;"><span><span style="color:#f92672">$</span> pivpn <span style="color:#f92672">-</span>a <span style="color:#f92672">-</span>n test
</span></span><span style="display:flex;"><span>::: Client Keys generated
</span></span><span style="display:flex;"><span>::: Client config generated
</span></span><span style="display:flex;"><span>::: Updated server config
</span></span><span style="display:flex;"><span>::: WireGuard reloaded
</span></span><span style="display:flex;"><span><span style="color:#f92672">======================================================================</span>
</span></span><span style="display:flex;"><span>::: Done<span style="color:#f92672">!</span> test<span style="color:#f92672">.</span>conf successfully created<span style="color:#f92672">!</span>
</span></span><span style="display:flex;"><span>::: test<span style="color:#f92672">.</span>conf was copied to <span style="color:#f92672">/</span>home<span style="color:#f92672">/</span>pi<span style="color:#f92672">/</span>configs <span style="color:#66d9ef">for</span> easy transfer<span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span>::: Please use this profile only on one device <span style="color:#f92672">and</span> create additional
</span></span><span style="display:flex;"><span>::: profiles <span style="color:#66d9ef">for</span> other devices<span style="color:#f92672">.</span> You can also use pivpn <span style="color:#f92672">-</span>qr
</span></span><span style="display:flex;"><span>::: to generate a QR Code you can scan with the mobile app<span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">======================================================================</span>
</span></span></code></pre></div><p>For more commands and usage see the pivpn docs <a href="https://docs.pivpn.io/wireguard/">https://docs.pivpn.io/wireguard/</a>, which cover removing, disabling and listing users, backup and restore, and other settings — for example how to configure it if you also have Pi-hole installed.</p>
<p><code>pivpn -qr</code> shows a QR code in the terminal that you can scan with your phone; on a computer you can just copy the config file under <code>configs</code> and feed it to the computer&rsquo;s client.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>$ pivpn -qr
</span></span><span style="display:flex;"><span>::  Client list  ::
</span></span><span style="display:flex;"><span>1) test
</span></span><span style="display:flex;"><span>Please enter the Index/Name of the Client to show: 1
</span></span><span style="display:flex;"><span>::: Showing client test below
</span></span><span style="display:flex;"><span>=====================================================================
</span></span></code></pre></div><p>For the WireGuard client, see the official site <a href="https://www.wireguard.com/install/">https://www.wireguard.com/install/</a> to download the right one.</p>
<blockquote>
<p>If you use port forwarding, remember to open the UDP port and configure it.</p>
</blockquote>
<p>Once everything is set up you should see the connected devices:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span> $ pivpn -c
</span></span><span style="display:flex;"><span>::: Connected Clients List :::
</span></span><span style="display:flex;"><span>Name      Remote IP                 Virtual IP      Bytes Received      Bytes Sent      Last Seen
</span></span><span style="display:flex;"><span>zet       114.136.***.**:35713      10.6.0.2        5.4MiB              36MiB           Apr 03 2022 - 17:57:05
</span></span></code></pre></div><h1 id="misc">Misc</h1>
<p>A few files you may need to modify:</p>
<ul>
<li><code>/etc/wireguard/wg0.conf</code> stores settings like <code>PrivateKey</code> and <code>ListenPort</code>; if you need to change the port or add other settings later, edit it here</li>
<li><code>/etc/pivpn/wireguard/setupVars.conf</code> holds settings such as port, dns and host addr, mainly the extra values used when generating configs and QR codes</li>
</ul>
]]></content:encoded>
    </item>
  </channel>
</rss>
