Below is a set of snippets for configuring WireGuard for three different Linux distributions and FreeBSD. The setup assumes a Debian server with a 10.9.21.0/24 network.
auto wg0
iface wg0 inet static
address 10.9.21.1/32
pre-up ip link add wg0 type wireguard
pre-up wg setconf wg0 /etc/wireguard/wg0.conf
up ip -4 route add 10.9.21.0/24 dev wg0
# up /bin/systemctl restart sshd.service
post-down ip link del wg0
umask 077 cd /etc/wireguard/ wg genkey > server-private.key wg pubkey < server-private.key > server-public.key
By the way, Debian and FreeBSD are closest to a true UNIX-like style, whereas Ubuntu is more geared toward ‘yaml-production’, and CentOS relies on systemd. I mean their out-of-the-box states.
Debian: configuring the wg0 interface in interfaces.d
auto wg0
iface wg0 inet static
address 10.9.21.3/32
pre-up ip link add wg0 type wireguard
pre-up wg setconf wg0 /etc/wireguard/wg0.conf
up ip -4 route add 10.9.21.0/24 dev wg0
post-down ip link del wg0
[Interface]# client private key
PrivateKey=<private.key>[Peer]# server public key
PublicKey=<server-public.key>Endpoint=<server-external-ip>:1234PersistentKeepalive=10AllowedIPs=10.9.21.0/24
printf'net.ipv4.ip_forward = 1'> /etc/sysctl.d/wireguard.conf
sysctl -p
wg-quick up /etc/wireguard/wg0.conf
wg-quick down /etc/wireguard/wg0.conf
systemctl enable wg-quick@wg0
systemctl status wg-quick@wg0
ifconfig -a
wg show
journalctl -u wg-quick@wg0
While wg show will display the status of the wg0 interface, service wireguard status will not return any information because the interface is configured via sysrc | rc.conf.
[Interface]# client private key
PrivateKey=<private.key>[Peer]# server public key
PublicKey=<server-public.key>Endpoint=<server-external-ip>:1234PersistentKeepalive=10AllowedIPs=10.9.21.0/24
ip link del wg0
ip link add wg0 type wireguard
wg setconf wg0 /etc/wireguard/wg0.conf
ip -4 address add 10.9.21.13/32 dev wg0
ip link set mtu 1420 up dev wg0
ip -4 route add 10.9.21.0/24 dev wg0
wg show
ping 10.9.21.1