Configure a Wireguard interface (wg) (2024)

WireGuard has become a nearly ubiquitous vpn solution for multiple platform and is available in the community repository since Alpine 3.10. WireGuard itself is now integrated into the linux kernel since v5.6. Only the userland configuration tools are required.

Install required packages

The most straightforward method, and the one recommended in WireGuard documentation, is to use wg-quick.

Install wireguard-tools, iptables, and sysctl:

apk add wireguard-tools-wg-quickapk add iptablesapk add sysctl

Create Server Keys and Interface Config

Create a server private and public key:

wg genkey | tee server.privatekey | wg pubkey > server.publickey

Then, we create a new config file /etc/wireguard/wg0.conf using these new keys:

[Interface]Address = 192.168.2.1/24ListenPort = 45340PrivateKey = <server private key value> # the key from the previously generated privatekey filePostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;iptables -A FORWARD -o %i -j ACCEPTPostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE;iptables -D FORWARD -o %i -j ACCEPT[Peer]PublicKey = <client public key value> # obtained from client device via wireguard connection setup processAllowedIPs = 192.168.2.2/32

The PostUp and PostDown iptable rules forward traffic from the wg0 subnet (192.168.2.1/24) to the lan subnet on interface eth0.

Refer to this WireGuard documentation for information on adding peers to the config file.

Bring up the new wg0 interface:,

wg-quick up wg0

To take it down, we can use wg-quick down wg0 which will clean up the interface and remove the iptables rules.Note: If running in a Docker container, you will need to run with --cap-add=NET_ADMIN to modify your interfaces.

Use with network interfaces

To enable connecting with Wireguard on boot, open your /etc/network/interfaces and add this information after your auto other network interfaces:

auto wg0iface wg0 inet staticpre-up wg-quick up /etc/wireguard/wg0.conf

As OpenRC service

Since Alpine 3.20, there is a OpenRC initd service file included. To use the WireGuard OpenRC script, you need to create a symbolic link to it with the configuration name, like this:

ln -s /etc/init.d/wg-quick /etc/init.d/wg-quick.wg0# And then call your service like this:rc-service wg-quick.wg0 start

Enable IP Forwarding

With a NAT destination rule in place on your router, you should be able connect to the wireguard instance and access the host. However, if you intend for peers to be able to access external resources (including the internet), you will need to enable ip forwarding.

Edit the file /etc/sysctl.conf (or a .conf file under /etc/sysctl.d/) and add the following line:

net.ipv4.ip_forward = 1

Add the sysctl service to run at boot:

rc-update add sysctl 

Then either reboot or run sysctl -p /etc/sysctl.conf to reload the settings. To ensure forwarding is turned on, run sysctl -a | grep ip_forward and ensure net.ipv4.ip_forward is set to 1.

Running with modloop

If you are running from a RAM disk, you can't modify the modloop.

You can get around it by unpacking the modloop, mounting the unpacked modules folder, then installing WireGuard.

#!/bin/shapk add squashfs-tools # install squashfs tools to unpack modloopunsquashfs -d /root/squash /lib/modloop-lts # unpack modloop to root dirumount /.modloop # unmount existing modloopmount /root/squash/ /.modloop/ # mount unpacked modloopapk del wireguard-lts # uninstall previous WireGuard installapk add wireguard-ltsapk add wireguard-tools

You can repack the squash filesystem or put this script in the /etc/local.d/ path so it runs at boot-up.

Configure a Wireguard interface (wg) (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 5559

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.