Score:1

Wireguard on Alpine: automatically mount wg0 after boot

us flag

I have a wireguard installed on a Alpine server. It's working, but when I reboot the server, I'm obliged to manually execute wg-quick up wg0

wireguard:~# wg show
wireguard:~# wg-quick up wg0
[#]
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.6.6.1/24 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] ip -6 route add fd9f:6666::1/128 dev wg0
[#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
wireguard:~# wg show
interface: wg0
  public key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
  private key: (hidden)
  listening port: 51820

peer: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
  preshared key: (hidden)
  endpoint: xx.xx.xx.xx:49323
  allowed ips: fd9f:6666::1/128, 10.6.6.2/32
  latest handshake: 2 seconds ago
  transfer: 7.05 KiB received, 6.31 KiB sent
wireguard:~#

I have applied this doc:https://wiki.alpinelinux.org/wiki/Configure_a_Wireguard_interface_(wg)#Bringing_up_an_interface_using_wg-tools

wireguard:~# cat /etc/modules
af_packet
ipv6
wireguard
wireguard:~# cat /etc/wireguard/wg0.conf
# Updated: 2021-11-29 22:52:22.801259882 +0000 UTC / Created: 2021-11-29 19:52:59.17510794 +0000 UTC
[Interface]
Address = 10.6.6.1/24
ListenPort = 51820
PrivateKey = xxxxxxxxxxxxx=

PreUp =
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PreDown =
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
...

Where is my mistake?

Score:0
us flag

Thanks Justin Ludwig for your answer. I didn't tested your solution because I've managed to solve my problem modifying my /etc/network/interfaces like this:

auto wg0
iface wg0 inet static
        address 10.6.6.1
        netmask 255.255.255.0
        post-up wg-quick up /etc/wireguard/wg0.conf
        post-down wg-quick down /etc/wireguard/wg0.conf
Score:0
cn flag

No mistakes, you just need to set up an OpenRC service for it. Create 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
}

Then run the following command to enable it for the default runlevel:

rc-update add wg-quick default
Limnic avatar
ca flag
Note that the init script must be made executable.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.