Configure a Wireguard interface (wg): Difference between revisions

From Alpine Linux
(Updated instructions to reflect current release packages (3.20).)
(fixed wikitags added see also section)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{TOC right}}
{{TOC right}}


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.
[https://en.wikipedia.org/wiki/WireGuard WireGuard] multiple platform vpn solution. WireGuard itself is now integrated into the linux kernel since v5.6. Only the userland configuration tools are required.


== Install required packages ==
== Installation  ==


The most straightforward method, and the one recommended in WireGuard documentation, is to use <code>wg-quick</code>.
The most straightforward method to configure WireGuard is to use the tool <code>wg-quick</code> available in the package {{pkg|wireguard-tools-wg-quick}}.  


Install wireguard-tools, iptables, and sysctl:
Install the meta package {{pkg|wireguard-tools}} to install the necessary WireGuard packages  and {{pkg|iptables}} as follows: {{Cmd|# apk add wireguard-tools iptables}}


apk add wireguard-tools-wg-quick
== Configuration ==
apk add iptables


== Create Server Keys and Interface Config ==
=== Create Server Keys and Interface Config ===


Create a server private and public key:
Create a server private and public key: {{Cmd|<nowiki># wg genkey | tee server.privatekey | wg pubkey > server.publickey</nowiki>}}


wg genkey | tee server.privatekey | wg pubkey > server.publickey
Then, we create a new config file {{Path|/etc/wireguard/wg0.conf}} using these new keys as follows:{{Cat|/etc/wireguard/wg0.conf|<nowiki>
 
[Interface]
Then, we create a new config file <code>/etc/wireguard/wg0.conf</code> using these new keys:
Address = 192.168.2.1/24
 
ListenPort = 45340
[Interface]
PrivateKey = <server private key value> # the key from the previously generated privatekey file
Address = 192.168.2.1/24
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;iptables -A FORWARD -o %i -j ACCEPT
ListenPort = 45340
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE;iptables -D FORWARD -o %i -j ACCEPT
PrivateKey = <server private key value> # the key from the previously generated privatekey file
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;iptables -A FORWARD -o %i -j ACCEPT
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE;iptables -D FORWARD -o %i -j ACCEPT
   
   
[Peer]
[Peer]
PublicKey = <client public key value> # obtained from client device via wireguard connection setup process
PublicKey = <client public key value> # obtained from client device via wireguard connection setup process
AllowedIPs = 192.168.2.2/32
AllowedIPs = 192.168.2.2/32</nowiki>}}
 
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 [https://github.com/pirate/wireguard-docs#user-content-config-reference this WireGuard documentation] for information on adding peers to the config file.
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 [https://github.com/pirate/wireguard-docs#user-content-config-reference this WireGuard documentation] for information on adding peers to the config file.


Bring up the new wg0 interface:
Bring up the new {{ic|wg0}} interface:{{Cmd|# wg-quick up wg0}}
,
wg-quick up wg0


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


== Use with network interfaces ==
{{Note|If running in a Docker container, you will need to run with <code>--cap-add{{=}}NET_ADMIN</code> to modify your interfaces.}}


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


<pre>
To enable connecting with Wireguard on boot, open your {{Path|/etc/network/interfaces}} and add this information after your auto other network interfaces as follows:{{Cat|/etc/network/interfaces|<nowiki>...
auto wg0
auto wg0
iface wg0 inet static
iface wg0 inet static
pre-up wg-quick up /etc/wireguard/wg0.conf
pre-up wg-quick up /etc/wireguard/wg0.conf</nowiki>}}
</pre>


== As OpenRC service ==
=== Service configuration ===


Since Alpine 3.20, there is wireguard-tools-openrc, which provides an OpenRC initd service file. First install the package:
Since Alpine 3.20, {{pkg|wireguard-tools-openrc}} package provides an OpenRC initd service file.  


<pre>
To use this, install the package:{{Cmd|# apk add wireguard-tools-openrc }}
apk add wireguard-tools-openrc
</pre>


To use the WireGuard OpenRC script, you then need to create a symbolic link to it with the configuration name, like this:
To use the WireGuard OpenRC script with {{ic|wg-quick.wg0}}, create a symbolic link to it with the configuration name as follows:{{Cmd|# ln -s /etc/init.d/wg-quick /etc/init.d/wg-quick.wg0}}


<pre>
Add the {{ic|wg-quick.wg0}} service to the default runlevel:{{Cmd|# rc-update add wg-quick.wg0}}
ln -s /etc/init.d/wg-quick /etc/init.d/wg-quick.wg0
To start|stop|restart the [[OpenRC]] service:{{Cmd|# rc-service wg-quick.wg0 start}}
# Add it to the default runlevel:
rc-update add wg-quick.wg0
# Finally, start the service:
rc-service wg-quick.wg0 start
</pre>


== Enable IP Forwarding ==
=== 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.
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 <code>/etc/sysctl.conf</code> (or a <code>.conf</code> file under <code>/etc/sysctl.d/</code>) and add the following line:
Edit the file {{Path|/etc/sysctl.conf}} or a <code>.conf</code> file under {{Path|/etc/sysctl.d/}} folder add the following line as follows:{{Cat|/etc/sysctl.conf|
 
net.ipv4.ip_forward {{=}} 1}}
net.ipv4.ip_forward = 1


Add the sysctl service to run at boot:
Add the sysctl service to run at boot:{{Cmd|# rc-update add sysctl}}


rc-update add sysctl  
Then either reboot or run {{ic|# sysctl -p /etc/sysctl.conf}} to reload the settings. To ensure forwarding is turned on, run {{ic|# sysctl -a | grep ip_forward}} and ensure <Code>net.ipv4.ip_forward</code> is set to <code>1</code>.


Then either reboot or run <code>sysctl -p /etc/sysctl.conf</code> to reload the settings. To ensure forwarding is turned on, run <code>sysctl -a | grep ip_forward</code> and ensure <code>net.ipv4.ip_forward</code> is set to <code>1</code>.
In the file {{Path|/etc/conf.d/iptables}}, Change the setting as follows:{{Cat|/etc/conf.d/iptables|...
IPFORWARD{{=}}"yes"}}


== Running with modloop ==
== Running with modloop ==


If you are running from a RAM disk, you can't modify the modloop.
If you are running [[Diskless Mode]] i.e 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.
You can get around it by unpacking the modloop, mounting the unpacked modules folder, then installing WireGuard.  


  #!/bin/sh
  #!/bin/sh
Line 100: Line 83:


You can repack the squash filesystem or put this script in the /etc/local.d/ path so it runs at boot-up.
You can repack the squash filesystem or put this script in the /etc/local.d/ path so it runs at boot-up.
== See also ==
* [https://github.com/pirate/wireguard-docs WireGuard documentation]


[[Category:Networking]]
[[Category:Networking]]

Latest revision as of 16:39, 10 June 2025

WireGuard multiple platform vpn solution. WireGuard itself is now integrated into the linux kernel since v5.6. Only the userland configuration tools are required.

Installation

The most straightforward method to configure WireGuard is to use the tool wg-quick available in the package wireguard-tools-wg-quick.

Install the meta package wireguard-tools to install the necessary WireGuard packages and iptables as follows:

# apk add wireguard-tools iptables

Configuration

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 as follows:

Contents of /etc/wireguard/wg0.conf

[Interface] Address = 192.168.2.1/24 ListenPort = 45340 PrivateKey = <server private key value> # the key from the previously generated privatekey file PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;iptables -A FORWARD -o %i -j ACCEPT PostDown = 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 process AllowedIPs = 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 as follows:

Contents of /etc/network/interfaces

... auto wg0 iface wg0 inet static pre-up wg-quick up /etc/wireguard/wg0.conf

Service configuration

Since Alpine 3.20, wireguard-tools-openrc package provides an OpenRC initd service file.

To use this, install the package:

# apk add wireguard-tools-openrc

To use the WireGuard OpenRC script with wg-quick.wg0, create a symbolic link to it with the configuration name as follows:

# ln -s /etc/init.d/wg-quick /etc/init.d/wg-quick.wg0

Add the wg-quick.wg0 service to the default runlevel:

# rc-update add wg-quick.wg0

To start|stop|restart the OpenRC service:

# 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/ folder add the following line as follows:

Contents of /etc/sysctl.conf

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 and ensure net.ipv4.ip_forward is set to 1.

In the file /etc/conf.d/iptables, Change the setting as follows:

Contents of /etc/conf.d/iptables

... IPFORWARD="yes"

Running with modloop

If you are running Diskless Mode i.e 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/sh
apk add squashfs-tools # install squashfs tools to unpack modloop
unsquashfs -d /root/squash /lib/modloop-lts # unpack modloop to root dir
umount /.modloop # unmount existing modloop
mount /root/squash/ /.modloop/ # mount unpacked modloop
apk del wireguard-lts # uninstall previous WireGuard install
apk add wireguard-lts
apk 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.

See also