What is WireGuard?
WireGuard is an extremely lightweight, high-performance VPN protocol built directly into modern Linux kernels. Unlike OpenVPN or IPsec (which comprise hundreds of thousands of lines of code), WireGuard is written in ~4,000 lines of code, offering lower power consumption on mobile devices and near line-speed throughput.
1. Installation & Key Generation
sudo apt update && sudo apt install -y wireguard qrencode
# Generate server private and public keys
cd /etc/wireguard
umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key
2. Configure the WireGuard Interface (`wg0.conf`)
Create `/etc/wireguard/wg0.conf`:
[Interface]
Address = 10.8.0.1/24
SaveConfig = true
PrivateKey = <PASTE_SERVER_PRIVATE_KEY>
ListenPort = 51820
# NAT forwarding via primary network interface (e.g., eth0)
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
Enable IPv4 packet forwarding in the Linux kernel:
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.d/99-sysctl.conf
sudo sysctl -p /etc/sysctl.d/99-sysctl.conf
3. Start the Interface
sudo systemctl enable --now wg-quick@wg0
sudo wg show
You can generate QR codes using `qrencode` to pair Android or iOS smartphones in seconds!