Configure a WireGuard peer

WireGuard fits in a few lines of configuration. Here is what each one means, and how to hand a configuration over to a new user.

How it works: peers that recognize each other by their key

WireGuard is a deliberately minimal VPN. Each machine on the network, called a peer ([Peer] in the configuration), has an X25519 (Curve25519) key pair: a private key that never leaves the machine, and a public key that identifies it to the others. There are no certificates, usernames or passwords: a peer is recognized by its public key, and nothing else.

Technically, all peers are equal. In practice, the network is often organized around a central peer reachable over the Internet (the “server”), to which users' computers and phones connect. Traffic travels over UDP, on port 51820 by convention.

Generate the keys

The wg command-line tool, which comes with WireGuard, creates the keys:

# Files created will be readable by you only
umask 077

# Private key, then the public key derived from it
wg genkey | tee client.key | wg pubkey > client.pub

# Pre-shared key (optional)
wg genpsk > client.psk

Each key is 32 bytes long and written in base64, as 44 characters ending with =. The public key is computed from the private key; the reverse is impossible. The kit's WireGuard key generator does the same job in your browser and also prepares the peer's complete configuration file.

A peer's configuration, line by line

Here is the wg0.conf file of a user joining a network whose server has the internal address 10.8.0.1:

[Interface]
PrivateKey = <user private key>
Address = 10.8.0.2/32
DNS = 10.8.0.1

[Peer]
PublicKey = <server public key>
PresharedKey = <pre-shared key>
Endpoint = vpn.example.com:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25
  • [Interface] describes the machine itself: its PrivateKey, its address in the tunnel (Address) and, optionally, the DNS server to use while connected. Address and DNS are read by wg-quick and by the apps, not by the wg tool alone.
  • [Peer] describes the other end: its PublicKey, the optional pre-shared key, and the Endpoint, the address and port where it can be reached.
  • PersistentKeepalive = 25 sends a small packet every 25 seconds. It keeps the connection open when the user is behind a router or firewall that forgets idle UDP exchanges.

AllowedIPs, the line that matters

AllowedIPs plays two roles at once, which WireGuard calls “cryptokey routing”:

  • Outbound, it is a routing table: traffic to these addresses goes into the tunnel, to this peer.
  • Inbound, it is a filter: a packet received from this peer is accepted only if its source address is in the list.

On the user side, two typical settings:

  • AllowedIPs = 10.8.0.0/24: only traffic to the internal network goes through the VPN; the rest of the browsing goes out directly (split tunnel).
  • AllowedIPs = 0.0.0.0/0, ::/0: all traffic goes through the VPN. wg-quick and the apps then adjust the routes so that the connection to the server itself keeps working. The server, for its part, must have IP forwarding enabled (net.ipv4.ip_forward = 1) and address translation (NAT) to the Internet.

On the server side, each user is a [Peer] block whose AllowedIPs is limited to their own address:

[Interface]
PrivateKey = <server private key>
Address = 10.8.0.1/24
ListenPort = 51820

[Peer]
# Alice
PublicKey = <Alice's public key>
PresharedKey = <Alice's pre-shared key>
AllowedIPs = 10.8.0.2/32

Each peer gets a unique address: an address assigned to a second peer is taken away from the first, which then loses its connection.

The pre-shared key

The PresharedKey is a 256-bit symmetric key, identical on both sides of a given relationship (a server and a user), and different for each user. It is mixed into the X25519 key exchange: an attacker would have to break both. The WireGuard documentation presents it as an additional layer of resistance against future quantum computers. It is optional, costs nothing, and should be passed on as discreetly as the private key.

Install and start

On Linux, place the file in /etc/wireguard/wg0.conf, then:

# Bring the tunnel up
sudo wg-quick up wg0

# Start it automatically at boot
sudo systemctl enable --now wg-quick@wg0

# Check: the “latest handshake” line proves the peers are talking
sudo wg show

On Windows and macOS, the WireGuard app imports the file as is. On iOS and Android, the app imports a file or scans a QR code, which you can produce for example with qrencode -t ansiutf8 < wg0.conf. To add a user to the server without disconnecting the others, update the server configuration, then apply it with wg syncconf wg0 <(wg-quick strip wg0) (bash syntax).

Hand a new user their configuration

Ideally, the user generates their own key pair (the WireGuard apps do it in one click) and sends only their public key. When the administrator prepares everything, the peer's file contains its private key and the pre-shared key: it is full access to the network.

With “Send with onlyU”, the file goes from the kit to onlyU tab to tab in your browser, without appearing in an address or going through the kit's server. onlyU encrypts it in your browser and creates a link the user opens only once, valid for 24 hours, after which the link disappears. It is free, with no sign-up. The administrator then deletes their copy; to remove access one day, simply delete the matching [Peer] block on the server. For access to a server rather than a network, see the guide on how to create an SSH key.