WireGuard — Lean, Modern VPN on the Command Line
Hands-on guide to WireGuard: generate key pairs, configure peers and AllowedIPs, manage tunnels with wg and wg-quick, and verify connections.
WireGuard is a modern, lean VPN protocol that runs directly inside the Linux kernel and gets by with just a few thousand lines of code – a fraction of OpenVPN or IPsec. Instead of negotiating ciphers, it relies on fixed, state-of-the-art cryptography (Curve25519, ChaCha20, Poly1305), which keeps the configuration remarkably simple. You manage keys, peers, and tunnel status with wg, and bring an interface up or down in seconds from a config file with wg-quick. Each node has a Curve25519 key pair; peers are linked by their public key, their AllowedIPs, and an endpoint. This guide takes you from key generation through peer management to troubleshooting a running tunnel.
Key Generation
wg genkey — Generate a private key (Curve25519). Never share the private key – lock the file down with chmod 600.
wg genkey > private.keywg pubkey < <private-key-file> — Derive the matching public key from a private key; the public key can safely be shared with peers.
wg pubkey < private.key > public.keywg genkey | tee private.key | wg pubkey > public.key — Generate both keys in one command.
wg genkey | tee private.key | wg pubkey > public.keywg genpsk — Generate a pre-shared key for an additional symmetric layer of security.
wg genpsk > preshared.keywg-quick (Easy Setup)
wg-quick up <interface> — Bring up a WireGuard interface using its config file.
wg-quick up wg0wg-quick down <interface> — Tear down a WireGuard interface.
wg-quick down wg0wg-quick strip <interface> — Show the config with wg-quick extras removed (pure wg format).
wg-quick strip wg0systemctl enable wg-quick@<interface> — Enable WireGuard to start at boot.
systemctl enable wg-quick@wg0systemctl start wg-quick@<interface> — Start WireGuard via systemd.
systemctl start wg-quick@wg0wg Interface Management
wg show — Show the status of all WireGuard interfaces.
wg showwg show <interface> — Show the status of a specific interface.
wg show wg0wg show <interface> dump — Show the status in machine-readable format.
wg show wg0 dumpwg showconf <interface> — Show the running configuration.
wg showconf wg0wg set <interface> listen-port <port> — Set the listening port.
wg set wg0 listen-port 51820wg set <interface> private-key <file> — Set the private key for an interface.
wg set wg0 private-key /etc/wireguard/private.keyPeer Management
wg set <interface> peer <pubkey> allowed-ips <cidr> endpoint <host:port> — Add a peer to an interface. The AllowedIPs decide which destination IPs are routed through the tunnel – set them carefully, as they control both routing and security.
wg set wg0 peer ABC123...= allowed-ips 10.0.0.2/32 endpoint 203.0.113.1:51820wg set <interface> peer <pubkey> remove — Remove a peer from an interface.
wg set wg0 peer ABC123...= removewg set <interface> peer <pubkey> persistent-keepalive <seconds> — Set the keepalive interval for NAT traversal.
wg set wg0 peer ABC123...= persistent-keepalive 25wg set <interface> peer <pubkey> preshared-key <file> — Set a pre-shared key for a peer.
wg set wg0 peer ABC123...= preshared-key /etc/wireguard/psk.keywg show <interface> latest-handshakes — Show when each peer last completed a handshake.
wg show wg0 latest-handshakeswg show <interface> transfer — Show data transfer statistics per peer.
wg show wg0 transferTroubleshooting
wg show <interface> endpoints — Show the current endpoints for all peers.
wg show wg0 endpointsping -c 4 <peer-ip> — Test connectivity to a peer through the tunnel.
ping -c 4 10.0.0.2ip addr show <interface> — Show the WireGuard interface IP configuration.
ip addr show wg0ip route | grep <interface> — Check which routes go through the WireGuard tunnel.
ip route | grep wg0journalctl -u wg-quick@<interface> — View the WireGuard systemd service logs.
journalctl -u wg-quick@wg0 -f Conclusion
WireGuard proves a VPN doesn't have to be complicated: one key pair per node, a short config file, and two commands are enough for an encrypted tunnel. Pay attention to two things above all – keep the private key locked down (chmod 600, never share it) and set AllowedIPs deliberately, since they decide routing and therefore the security of your tunnel. Once you manage more than a handful of peers, pair WireGuard with systemd (wg-quick@) and a version-controlled config under /etc/wireguard/.
Further Reading
- WireGuard – official website – concept, protocol, and downloads
- WireGuard Quick Start – step-by-step getting started guide
- WireGuard – Wikipedia – background and context