Okay, I lied. WireGuard won’t just run on Linux for the server side but that is what it was originally designed for. Linux is the first class citizen as the WireGuard implementation there exists within the kernel.
I also lied about the clients – it’ll work on nearly any OS. We have been using OpenVPN with great success with many customers for years. We have our own management software and my macOS Viscosity client (highly recommended) has over 30 endpoints at this time. For various reasons, we use tap interfaces which just do not work for iOS.
I came across WireGuard a while ago and was intrigued by some of it’s design principles. Specifically:
- UDP only (I remain, to this day, completely bewildered and baffled by any VPN running over TCP – yes, Mikrotik, I’m looking at your OpenVPN implementation);
- how it presents as a simple network interface (and thus is configured via the normal iproute2 tools such as ip); and
- its ssh-like public/private key exchange mechanism.
But I turned away as it stated that it was still a work in progress. It still states this but it looks pretty mature. Two gaps I have with OpenVPN right now seem to be filled by WireGuard: simple just works client for Apple iOS; and easy set-up mechanism for small deployments (e.g. I just want to get remote access to my home server without setting up a certificate authority or using static keys).
So, let’s look at setting up a server (Linux) / client (iOS) with WireGuard. As usual, I’m running the latest Ubuntu LTS on my server – in this case 18.04.
Important note about VPNs and dual-stack networks: many VPNs only work on IPv4. When using such VPNs on a foreign network with IPv6 support, you will only be protected for traffic that transit the IPv4 VPN. Any traffic that works over IPv6 will not go through your VPN – and today, this is a good chunk of traffic. The configuration below assumes your server is dual-stacked – which, today, it should be.
Note also in the examples below, I am using Google’s public DNS. You should install your own DNS resolver on your VPN server rather than using a third party one.
As WireGuard routes packets to and from its encrypted interface, you will need to ensure packet forward is enabled on your server:
sysctl net.ipv4.ip_forward=1 sysctl net.ipv6.conf.all.forwarding=1
Make this permanent by editing
Install WireGuard using its PPA via:
add-apt-repository ppa:wireguard/wireguard apt-get update apt-get install wireguard
WireGuard uses DKMS to build the module for the kernel you are running. It would be useful to do a dist-upgrade and reboot before installing this to put yourself on the latest kernel.
The installation of WireGuard above will install and build the kernel module, install the tools and create the
/etc/wireguard directory. Let’s go there now and create keys for the server:
cd /etc/wireguard # create a private server key: wg genkey >server-private.key chmod go-rwx server-private.key # and create a public key from the private key: cat server-private.key | wg pubkey >server-public.key
We may as well get ahead of ourselves and generate a key pair for our iOS client now also. When we’ve generated the configuration for the server and client, we can delete these key files from the server. In fact you should do this.
wg genkey >client1-private.key cat client1-private.key | wg pubkey >client1-public.key
Now let’s create the server side configuration in
[Interface] Address = 10.97.98.1/24, fd80:10:97:98::1/64 SaveConfig = false DNS = 22.214.171.124, 2a00:1450:400b:c01::8b ListenPort = 51820 PrivateKey = <contents of server-private.key> # client1 [Peer] PublicKey = <contents of client1-public.key> AllowedIPs = 10.97.98.2/32, fd80:10:97:98::2/64
chmod go-rwx wg0.conf.
The IPv6 addresses chosen above are unique local addresses (rfc4193) – similar to RFC1918 private addresses in IPv4. When choosing your IPv6 ULA, use a prefix generator such as this one. As we are using ULA addresses, we have to NAT IPv6. I hate doing this but it makes the example simple. If you have routable IPv6 addresses, try and use a real prefix without NAT.
You can now bring the tunnel up and down using the useful utility commands:
wg-quick up wg0 and
wg-quick down wg0. But you’ll probably want to enable them on systemd for auto-start on system boot:
systemctl enable wg-quick@wg0 systemctl start wg-quick@wg0
When up and running, you can examine the interface with
ifconfig wg0 and see the state of clients with just
# ifconfig wg0 wg0: flags=209<UP,POINTOPOINT,RUNNING,NOARP> mtu 1420 inet 10.97.98.1 netmask 255.255.255.0 destination 10.97.98.1 inet6 fd80:10:97:98::1 prefixlen 64 scopeid 0x0<global> unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 1000 (UNSPEC) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# wg interface: wg0 public key: w29jZeurXAcABTTvA0V5pIOgK8jUZuYxNE9dCciN7Q8= private key: (hidden) listening port: 51821 peer: WrZzlF0fjMWKFqn/krqPrdyfYnlshLMwDNNiweEocRE= allowed ips: 10.97.98.2/32, fd80:10:97:98::2/128
WireGuard has an iOS client – download it from the AppStore here. One of its most useful features is the ability to add a configuration via a QR code (you will need to
apt install qrencode on your server). Let’s create a client configuration in a text file on the server now:
[Interface] PrivateKey = <contents of client1-private.key> Address = 10.97.98.2/24, fd80:10:97:98::2/64 DNS = 126.96.36.199, 2a00:1450:400b:c01::8b [Peer] PublicKey = <contents of server-public.key> Endpoint = <server-ip/hostname>:51820 AllowedIPs = 0.0.0.0/0, ::/0
Then generate the qrcode and display to screen with:
qrencode -t ansiutf8 <client.conf. You’ll be able to import it by pointing your phone at the screen. Sample QR code:
There’s still a couple things you need to do to make this all work: allow UDP packets in your firewall and allow the forwarding and NATing of tunnelled traffic between the tunnel interface and the public internet facing interface(s). I don’t like to over-prescribe how to do this as there are different ways and different topologies. But let me give a basic example.
Start with allowing WireGuard traffic in your firewall – you need an iptables rules such as:
iptables -A INPUT -p udp --dport 51820 -j ACCEPT ip6tables -A INPUT -p udp --dport 51820 -j ACCEPT
For forwarding traffic, there are a number of options but the easiest is to use stateful rules to allow established / related traffic and assume everything coming in your encrypted tunnelled WireGuard interfaces is okay:
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A FORWARD -i wg+ -j ACCEPT ip6tables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT ip6tables -A FORWARD -i wg+ -j ACCEPT
Lastly, for NAT – and assuming eth0 is your public interface, use:
iptables -t nat -A POSTROUTING -o eth+ -s 10.97.98.0/24 -j MASQUERADE ip6tables -t nat -A POSTROUTING -o eth+ -s fd80:10:97:98::/64 -j MASQUERADE
You can add more peers by editing
/etc/wireguard/wg0.conf and then restarting the tunnel interface via
systemctl restart wg-guard@wg0. This will briefly disrupt existing tunnel traffic but it’s the simplest method. There are ways to add new tunnels on the command line but you need to remember to keep the configuration file in sync.