Getting Started with WireGuard » Miguel Mota | Software Developer

<div id=""><p><a href="https://www.wireguard.com/" style="color: inherit; text-decoration: none;" name="readabilityLink-1">WireGuard</a><a href="#readabilityFootnoteLink-1" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[1]</sup></small></a> is a relatively new VPN tunnel protocol that aims to be very fast and easy to setup. It follows the <a href="https://homepage.cs.uri.edu/~thenry/resources/unixart/ch01s06.html" style="color: inherit; text-decoration: none;" name="readabilityLink-2">Unix Philosophy</a><a href="#readabilityFootnoteLink-2" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[2]</sup></small></a> closely in that it only <em>does one thing</em> (creating secured VPN tunnels) and <em>does it well</em>.</p><p>If you’ve ever set up an VPN service such as <a href="https://github.com/OpenVPN/openvpn" style="color: inherit; text-decoration: none;" name="readabilityLink-3">OpenVPN</a><a href="#readabilityFootnoteLink-3" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[3]</sup></small></a> before then you know that it can get complicated because of all the steps you have to go through such as generating certificate authorities, issuing server and client keys and certificates, setting up multiple configuration files, configuring firewall rules, setting up route traffic forwarding, etc. which can either be dreadful or daunting. WireGuard is changing all that by simplifying the process of getting up and running in no time and allowing for easy configuration to connect multiple clients (peers).</p><h3 id="why-use-wireguard">Why use WireGuard?</h3><ul><li>A VPN helps protect you from man in the middle attacks.</li><li>Protect your privacy against ISPs that snoop into your traffic.</li><li>Get around internet censorship in countries.</li></ul><p>Advantages of WireGuard over other VPNs:</p><ul><li>It’s kernel-based; improved performance.</li><li>Establishes connections in less than 100ms.</li><li>Small footprint; can be ran in virtually any device, ie. embedded devices.</li><li>Easy to configure and deploy as SSH; reduces attack surface since there’s less complexity.</li><li>Uses modern and improved <a href="https://en.wikipedia.org/wiki/WireGuard#Protocol" style="color: inherit; text-decoration: none;" name="readabilityLink-4">cryptographic standards</a><a href="#readabilityFootnoteLink-4" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[4]</sup></small></a>.</li><li>Simple handshake occurring every few minutes to ensure connection secrecy.</li><li>IP roaming support meaning you can change wifi networks or disconnect from wifi or celluar and the VPN tunnel connection won’t be lost. <em>It just works!</em></li></ul><h3 id="what-well-be-going-over">What we’ll be going over</h3><p>This post assumes that you’ve never installed a VPN service before and we’ll be using an Ubuntu machine since it’s the most popular distro.</p><p>This post is pretty verbose! but you can skip to the <a href="#tldr" style="color: inherit; text-decoration: none;" name="readabilityLink-5">TLDR;</a><a href="#readabilityFootnoteLink-5" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[5]</sup></small></a> to see the final scripts and configuration files used if you’re familiar with the concepts already.</p><p>The steps outlined in this post are:</p><ol><li><a href="#setting-up-a-server" style="color: inherit; text-decoration: none;" name="readabilityLink-6">Setting up a server</a><a href="#readabilityFootnoteLink-6" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[6]</sup></small></a></li><li><a href="#installing-wireguard-on-server" style="color: inherit; text-decoration: none;" name="readabilityLink-7">Installing WireGuard on server</a><a href="#readabilityFootnoteLink-7" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[7]</sup></small></a></li><li><a href="#generating-server-keys" style="color: inherit; text-decoration: none;" name="readabilityLink-8">Generating server keys</a><a href="#readabilityFootnoteLink-8" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[8]</sup></small></a></li><li><a href="#creating-server-configuration-file" style="color: inherit; text-decoration: none;" name="readabilityLink-9">Creating server configuration file</a><a href="#readabilityFootnoteLink-9" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[9]</sup></small></a></li><li><a href="#enabling-ip-forwarding-on-server" style="color: inherit; text-decoration: none;" name="readabilityLink-10">Enabling IP forwarding on server</a><a href="#readabilityFootnoteLink-10" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[10]</sup></small></a></li><li><a href="#installing-wireguard-on-client" style="color: inherit; text-decoration: none;" name="readabilityLink-11">Installing WireGuard on client</a><a href="#readabilityFootnoteLink-11" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[11]</sup></small></a></li><li><a href="#generating-client-keys" style="color: inherit; text-decoration: none;" name="readabilityLink-12">Generating client keys</a><a href="#readabilityFootnoteLink-12" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[12]</sup></small></a></li><li><a href="#creating-client-configuration-file" style="color: inherit; text-decoration: none;" name="readabilityLink-13">Creating client configuration file</a><a href="#readabilityFootnoteLink-13" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[13]</sup></small></a></li><li><a href="#setting-client-info-on-server-config" style="color: inherit; text-decoration: none;" name="readabilityLink-14">Setting client info on server config</a><a href="#readabilityFootnoteLink-14" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[14]</sup></small></a></li><li><a href="#starting-wireguard-service-on-server" style="color: inherit; text-decoration: none;" name="readabilityLink-15">Starting WireGuard service on server</a><a href="#readabilityFootnoteLink-15" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[15]</sup></small></a></li><li><a href="#starting-wireguard-service-on-client" style="color: inherit; text-decoration: none;" name="readabilityLink-16">Starting WireGuard service on client</a><a href="#readabilityFootnoteLink-16" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[16]</sup></small></a></li><li><a href="#connecting-a-mobile-client-to-server" style="color: inherit; text-decoration: none;" name="readabilityLink-17">Connecting a mobile client to server</a><a href="#readabilityFootnoteLink-17" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[17]</sup></small></a></li></ol><p>Please note that in WireGuard land there is no “server” and “client” in the traditional sense. Rather, computers and devices connected to each other are known as “peers”. For simplicity sake, we’ll be using “server” to mean the hosted server that will be forwarding all our traffic to, and we’ll be using “client” to refer to the home computer that forwards all it’s traffic to the server.</p><h2 id="setting-up-a-server">Setting up a server</h2><p>I’ll be using a free tier EC2 micro instance from AWS for the example (and tearing down it afterwards). If you have an AWS account you can launch a new instance by going to:</p><p>EC2 → Launch Instance → t2.micro with Ubuntu → Review and Launch → Launch</p><p>In this example I’m running Ubuntu 18.04 (Bionic Beaver).</p><h2 id="installing-wireguard-on-server">Installing WireGuard on server</h2><p>To install wireguard on Ubuntu &lt;19.04 run the following comands:</p><ol><li><code>sudo add-apt-repository ppa:wireguard/wireguard</code></li><li><code>sudo apt-get update</code></li><li><code>sudo apt-get install wireguard</code></li></ol><p>If your server is using a different distro then look at the WireGuard <a href="https://www.wireguard.com/install/" style="color: inherit; text-decoration: none;" name="readabilityLink-18">installation instructions</a><a href="#readabilityFootnoteLink-18" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[18]</sup></small></a>.</p><div class="highlight" readability="11"><pre><code class="language-bash" data-lang="bash">ubuntu@ip-172-30-0-233:~$ sudo add-apt-repository ppa:wireguard/wireguard
WireGuard is a novel VPN that runs inside the Linux Kernel. This is the Ubuntu packaging <span>for</span> WireGuard. More info may be found at its website, listed below.

More info: https://www.wireguard.com/
Packages: wireguard wireguard-tools wireguard-dkms

Install with: $ apt install wireguard
More info: https://launchpad.net/~wireguard/+archive/ubuntu/wireguard
Press <span>[</span>ENTER<span>]</span> to <span>continue</span> or Ctrl-c to cancel adding it.

&lt;truncated&gt;

Fetched 18.5 MB in 4s <span>(</span><span>4840</span> kB/s<span>)</span>

ubuntu@ip-172-30-0-233:~$ sudo apt-get update
Hit:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:5 http://ppa.launchpad.net/wireguard/wireguard/ubuntu bionic InRelease
Reading package lists... Done

ubuntu@ip-172-30-0-233:~$ sudo apt-get install wireguard
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:

&lt;truncated&gt;

update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ <span>(</span>c++<span>)</span> in auto mode
Setting up build-essential <span>(</span>12.4ubuntu1<span>)</span> ...
Setting up wireguard-dkms <span>(</span>1.0.20200401-1ubuntu1~18.04<span>)</span> ...
Loading new wireguard-1.0.20200401 DKMS files...
Building <span>for</span> 4.15.0-1057-aws
Building initial module <span>for</span> 4.15.0-1057-aws
Done.

wireguard:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/4.15.0-1057-aws/updates/dkms/

depmod...

DKMS: install completed.
Setting up wireguard <span>(</span>1.0.20200319-0ppa1~18.04<span>)</span> ...
Processing triggers <span>for</span> libc-bin <span>(</span>2.27-3ubuntu1<span>)</span> ...
Processing triggers <span>for</span> man-db <span>(</span>2.8.3-2ubuntu0.1<span>)</span> ...
</code></pre></div><p>Let’s launch a shell as <code>root</code> with <code>sudo -s</code> to avoid having to type sudo every time from now on:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">ubuntu@ip-172-30-0-233:~$ sudo -s
root@ip-172-30-0-233:~#
</code></pre></div><p>Run <code>wg</code> to check if installation was successful which should <em>not</em> output anything if everything is OK:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# wg
root@ip-172-30-0-233:/etc/wireguard/keys#
</code></pre></div><p>The two WireGuard commands we’ll be using are:</p><ul><li><code>wg</code> for configuring WireGuard interfaces.</li><li><code>wg-quick</code> for starting and stopping WireGuard VPN tunnels.</li></ul><h2 id="generating-server-keys">Generating server keys</h2><p>WireGuard configuration files will live under <code>/etc/wireguard/</code> so let’s create a directory named <code>keys</code> there to store the keys we’ll generate:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:~# mkdir /etc/wireguard/keys
</code></pre></div><p>Go into the <code>/etc/wireguard/keys/</code> directory:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:~# <span>cd</span> /etc/wireguard/keys
</code></pre></div><p>Set the directory user mask to <code>077</code> by running <code>umask 077</code>. A umask of 077 allows read, write, and execute permissions for the file’s owner (root in this case), but prohibits read, write, and execute permissions for everyone else and makes sure credentials don’t leak in a race condition:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# <span>umask</span> <span>077</span>
</code></pre></div><p>WireGuard uses asymmetric public/private Curve25519 key pairs for authentication between client and server.</p><p>Use the <code>wg genkey</code> command to generate a private key. We can generate both the private and public key at once by piping the private key output to <code>tee</code> to save it to file but also to forward the private key to <code>wg publickey</code> which derived the public key from a private key and the save it to a file.</p><p>So the command to run is <code>wg genkey | tee privatekey | wg pubkey &gt; publickey</code> to generate the key pair at once:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# wg genkey | tee privatekey | wg pubkey &gt; publickey
</code></pre></div><p>If we do an <code>ls</code> we see there’s a <code>privatekey</code> and <code>publickey</code> file:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# ls
privatekey publickey
</code></pre></div><p>Outputting the contents of the private key file shows us the random key it generated in base64 format:</p><div class="highlight" readability="8"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# cat privatekey
wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M<span>=</span>
</code></pre></div><p>Likewise the public key it derived from the private key is in base64 format:</p><div class="highlight" readability="8"><pre><code class="language-fallback" data-lang="fallback">root@ip-172-30-0-233:/etc/wireguard/keys# cat publickey
H6StMJOYIjfqhDvG9v46DSX9UlQl52hOoUm7F3COxC4=
</code></pre></div><p>We’ll be needing the private key for the WireGuard server configuration, and the public key for the client configuration.</p><h3 id="creating-server-configuration-file">Creating server configuration file</h3><p>Go into the <code>/etc/wireguard/</code> directory and create a new file <code>wg0.conf</code>. WireGuard will create a new network interface named the same as the filename so it’s common convention to denote the first WireGuard network interface as <code>wg0</code> for context:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# touch /etc/wireguard/wg0.conf
</code></pre></div><p>Open up the server configuration file <code>/etc/wireguard/wg0.conf</code> in your favorite editor:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# vim /etc/wireguard/wg0.conf
</code></pre></div><p>Paste the following configuration into the new config file:</p><div class="highlight" readability="7"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>&lt;server private key&gt;</span>
<span>Address</span> <span>=</span> <span>10.0.0.1/24</span>
<span>ListenPort</span> <span>=</span> <span>51820</span>
</code></pre></div><p>The config files are in standard <a href="https://en.wikipedia.org/wiki/INI
file" style="color: inherit; text-decoration: none;" name="readabilityLink-19">INI</a><a href="#readabilityFootnoteLink-19" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[19]</sup></small></a> format.</p><p>Replace the <code>PrivateKey</code> value with the private key content you generated earlier:</p><div class="highlight" readability="8"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=</span>
<span>Address</span> <span>=</span> <span>10.0.0.1/24</span>
<span>ListenPort</span> <span>=</span> <span>51820</span>
</code></pre></div><p>The address <code>10.0.0.1</code> was chosen because it’s an available private subnet on the server. If your server is using that IP range already, then pick a different address like <code>192.168.2.1</code> to avoid conflicts.</p><p>The <code>[Interface]</code> section is for configuration the new WireGuard interface we are creating.</p><ul><li><code>PrivateKey</code> is your server’s private key.</li><li><code>Address</code> is the private network IP address range that we’re assigning to for this network interface.</li><li><code>ListenPort</code> is the host port to run the service on. This port will need to be publicly accessible. The port <code>51820</code> is the default port.</li></ul><p>Make sure to enable the port <code>51820</code> for <code>UDP</code> traffic. If using EC2 then you should allow it under the Security Group for the EC2 instance.</p><p>EC2 instance → Security groups → Click on security group → Edit inbound rules → Add rules → Custom UDP → Port range: 51820 → Source: Anywhere → Save rules</p><p>The rules immediately take effect.</p><p>If your server is behind a NAT (which in our case it is because it’s on EC2 behind a VPC) then all traffic needs to be forwarded from the default interface to the WireGuard interface.</p><p>To find out the name of the default interface run <code>ip route</code>:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# ip route | grep default | awk <span>'{print $5}'</span>
eth0
</code></pre></div><p>Now add forwarding rules for forwarding in the server configuration file using the <code>PostUp</code> and <code>PostDown</code> config settings where <code>PostUp</code> value command is ran when the WireGuard service starts and <code>PostDown</code> value command runs when the service is shutting down.</p><div class="highlight" readability="10"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=</span>
<span>Address</span> <span>=</span> <span>10.0.0.1/24</span>
<span>ListenPort</span> <span>=</span> <span>51820</span>
<span>PostUp</span> <span>=</span> <span>iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</span>
<span>PostDown</span> <span>=</span> <span>iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE</span>
</code></pre></div><p>The three iptable rules are:</p><ul><li><code>iptables -A FORWARD -i %i -j ACCEPT</code> for allowing inbound traffic received by the interface.</li><li><code>iptables -A FORWARD -o %i -j ACCEPT</code> for allowing outbound traffic from the interface.</li><li><code>iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</code> for masking the private IP address of the interface with the external IP address of the default interface.</li></ul><h2 id="enabling-ip-forwarding-on-server">Enabling IP forwarding on server</h2><p>By default IP forwarding is disabled meaning that if the interface receives a packet that wasn’t intended for it then it’ll reject it. Since we need to pass on packets from one interface to another then we need to allow IP forwarding.</p><p>Open up the file <code>/etc/sysctl.conf</code> for editing:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# vim /etc/sysctl.conf
</code></pre></div><p>Allow forwarding of IP packets by uncommenting out the line <code>net.ipv4.ipforward=1</code> near line 28:</p><div class="highlight" readability="7"><pre><code class="language-ini" data-lang="ini"><span># Uncomment the next line to enable packet forwarding for IPv4</span>
<span>net.ipv4.ip
forward</span><span>=</span><span>1</span>
</code></pre></div><p>Run <code>sysctl -p</code> for the changes to take effect without requiring a reboot:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# sysctl -p
net.ipv4.ipforward <span>=</span> <span>1</span>
</code></pre></div><p>Confirm that IP forwarding is enabled by outputting the contents of <code>/proc/sys/net/ipv4/ip
forward</code> which should return <code>1</code>:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# cat /proc/sys/net/ipv4/ipforward
<span>1</span>
</code></pre></div><p>The server is almost fully configured. It’s only now missing information about the client so let’s set up the client next.</p><h2 id="installing-wireguard-on-client">Installing WireGuard on client</h2><p>Jump back to your client machine and install WireGuard. My client machine is running Arch linux but the process will be the same for most linux distros. If you’re running Ubuntu on the client then do the same install steps you did on the server above or look at the official WireGuard <a href="https://www.wireguard.com/install/" style="color: inherit; text-decoration: none;" name="readabilityLink-20">installation instructions</a><a href="#readabilityFootnoteLink-20" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[20]</sup></small></a>.</p><p>Ubuntu WireGuard install instructions:</p><div class="highlight" readability="8"><pre><code class="language-bash" data-lang="bash">$ sudo add-apt-repository ppa:wireguard/wireguard
$ sudo apt-get update
$ sudo apt-get install wireguard
</code></pre></div><p>If running Arch like I am, then these are the WireGuard install instructions:</p><div class="highlight" readability="7"><pre><code class="language-fallback" data-lang="fallback">$ sudo pacman -S wireguard-tools wireguard-dkms
</code></pre></div><p>Let’s launch a shell as <code>root</code> with <code>sudo -s</code> to avoid having to type sudo every time from now on:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">$ sudo -s
<span>[</span>root@archlinux ~<span>]</span>#
</code></pre></div><h2 id="generating-client-keys">Generating client keys</h2><p>The process of generating WireGuard keys on the client is the same as how it’s done on the server. Create the directory <code>/etc/wireguard/keys</code> and set the user mask to <code>077</code>.</p><div class="highlight" readability="8"><pre><code class="language-bash" data-lang="bash"><span>[</span>root@archlinux ~<span>]</span><span># mkdir /etc/wireguard/keys</span>
<span>[</span>root@archlinux ~<span>]</span><span># cd /etc/wireguard/keys</span>
<span>[</span>root@archlinux keys<span>]</span><span># umask 077</span>
</code></pre></div><p>Generate a private and public key pair for the client using the same command as we did on the server:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash"><span>[</span>root@archlinux keys<span>]</span><span># wg genkey | tee privatekey | wg pubkey &gt; publickey</span>
</code></pre></div><p>Output the key contents which we’ll be needing soon in our configuration files:</p><div class="highlight" readability="8"><pre><code class="language-bash" data-lang="bash"><span>[</span>root@archlinux keys<span>]</span><span># cat privatekey</span>
<span>cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c</span><span>=</span>
<span>[</span>root@archlinux keys<span>]</span><span># cat publickey</span>
<span>vi4TCAo8TNRkpf4ZpiMsp3YHaOLrcouSDkrm4wJxezw</span><span>=</span>
</code></pre></div><h2 id="creating-client-configuration-file">Creating client configuration file</h2><p>On the client create the configuration <code>/etc/wireguard/wg0.conf</code>:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash"><span>[</span>root@archlinux keys<span>]</span><span># vim /etc/wireguard/wg0.conf</span>
</code></pre></div><p>Paste the configuration into your client configuration file:</p><div class="highlight" readability="7"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>Address</span> <span>=</span> <span>10.0.0.2/32</span>
<span>PrivateKey</span> <span>=</span> <span>&lt;client private key&gt;</span>
</code></pre></div><p>Replace the <code>PrivateKey</code> value with your client’s private key:</p><div class="highlight" readability="7"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>Address</span> <span>=</span> <span>10.0.0.2/32</span>
<span>PrivateKey</span> <span>=</span> <span>cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=</span>
</code></pre></div><p>Set the DNS to Cloudflare’s public DNS resolver <code>1.1.1.1</code> which is fast and secure:</p><div class="highlight" readability="8"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>Address</span> <span>=</span> <span>10.0.0.2/32</span>
<span>PrivateKey</span> <span>=</span> <span>cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=</span>
<span>DNS</span> <span>=</span> <span>1.1.1.1</span>
</code></pre></div><p>The <code>[Interface]</code> section is for configuration the new WireGuard interface we are creating.</p><ul><li><code>Address</code> is the private network IP address range that we’re assigning to for this network interface.</li><li><code>PrivateKey</code> is your client’s private key.</li><li><code>DNS</code> is the DNS resolve to use.</li></ul><h2 id="setting-server-peer-on-client-config">Setting server peer on client config</h2><p>The next step is to set information about the server in the client configuration file under the <code>[Peer]</code> section:</p><div class="highlight" readability="9"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>Address</span> <span>=</span> <span>10.0.0.2/32</span>
<span>PrivateKey</span> <span>=</span> <span>cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=</span>
<span>DNS</span> <span>=</span> <span>1.1.1.1</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>&lt;server public key&gt;</span>
<span>Endpoint</span> <span>=</span> <span>&lt;server public ip&gt;:51820</span>
<span>AllowedIPs</span> <span>=</span> <span>0.0.0.0/0</span>
</code></pre></div><p>Replace the <code>PublicKey</code> value to your server’s public key and set the <code>Endpoint</code> to be your server’s public IP address:</p><div class="highlight" readability="9"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>Address</span> <span>=</span> <span>10.0.0.2/32</span>
<span>PrivateKey</span> <span>=</span> <span>cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=</span>
<span>DNS</span> <span>=</span> <span>1.1.1.1</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>H6StMJOYIjfqhDvG9v46DSX9UlQl52hOoUm7F3COxC4=</span>
<span>Endpoint</span> <span>=</span> <span>54.225.123.18:51820</span>
<span>AllowedIPs</span> <span>=</span> <span>0.0.0.0/0</span>
</code></pre></div><p>Because our server is behind a NAT, we’ll also need to set <code>PersistentKeepalive</code> to keep the connection alive:</p><div class="highlight" readability="9"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>Address</span> <span>=</span> <span>10.0.0.2/32</span>
<span>PrivateKey</span> <span>=</span> <span>cAqmevIKScn5l4Jg1F69KEIty6gVb8wGNqNlApvzc0c=</span>
<span>DNS</span> <span>=</span> <span>1.1.1.1</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>H6StMJOYIjfqhDvG9v46DSX9UlQl52hOoUm7F3COxC4=</span>
<span>Endpoint</span> <span>=</span> <span>54.225.123.18:51820</span>
<span>AllowedIPs</span> <span>=</span> <span>0.0.0.0/0</span>
<span>PersistentKeepalive</span> <span>=</span> <span>25</span>
</code></pre></div><p>The <code>[Peer]</code> section is for configuration information about the peer it’s connecting to, which in this case it’s the client connection to the server.</p><ul><li><code>PublicKey</code> is the public key of the server.</li><li><code>Endpoint</code> is your server’s public IP and port the server’s interface is listening, configured with <code>ListenPort</code> in the server’s config.</li><li><code>AllowedIPs</code> is the IP range to allow forwarding from. Setting it to <code>0.0.0.0/0</code> will forward all traffic over the tunnel.</li><li><code>PersistentKeepalive</code> is the interval to periodically send keepalive packets to the server.</li></ul><p>If you’re not sure what your server’s public address is, you can do an IP lookup by doing a DNS query request to <code>myip.opendns.com</code>:</p><div class="highlight" readability="8"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# dig +short myip.opendns.com @resolver1.opendns.com
54.225.123.18
</code></pre></div><p>If your server is an EC2 instance, you get query the metadata endpoint to get the public IP address:</p><div class="highlight" readability="8"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# curl http://169.254.169.254/latest/meta-data/public-ipv4
54.225.123.18
</code></pre></div><h2 id="setting-client-peer-on-server-config">Setting client peer on server config</h2><p>Go back into the server and edit the config. We’re going to add information about the client so that the server and client can authenticate with each other.</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# vim /etc/wireguard/wg0.conf
</code></pre></div><p>Add the <code>[Peer]</code> section to the server config:</p><div class="highlight" readability="10"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=</span>
<span>Address</span> <span>=</span> <span>10.0.0.1/24</span>
<span>ListenPort</span> <span>=</span> <span>51820</span>
<span>PostUp</span> <span>=</span> <span>iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</span>
<span>PostDown</span> <span>=</span> <span>iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>&lt;client public key&gt;</span>
<span>AllowedIPs</span> <span>=</span> <span>10.0.0.2/32</span>
</code></pre></div><p>Replace the <code>PublicKey</code> value with your client’s public key:</p><div class="highlight" readability="10"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=</span>
<span>Address</span> <span>=</span> <span>10.0.0.1/24</span>
<span>ListenPort</span> <span>=</span> <span>51820</span>
<span>PostUp</span> <span>=</span> <span>iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</span>
<span>PostDown</span> <span>=</span> <span>iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>vi4TCAo8TNRkpf4ZpiMsp3YHaOLrcouSDkrm4wJxezw=</span>
<span>AllowedIPs</span> <span>=</span> <span>10.0.0.2/32</span>
</code></pre></div><p>The <code>[Peer]</code> section is for configuration information about the peer it’s connecting to, which in this case it’s the servers connection to the client.</p><ul><li><code>PublicKey</code> is the client’s public key.</li><li><code>AllowedIPs</code> are allowed client IP addresses.</li></ul><h2 id="starting-wireguard-service-on-server">Starting WireGuard service on server</h2><p>Now that the server has the client peer information we can start the WireGuard service with <code>wg-quick up wg0</code> on the server:</p><div class="highlight" readability="10"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# wg-quick up wg0
<span>[</span><span>#] ip link add wg0 type wireguard</span>
<span>[</span><span>#] wg setconf wg0 /dev/fd/63</span>
<span>[</span><span>#] ip -4 address add 10.0.0.1/24 dev wg0</span>
<span>[</span><span>#] ip link set mtu 8921 up dev wg0</span>
<span>[</span><span>#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</span>
</code></pre></div><p>Depending on your Ubuntu installation, you might need to install additional kernel modules. If you got the error <em>“RNETLINK answers: Operation not supported”</em> trying to start the wg0 interface then install the following packages on the server:</p><div class="highlight" readability="8"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# apt-get install wireguard-dkms wireguard-tools linux-headers-<span>$(</span>uname -r<span>)</span>
</code></pre></div><p>To start WireGuard across reboots you’ll need to <em>enable</em> the service to add it to the systemd init system by running <code>systemctl enable wg-quick@wg0.service</code>:</p><div class="highlight" readability="9"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# systemctl <span>enable</span> wg-quick@wg0.service
Created symlink /etc/systemd/system/multi-user.target.wants/wg-quick@wg0.service → /lib/systemd/system/wg-quick@.service.
</code></pre></div><p>Check the status by running <code>systemctl status wg-quick@wg0.service</code> and if you see <em>Active: active (exited)</em> then if everything is good so far:</p><div class="highlight" readability="12"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# systemctl status wg-quick@wg0.service
● wg-quick@wg0.service - WireGuard via wg-quick<span>(</span>8<span>)</span> <span>for</span> wg0
Loaded: loaded <span>(</span>/lib/systemd/system/wg-quick@.service; indirect; vendor preset: enabled<span>)</span>
Active: active <span>(</span>exited<span>)</span> since Thu 2020-04-02 06:35:22 UTC; 1s ago
Docs: man:wg-quick<span>(</span>8<span>)</span>
man:wg<span>(</span>8<span>)</span>
https://www.wireguard.com/
https://www.wireguard.com/quickstart/
https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8
https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8
Process: <span>10730</span> <span>ExecStart</span><span>=</span>/usr/bin/wg-quick up wg0 <span>(</span><span>code</span><span>=</span>exited, <span>status</span><span>=</span>0/SUCCESS<span>)</span>
Main PID: <span>10730</span> <span>(</span><span>code</span><span>=</span>exited, <span>status</span><span>=</span>0/SUCCESS<span>)</span>

Apr <span>02</span> 06:35:21 ip-172-30-0-233 systemd<span>[</span>1<span>]</span>: Starting WireGuard via wg-quick<span>(</span>8<span>)</span> <span>for</span> wg0...
Apr <span>02</span> 06:35:22 ip-172-30-0-233 wg-quick<span>[</span>10730<span>]</span>: <span>[</span><span>#] ip link add wg0 type wireguard</span>
Apr <span>02</span> 06:35:22 ip-172-30-0-233 wg-quick<span>[</span>10730<span>]</span>: <span>[</span><span>#] wg setconf wg0 /dev/fd/63</span>
Apr <span>02</span> 06:35:22 ip-172-30-0-233 wg-quick<span>[</span>10730<span>]</span>: <span>[</span><span>#] ip -4 address add 10.0.0.1/24 dev wg0</span>
Apr <span>02</span> 06:35:22 ip-172-30-0-233 wg-quick<span>[</span>10730<span>]</span>: <span>[</span><span>#] ip link set mtu 8921 up dev wg0</span>
Apr <span>02</span> 06:35:22 ip-172-30-0-233 wg-quick<span>[</span>10730<span>]</span>: <span>[</span><span>#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; ipt</span>
Apr <span>02</span> 06:35:22 ip-172-30-0-233 systemd<span>[</span>1<span>]</span>: Started WireGuard via wg-quick<span>(</span>8<span>)</span> <span>for</span> wg0.
</code></pre></div><p>Verify that new iproute rules have been applied with <code>iptables -L -n</code>:</p><div class="highlight" readability="10"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# iptables -L -n
Chain INPUT <span>(</span>policy ACCEPT<span>)</span>
target prot opt <span>source</span> destination

Chain FORWARD <span>(</span>policy ACCEPT<span>)</span>
target prot opt <span>source</span> destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

Chain OUTPUT <span>(</span>policy ACCEPT<span>)</span>
target prot opt <span>source</span> destination
</code></pre></div><p>Running the command <code>ifconfig</code> shows the new network interface <code>wg0</code> with the internal IP address we specified <code>10.0.0.1</code>:</p><div class="highlight" readability="18"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# ifconfig
eth0: <span>flags</span><span>=</span>4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt; mtu <span>9001</span>
inet 172.30.0.233 netmask 255.255.255.0 broadcast 172.30.0.255
inet6 fe80::1097:5bff:fe30:d57 prefixlen <span>64</span> scopeid 0x20&lt;link&gt;
ether 12:97:5b:30:0d:57 txqueuelen <span>1000</span> <span>(</span>Ethernet<span>)</span>
RX packets <span>1151269</span> bytes <span>524679242</span> <span>(</span>524.6 MB<span>)</span>
RX errors <span>0</span> dropped <span>0</span> overruns <span>0</span> frame <span>0</span>
TX packets <span>1022229</span> bytes <span>345390292</span> <span>(</span>345.3 MB<span>)</span>
TX errors <span>0</span> dropped <span>0</span> overruns <span>0</span> carrier <span>0</span> collisions <span>0</span>

lo: <span>flags</span><span>=</span>73&lt;UP,LOOPBACK,RUNNING&gt; mtu <span>65536</span>
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen <span>128</span> scopeid 0x10&lt;host&gt;
loop txqueuelen <span>1000</span> <span>(</span>Local Loopback<span>)</span>
RX packets <span>526</span> bytes <span>48787</span> <span>(</span>48.7 KB<span>)</span>
RX errors <span>0</span> dropped <span>0</span> overruns <span>0</span> frame <span>0</span>
TX packets <span>526</span> bytes <span>48787</span> <span>(</span>48.7 KB<span>)</span>
TX errors <span>0</span> dropped <span>0</span> overruns <span>0</span> carrier <span>0</span> collisions <span>0</span>

wg0: <span>flags</span><span>=</span>209&lt;UP,POINTOPOINT,RUNNING,NOARP&gt; mtu <span>8921</span>
inet 10.0.0.1 netmask 255.255.255.0 destination 10.0.0.1
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen <span>1000</span> <span>(</span>UNSPEC<span>)</span>
RX packets <span>0</span> bytes <span>0</span> <span>(</span>0.0 B<span>)</span>
RX errors <span>0</span> dropped <span>0</span> overruns <span>0</span> frame <span>0</span>
TX packets <span>0</span> bytes <span>0</span> <span>(</span>0.0 B<span>)</span>
TX errors <span>0</span> dropped <span>0</span> overruns <span>0</span> carrier <span>0</span> collisions <span>0</span>
</code></pre></div><h2 id="starting-wireguard-service-on-client">Starting WireGuard service on client</h2><p>First take note of your current public IP address on the client machine:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash"><span>[</span>root@archlinux wireguard<span>]</span><span># dig +short myip.opendns.com @resolver1.opendns.com</span>
65.88.88.4
</code></pre></div><p>After we start the WireGuard service on the client then the public IP address will be resolved to the server’s public IP address.</p><p>Start the WireGuard service using <code>wg-quick</code> just like we did previously on the server:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash"><span>[</span>root@archlinux wireguard<span>]</span><span># wg-quick up wg0</span>
</code></pre></div><p>Now that WireGuard is running, check the public IP address again of the client and it should now be the public IP address of the server:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash"><span>[</span>root@archlinux wireguard<span>]</span><span># dig +short myip.opendns.com @resolver1.opendns.com</span>
54.225.123.18
</code></pre></div><p>Success! WireGuard is correctly configured and the peers are connected.</p><h2 id="connecting-a-mobile-client-to-server">Connecting a mobile client to server</h2><p>Download the WireGuard app for <a href="https://apps.apple.com/us/app/wireguard/id1441195209" style="color: inherit; text-decoration: none;" name="readabilityLink-21">iOS</a><a href="#readabilityFootnoteLink-21" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[21]</sup></small></a> or <a href="https://play.google.com/store/apps/details?id=com.wireguard.android&amp;hl=en
US" style="color: inherit; text-decoration: none;" name="readabilityLink-22">Android</a><a href="#readabilityFootnoteLink-22" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[22]</sup></small></a> on your device.</p><p>For this example we’ll create a second client (an iPhone) to connect to the WireGuard server. The same steps will need to be followed from when we setup the first client.</p><p>We can generate keys directly on the device and set up the configuration manually but that’s not quick and ideal. Instead we can generate the keys and configuration on the server and then securely transfer the information into the WireGuard app.</p><p>Run <code>wg genkey</code> but specify different filenames this time to distinguish them from the server keys:</p><div class="highlight" readability="10"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# <span>cd</span> /etc/wireguard/keys
root@ip-172-30-0-233:/etc/wireguard/keys# wg genkey | tee iphoneprivatekey | wg pubkey &gt; iphonepublickey
root@ip-172-30-0-233:/etc/wireguard/keys# cat iphoneprivatekey
kFnMqMSiAluwb/xWgemXhjLh/II/sb92OoYCbh7yaWw<span>=</span>
root@ip-172-30-0-233:/etc/wireguard/keys# cat iphone
publickey
cKIxzfp5ESpdM34vT2Qk/S7yvprOff6Le4YnyOTI4B8<span>=</span>
</code></pre></div><p>Open up the server config <code>/etc/wireguard/wg0.conf</code> in your favorite editor:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# vim /etc/wireguard/wg0.conf
</code></pre></div><p>Add the second peer section and include the client’s public key and IP address:</p><div class="highlight" readability="10"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=</span>
<span>Address</span> <span>=</span> <span>10.0.0.1/24</span>
<span>ListenPort</span> <span>=</span> <span>51820</span>
<span>PostUp</span> <span>=</span> <span>iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</span>
<span>PostDown</span> <span>=</span> <span>iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE</span>

<span>[Peer]</span>
<span>PrivateKey</span> <span>=</span> <span>wIObajifv6U2emcZsAGNZbbWzkyrs84EEyr+bgmlB3M=</span>
<span>AllowedIPs</span> <span>=</span> <span>10.0.0.2/32</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>cKIxzfp5ESpdM34vT2Qk/S7yvprOff6Le4YnyOTI4B8=</span>
<span>AllowedIPs</span> <span>=</span> <span>10.0.0.3/32</span>
</code></pre></div><p>Create a new configuration file for the iPhone client on the server. We’ll name it <code>wgo-iphone.conf</code>:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard/keys# vim /etc/wireguard/wg0-iphone.conf
</code></pre></div><p>Paste client configuration but remember to use a different private IP that differs from the first client.</p><div class="highlight" readability="9"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>kFnMqMSiAluwb/xWgemXhjLh/II/sb92OoYCbh7yaWw=</span>
<span>Address</span> <span>=</span> <span>10.0.0.3/32</span>
<span>DNS</span> <span>=</span> <span>1.1.1.1</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>H6StMJOYIjfqhDvG9v46DSX9UlQl52hOoUm7F3COxC4=</span>
<span>Endpoint</span> <span>=</span> <span>54.225.123.18:51820</span>
<span>AllowedIPs</span> <span>=</span> <span>0.0.0.0/0</span>
<span>PersistentKeepalive</span> <span>=</span> <span>25</span>
</code></pre></div><p>Install <a href="https://github.com/fukuchi/libqrencode" style="color: inherit; text-decoration: none;" name="readabilityLink-23">qrencode</a><a href="#readabilityFootnoteLink-23" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[23]</sup></small></a> on the server to generate a QRCode from the configuration file.</p><p>You’ll be scanning this qrcode in the WireGuard app to download the configuration. This is a safer way to transport credentials since the keys and configuration files don’t need to be zipped and moved.</p><p>Generate the text based qrcode image in your terminal with <code>qrencode -t ansiutf8 &lt; wg0-iphone.conf</code></p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">root@ip-172-30-0-233:/etc/wireguard# qrencode -t ansiutf8 &lt; wg0-iphone.conf
&lt;qrcode image&gt;
</code></pre></div><p>In the WireGuard app go to: Add a tunnel → Create from QRCode</p><p>Scan the qrcode code generated in the terminal and make sure to <em>Allow</em> the VPN configuration in the settings popup.</p><p>Enable the VPN by toggling on the switch.</p><p>Visit <a href="https://ipchicken.com/" style="color: inherit; text-decoration: none;" name="readabilityLink-24">ipchicken.com</a><a href="#readabilityFootnoteLink-24" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[24]</sup></small></a> in the browser to verify the public iP address has changed.</p><h2 id="generating-vanity-addresses">Generating vanity addresses</h2><p>It’s easy to lose track of which keys belong to which devices since they all look like random strings. To make it easier to associate keys to devices you can use this <a href="https://github.com/warner/wireguard-vanity-address" style="color: inherit; text-decoration: none;" name="readabilityLink-25">vanity address generator</a><a href="#readabilityFootnoteLink-25" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[25]</sup></small></a> to generate public keys that contain a custom array of characters.</p><p>For example, we’ll generate a key pair where the public key starts with “iPho” to denote that it’s a key pair to be used on the iPhone client.</p><p>First install the vanity address generator with <a href="https://crates.io/" style="color: inherit; text-decoration: none;" name="readabilityLink-26">cargo</a><a href="#readabilityFootnoteLink-26" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[26]</sup></small></a>:</p><div class="highlight" readability="7"><pre><code class="language-bash" data-lang="bash">$ cargo install wireguard-vanity-address
</code></pre></div><p>Now just specify the list of characters that you want in the public key base64 output:</p><div class="highlight" readability="13"><pre><code class="language-bash" data-lang="bash">$ wireguard-vanity-address ipho
searching <span>for</span> <span>'ipho'</span> in pubkey<span>[</span>0..10<span>]</span>, one of every <span>149796</span> keys should match
one trial takes 83.6 us, CPU cores available: <span>8</span>
est yield: 1.6 seconds per key, 638.83e-3 keys/s
hit Ctrl-C to stop
private YPpudjAoVCnaPUJdcEVhj5Ttedq7WP1ozL+ZdtuTC1g<span>=</span> public <span>cHklbipHoMS9CA8XlRdKMBOOIfQC28Ut8SVyYsqmox0</span><span>=</span>
private <span>kD6FSIZehv1DKJ28MKJQcmSDdd69U3s4s11ymtP1Ekc</span><span>=</span> public iPHoaaQye7+OJNq/TfOvXjMr99pq9ADDDlGynRQ6KQ8<span>=</span>
private <span>aEJ33LXCeipouhiAoQjfMjtwrHPfZDvKLguE8XlawnY</span><span>=</span> public iPHoEoUy4WgkUXr4e47IkA06IZqVI/AqHNS2RZlGhHM<span>=</span>
^C
</code></pre></div><p>It’ll keep generating until you manually stop it when you see a key pair that you like.</p><h2 id="automation">Automation</h2><p>A nice tool to automate the process of setting up a WireGuard VPN is <a href="https://github.com/trailofbits/algo" style="color: inherit; text-decoration: none;" name="readabilityLink-27">Algo</a><a href="#readabilityFootnoteLink-27" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[27]</sup></small></a>.</p><p>Algo is a set of <a href="https://github.com/ansible/ansible" style="color: inherit; text-decoration: none;" name="readabilityLink-28">Ansible</a><a href="#readabilityFootnoteLink-28" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[28]</sup></small></a> scripts to help you set up and configure WireGuard on the remote server from your local machine.</p><p>To get started, clone the algo repository and install the python dependencies:</p><div class="highlight" readability="9"><pre><code class="language-bash" data-lang="bash">~$ git clone https://github.com/trailofbits/algo.git
~$ <span>cd</span> algo/
~/algo$ pip install virtualenv
~/algo$ python3 -m virtualenv --python<span>=</span><span>"</span><span>$(</span><span>command</span> -v python3<span>)</span><span>"</span> .env <span>&amp;&amp;</span> <span>source</span> .env/bin/activate <span>&amp;&amp;</span> python3 -m pip install -U pip virtualenv <span>&amp;&amp;</span> python3 -m pip install -r requirements.txt
</code></pre></div><p>Now run the algo executable file to start the walkthough of deploying an Algo server to the cloud:</p><div class="highlight" readability="10"><pre><code class="language-bash" data-lang="bash"><span>(</span>.env<span>)</span> ~/algo$ ./algo
<span>[</span>Cloud prompt<span>]</span>
What provider would you like to use?
1. DigitalOcean
2. Amazon Lightsail
3. Amazon EC2
4. Microsoft Azure
5. Google Compute Engine
6. Hetzner Cloud
7. Vultr
8. Scaleway
9. OpenStack <span>(</span>DreamCompute optimised<span>)</span>
10. CloudStack <span>(</span>Exoscale optimised<span>)</span>
11. Install to existing Ubuntu 18.04 or 19.10 server <span>(</span><span>for</span> more advanced users<span>)</span>

Enter the number of your desired provider
:
&lt;truncated&gt;
</code></pre></div><h2 id="tldr">TLDR;</h2><p>Here’s a summary of the server and client configuration and commands used in this post:</p><h3 id="server">Server</h3><p>Server commands:</p><div class="highlight" readability="10"><pre><code class="language-bash" data-lang="bash">sudo -s
apt-get install wireguard
mkdir -p /etc/wireguard/keys
<span>cd</span> /etc/wireguard/keys
<span>umask</span> <span>077</span>
wg genkey | tee privatekey | wg pubkey &gt; publickey
vim /etc/wireguard/wg0.conf <span># see server config below</span>
vim /etc/sysctl.conf <span># uncomment line "net.ipv4.ipforward=1"</span>
sysctl -p
wg-quick up wg0
systemctl <span>enable</span> wg-quick@wg0.service
</code></pre></div><p>Server config <code>/etc/wireguard/wg0.conf</code>:</p><div class="highlight" readability="10"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>&lt;server private key&gt;</span>
<span>Address</span> <span>=</span> <span>10.0.0.1/24</span>
<span>ListenPort</span> <span>=</span> <span>51820</span>
<span>PostUp</span> <span>=</span> <span>iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE</span>
<span>PostDown</span> <span>=</span> <span>iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>&lt;client public key&gt;</span>
<span>AllowedIPs</span> <span>=</span> <span>10.0.0.2/32</span>
</code></pre></div><h3 id="client">Client</h3><p>Client commands:</p><div class="highlight" readability="9"><pre><code class="language-bash" data-lang="bash">sudo -s
apt-get install wireguard
mkdir -p /etc/wireguard/keys
<span>cd</span> /etc/wireguard/keys
<span>umask</span> <span>077</span>
wg genkey | tee privatekey | wg pubkey &gt; publickey
vim /etc/wireguard/wg0.conf <span># see client config below</span>
wg-quick up wg0
</code></pre></div><p>Client config <code>/etc/wireguard/wg0.conf</code>:</p><div class="highlight" readability="9"><pre><code class="language-ini" data-lang="ini"><span>[Interface]</span>
<span>PrivateKey</span> <span>=</span> <span>&lt;client private key&gt;</span>
<span>Address</span> <span>=</span> <span>10.0.0.2/32</span>
<span>DNS</span> <span>=</span> <span>1.1.1.1</span>

<span>[Peer]</span>
<span>PublicKey</span> <span>=</span> <span>&lt;server public key&gt;</span>
<span>Endpoint</span> <span>=</span> <span>&lt;server public ip&gt;:51820</span>
<span>AllowedIPs</span> <span>=</span> <span>0.0.0.0/0</span>
<span>PersistentKeepalive</span> <span>=</span> <span>25</span>
</code></pre></div><h2 id="resources">Resources</h2><p>Follow discussion on <a href="https://news.ycombinator.com/item?id=22788584" style="color: inherit; text-decoration: none;" name="readabilityLink-29">Hacker News</a><a href="#readabilityFootnoteLink-29" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[29]</sup></small></a>.</p></div><div id="readability-footnotes"><h3>References</h3><ol id="readability-footnotes-list"><li><small><sup><a href="#readabilityLink-1" title="Jump to Link in Article">^</a></sup></small> <a href="https://www.wireguard.com/" name="readabilityFootnoteLink-1">WireGuard</a><small> (www.wireguard.com)</small></li><li><small><sup><a href="#readabilityLink-2" title="Jump to Link in Article">^</a></sup></small> <a href="https://homepage.cs.uri.edu/~thenry/resources/unix
art/ch01s06.html" name="readabilityFootnoteLink-2">Unix Philosophy</a><small> (homepage.cs.uri.edu)</small></li><li><small><sup><a href="#readabilityLink-3" title="Jump to Link in Article">^</a></sup></small> <a href="https://github.com/OpenVPN/openvpn" name="readabilityFootnoteLink-3">OpenVPN</a><small> (github.com)</small></li><li><small><sup><a href="#readabilityLink-4" title="Jump to Link in Article">^</a></sup></small> <a href="https://en.wikipedia.org/wiki/WireGuard#Protocol" name="readabilityFootnoteLink-4">cryptographic standards</a><small> (en.wikipedia.org)</small></li><li><small><sup><a href="#readabilityLink-5" title="Jump to Link in Article">^</a></sup></small> <a href="#tldr" name="readabilityFootnoteLink-5">TLDR;</a></li><li><small><sup><a href="#readabilityLink-6" title="Jump to Link in Article">^</a></sup></small> <a href="#setting-up-a-server" name="readabilityFootnoteLink-6">Setting up a server</a></li><li><small><sup><a href="#readabilityLink-7" title="Jump to Link in Article">^</a></sup></small> <a href="#installing-wireguard-on-server" name="readabilityFootnoteLink-7">Installing WireGuard on server</a></li><li><small><sup><a href="#readabilityLink-8" title="Jump to Link in Article">^</a></sup></small> <a href="#generating-server-keys" name="readabilityFootnoteLink-8">Generating server keys</a></li><li><small><sup><a href="#readabilityLink-9" title="Jump to Link in Article">^</a></sup></small> <a href="#creating-server-configuration-file" name="readabilityFootnoteLink-9">Creating server configuration file</a></li><li><small><sup><a href="#readabilityLink-10" title="Jump to Link in Article">^</a></sup></small> <a href="#enabling-ip-forwarding-on-server" name="readabilityFootnoteLink-10">Enabling IP forwarding on server</a></li><li><small><sup><a href="#readabilityLink-11" title="Jump to Link in Article">^</a></sup></small> <a href="#installing-wireguard-on-client" name="readabilityFootnoteLink-11">Installing WireGuard on client</a></li><li><small><sup><a href="#readabilityLink-12" title="Jump to Link in Article">^</a></sup></small> <a href="#generating-client-keys" name="readabilityFootnoteLink-12">Generating client keys</a></li><li><small><sup><a href="#readabilityLink-13" title="Jump to Link in Article">^</a></sup></small> <a href="#creating-client-configuration-file" name="readabilityFootnoteLink-13">Creating client configuration file</a></li><li><small><sup><a href="#readabilityLink-14" title="Jump to Link in Article">^</a></sup></small> <a href="#setting-client-info-on-server-config" name="readabilityFootnoteLink-14">Setting client info on server config</a></li><li><small><sup><a href="#readabilityLink-15" title="Jump to Link in Article">^</a></sup></small> <a href="#starting-wireguard-service-on-server" name="readabilityFootnoteLink-15">Starting WireGuard service on server</a></li><li><small><sup><a href="#readabilityLink-16" title="Jump to Link in Article">^</a></sup></small> <a href="#starting-wireguard-service-on-client" name="readabilityFootnoteLink-16">Starting WireGuard service on client</a></li><li><small><sup><a href="#readabilityLink-17" title="Jump to Link in Article">^</a></sup></small> <a href="#connecting-a-mobile-client-to-server" name="readabilityFootnoteLink-17">Connecting a mobile client to server</a></li><li><small><sup><a href="#readabilityLink-18" title="Jump to Link in Article">^</a></sup></small> <a href="https://www.wireguard.com/install/" name="readabilityFootnoteLink-18">installation instructions</a><small> (www.wireguard.com)</small></li><li><small><sup><a href="#readabilityLink-19" title="Jump to Link in Article">^</a></sup></small> <a href="https://en.wikipedia.org/wiki/INIfile" name="readabilityFootnoteLink-19">INI</a><small> (en.wikipedia.org)</small></li><li><small><sup><a href="#readabilityLink-20" title="Jump to Link in Article">^</a></sup></small> <a href="https://www.wireguard.com/install/" name="readabilityFootnoteLink-20">installation instructions</a><small> (www.wireguard.com)</small></li><li><small><sup><a href="#readabilityLink-21" title="Jump to Link in Article">^</a></sup></small> <a href="https://apps.apple.com/us/app/wireguard/id1441195209" name="readabilityFootnoteLink-21">iOS</a><small> (apps.apple.com)</small></li><li><small><sup><a href="#readabilityLink-22" title="Jump to Link in Article">^</a></sup></small> <a href="https://play.google.com/store/apps/details?id=com.wireguard.android&amp;hl=enUS" name="readabilityFootnoteLink-22">Android</a><small> (play.google.com)</small></li><li><small><sup><a href="#readabilityLink-23" title="Jump to Link in Article">^</a></sup></small> <a href="https://github.com/fukuchi/libqrencode" name="readabilityFootnoteLink-23">qrencode</a><small> (github.com)</small></li><li><small><sup><a href="#readabilityLink-24" title="Jump to Link in Article">^</a></sup></small> <a href="https://ipchicken.com/" name="readabilityFootnoteLink-24">ipchicken.com</a><small> (ipchicken.com)</small></li><li><small><sup><a href="#readabilityLink-25" title="Jump to Link in Article">^</a></sup></small> <a href="https://github.com/warner/wireguard-vanity-address" name="readabilityFootnoteLink-25">vanity address generator</a><small> (github.com)</small></li><li><small><sup><a href="#readabilityLink-26" title="Jump to Link in Article">^</a></sup></small> <a href="https://crates.io/" name="readabilityFootnoteLink-26">cargo</a><small> (crates.io)</small></li><li><small><sup><a href="#readabilityLink-27" title="Jump to Link in Article">^</a></sup></small> <a href="https://github.com/trailofbits/algo" name="readabilityFootnoteLink-27">Algo</a><small> (github.com)</small></li><li><small><sup><a href="#readabilityLink-28" title="Jump to Link in Article">^</a></sup></small> <a href="https://github.com/ansible/ansible" name="readabilityFootnoteLink-28">Ansible</a><small> (github.com)</small></li><li><small><sup><a href="#readabilityLink-29" title="Jump to Link in Article">^</a></sup></small> <a href="https://news.ycombinator.com/item?id=22788584" name="readabilityFootnoteLink-29">Hacker News</a><small> (news.ycombinator.com)</small></li></ol></div>

keywords miguel mota,web developer,software developer,html5 developer,javascript developer,blockchain developer

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Linx
Search Linx by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
You want to be the best, you MUST put the long yards in! Nothing comes easy in life so stop wishing and start DOING! So many people would rather bitch and moan than help themselves. Dont be one of those negative drainers, start today, make a small change and keep going forwards with this attitude!
Unknown
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.