Configure Persistent Wireguard Network Interface on Alpine Linux

alpineipwireguard

I have configured Wireguard VPN on Alpine Linux 3.16.2. Manual server and client configuration works fine.

uname -mrs
# Linux 5.15.60-0-virt aarch64

ip link add dev wg0 type wireguard
ip address add dev wg0 10.0.0.4/8
wg setconf wg0 /etc/wireguard/wg0.conf
ip link set up dev wg0
wg
# interface: wg0
#   public key: 9nEynT6g.....
#   private key: (hidden)
#   listening port: 31194
# peer: A2zDuhbX6....
#   allowed ips: 10.0.0.5/32

but when I reboot the system device wg0 disappears. How to do it persistent?

I followed the instruction Alpine Linux set up WireGuard VPN server but it seems a bit outdated.

When adding interface to /etc/network/interfaces I faced the following problems.
They do not add new entries to this file directly because NOTE: /sbin/assemble-interfaces rewrites this file. Edit files in /etc/network/interfaces.d/ to persist any customizations.

If I add new config file with interface description it does not load after reboot:

nano /etc/network/interfaces.d/wg0

The content of /etc/network/interfaces.d/wg0

auto wg0
iface wg0 inet static
    address 10.0.0.4/8
    gateway 10.0.0.1
    pre-up ip link add dev wg0 type wireguard
    pre-up wg setconf wg0 /etc/wireguard/wg0.conf
    post-down ip link delete wg0

Manual configuration is working. I can establish VPN connection from remote client to server, ssh to server using private address 10.0.0.4 and access Internet.
So how configure this interface to up at boot time on Alpine Linux 3.16.2]1?

Best Answer

That /sbin/assemble-interfaces script (and the /etc/network/interfaces.d/ directory) is not a standard part of Alpine Linux. It's probably part of some other networking package you added, and you likely have to run it manually every time you make any changes in your /etc/network/interface.d/ directory.

Normally you would add interface definitions directly to /etc/network/interfaces, and run rc-service networking restart to apply your changes (see Alpine's Configure Networking documentation).

Another option would be to use wg-quick to manage your WireGuard interface. To do so, add the interface's address to your /etc/wireguard/wg0.conf file:

[Interface]
PrivateKey = ABC123...
Address = 10.0.0.4/8
...

Then set up an OpenRC service for it, with an init script like this at /etc/init.d/wg-quick:

#!/sbin/openrc-run

description="WireGuard Quick"

depend() {
    need localmount
    need net
}

start() {
    wg-quick up wg0
}

stop() {
    wg-quick down wg0
}

You can use the rc-service wg-quick start and rc-service wg-quick stop commands to start up and shut down this service; and you can enable it at boot with the following command:

rc-update add wg-quick default
Related Topic