Wireguard
WireGuard® is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN. WireGuard is designed as a general purpose VPN for running on embedded interfaces and super computers alike, fit for many different circumstances. Initially released for the Linux kernel, it is now cross-platform (Windows, macOS, BSD, iOS, Android) and widely deployable. It is currently under heavy development, but already it might be regarded as the most secure, easiest to use, and simplest VPN solution in the industry.
Image details
Configuration
TypeContainerlinuxlinuxserver/wireguard:latest51820:51820/udp/config : /srv/lsio/wireguard/config/lib/modules : /lib/modulesPUID=1000PGID=1000TZ=Etc/UTCSERVERURL=wireguard.domain.comSERVERPORT=51820PEERS=1PEERDNS=autoINTERNAL_SUBNET=10.13.13.0ALLOWEDIPS=0.0.0.0/0PERSISTENTKEEPALIVE_PEERS=LOG_CONFS=trueunless-stoppedTemplate by technorabilia
Notes
Ensure to create the following volume directories on the host file system, or modify the paths in the volume mapping section under the advanced options below, as needed.
mkdir -p /srv/lsio/wireguard/config
Installation
- Ensure both Docker and Portainer are installed, and up-to-date
- Log into your Portainer web UI
- Under Settings → App Templates, paste the below URL
- Head to Home → App Templates, and the list of apps will show up
- Select the app you wish to deploy, fill in any config options, and hit Deploy
Template Import URL
https://raw.githubusercontent.com/Lissy93/portainer-templates/main/templates.json
Show Me
docker run -d --name Wireguard \
-p 51820:51820/udp \
-e 'PUID=1000' \
-e 'PGID=1000' \
-e 'TZ=Etc/UTC' \
-e 'SERVERURL=wireguard.domain.com' \
-e 'SERVERPORT=51820' \
-e 'PEERS=1' \
-e 'PEERDNS=auto' \
-e 'INTERNAL_SUBNET=10.13.13.0' \
-e 'ALLOWEDIPS=0.0.0.0/0' \
-e 'PERSISTENTKEEPALIVE_PEERS=' \
-e 'LOG_CONFS=true' \
-v /srv/lsio/wireguard/config:/config \
-v /lib/modules:/lib/modules \
--restart=unless-stopped \
linuxserver/wireguard:latestSave this file as compose.yaml and run docker compose up -d
Use this only as a guide.
services:
wireguard:
image: linuxserver/wireguard:latest
ports:
- 51820:51820/udp
volumes:
- /srv/lsio/wireguard/config:/config
- /lib/modules:/lib/modules
environment:
PUID: '1000'
PGID: '1000'
TZ: Etc/UTC
SERVERURL: wireguard.domain.com
SERVERPORT: '51820'
PEERS: '1'
PEERDNS: auto
INTERNAL_SUBNET: 10.13.13.0
ALLOWEDIPS: 0.0.0.0/0
PERSISTENTKEEPALIVE_PEERS: ''
LOG_CONFS: 'true'
restart: unless-stopped
Save this file as wireguard-stack.yml, then from a manager node, run docker stack deploy -c wireguard-stack.yml wireguard
version: '3.8'
services:
wireguard:
image: linuxserver/wireguard:latest
ports:
- 51820:51820/udp
volumes:
- /srv/lsio/wireguard/config:/config
- /lib/modules:/lib/modules
environment:
PUID: '1000'
PGID: '1000'
TZ: Etc/UTC
SERVERURL: wireguard.domain.com
SERVERPORT: '51820'
PEERS: '1'
PEERDNS: auto
INTERNAL_SUBNET: 10.13.13.0
ALLOWEDIPS: 0.0.0.0/0
PERSISTENTKEEPALIVE_PEERS: ''
LOG_CONFS: 'true'
deploy:
restart_policy:
condition: any
Save each file below into a folder, then run kubectl apply -f .
Written for k3s, but works on any cluster: bind mounts use hostPath, named volumes get a
persistent volume claim, and ports are exposed via a LoadBalancer service.
wireguard-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wireguard
labels:
app: wireguard
spec:
replicas: 1
selector:
matchLabels:
app: wireguard
template:
metadata:
labels:
app: wireguard
spec:
containers:
- name: wireguard
image: linuxserver/wireguard:latest
env:
- name: PUID
value: '1000'
- name: PGID
value: '1000'
- name: TZ
value: Etc/UTC
- name: SERVERURL
value: wireguard.domain.com
- name: SERVERPORT
value: '51820'
- name: PEERS
value: '1'
- name: PEERDNS
value: auto
- name: INTERNAL_SUBNET
value: 10.13.13.0
- name: ALLOWEDIPS
value: 0.0.0.0/0
- name: PERSISTENTKEEPALIVE_PEERS
value: ''
- name: LOG_CONFS
value: 'true'
ports:
- containerPort: 51820
protocol: UDP
volumeMounts:
- name: wireguard-data-0
mountPath: /config
- name: wireguard-data-1
mountPath: /lib/modules
volumes:
- name: wireguard-data-0
hostPath:
path: /srv/lsio/wireguard/config
- name: wireguard-data-1
hostPath:
path: /lib/modules
wireguard-service.yaml
apiVersion: v1
kind: Service
metadata:
name: wireguard
spec:
type: LoadBalancer
selector:
app: wireguard
ports:
- name: 51820-udp
port: 51820
targetPort: 51820
protocol: UDP
Save this file as ~/.config/containers/systemd/wireguard.container
[Unit]
Description=Wireguard
[Container]
Image=linuxserver/wireguard:latest
ContainerName=wireguard
PublishPort=51820:51820/udp
Environment=PUID=1000
Environment=PGID=1000
Environment=TZ=Etc/UTC
Environment=SERVERURL=wireguard.domain.com
Environment=SERVERPORT=51820
Environment=PEERS=1
Environment=PEERDNS=auto
Environment=INTERNAL_SUBNET=10.13.13.0
Environment=ALLOWEDIPS=0.0.0.0/0
Environment=PERSISTENTKEEPALIVE_PEERS=
Environment=LOG_CONFS=true
Volume=/srv/lsio/wireguard/config:/config
Volume=/lib/modules:/lib/modules
[Service]
Restart=always
[Install]
WantedBy=default.targetThen reload systemd and start the service:
systemctl --user daemon-reload
systemctl --user start wireguardFor a system-wide service, use /etc/containers/systemd/ and drop --user.
For more installation options, see the Documentation in our GitHub repo
linuxserver/wireguard
This readme has been truncated from the full version found HERE
WireGuard® is an extremely simple yet fast and modern VPN that utilizes state-of-the-art cryptography. It aims to be faster, simpler, leaner, and more useful than IPsec, while avoiding the massive headache. It intends to be considerably more performant than OpenVPN. WireGuard is designed as a general purpose VPN for running on embedded interfaces and super computers alike, fit for many different circumstances. Initially released for the Linux kernel, it is now cross-platform (Windows, macOS, BSD, iOS, Android) and widely deployable. It is currently under heavy development, but already it might be regarded as the most secure, easiest to use, and simplest VPN solution in the industry.Application Setup
During container start, it will first check if the wireguard module is already installed and loaded. All currently supported kernels should have the wireguard module built-in (along with some older custom kernels). However, the module may not be enabled. Make sure it is enabled prior to starting the container.This can be run as a server or a client, based on the parameters used.
Note on iptables
Some hosts may not load the iptables kernel modules by default. In order for the container to be able to load them, you need to assign theSYS_MODULE capability and add the optional /lib/modules volume mount. Alternatively you can modprobe them from the host before starting the container.Server Mode
If the environment variablePEERS is set to a number or a list of strings separated by comma, the container will run in server mode and the necessary server and peer/client confs will be generated. The peer/client config qr codes will be output in the docker log if LOG_CONFS is set to true. They will also be saved in text and png format under /config/peerX in case PEERS is a variable and an integer or /config/peer_X in case a list of names was provided instead of an integer.Variables
SERVERURL, SERVERPORT, INTERNAL_SUBNET, PEERDNS, INTERFACE, ALLOWEDIPS and PERSISTENTKEEPALIVE_PEERS are optional variables used for server mode. Any changes to these environment variables will trigger regeneration of server and peer confs. Peer/client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.To add more peers/clients later on, you increment the
PEERS environment variable or add more elements to the list and recreate the container.To display the QR codes of active peers again, you can use the following command and list the peer numbers as arguments:
docker exec -it wireguard /app/show-peer 1 4 5 or docker exec -it wireguard /app/show-peer myPC myPhone myTablet (Keep in mind that the QR codes are also stored as PNGs in the config folder).The templates used for server and peer confs are saved under
/config/templates. Advanced users can modify these templates and force conf generation by deleting /config/wg_confs/wg0.conf and restarting the container.The container managed server conf is hardcoded to
wg0.conf. However, the users can add additional tunnel config files with .conf extensions into /config/wg_confs/ and the container will attempt to start them all in alphabetical order. If any one of the tunnels fail, they will all be stopped and the default route will be deleted, requiring user intervention to fix the invalid conf and a container restart.Client Mode
Do not set thePEERS environment variable. Drop your client conf(s) into the config folder as /config/wg_confs/<tunnel name>.conf and start the container. If there are multiple tunnel configs, the container will attempt to start them all in alphabetical order. If any one of the tunnels fail, they will all be stopped and the default route will be deleted, requiring user intervention to fix the invalid conf and a container restart.If you get IPv6 related errors in the log and connection cannot be established, edit the
AllowedIPs line in your peer/client wg0.conf to include only 0.0.0.0/0 and not ::/0; and restart the container.Road warriors, roaming and returning home
If you plan to use Wireguard both remotely and locally, say on your mobile phone, you will need to consider routing. Most firewalls will not route ports forwarded on your WAN interface correctly to the LAN out of the box. This means that when you return home, even though you can see the Wireguard server, the return packets will probably get lost.This is not a Wireguard specific issue and the two generally accepted solutions are NAT reflection (setting your edge router/firewall up in such a way as it translates internal packets correctly) or split horizon DNS (setting your internal DNS to return the private rather than public IP when connecting locally).
Both of these approaches have positives and negatives however their setup is out of scope for this document as everyone's network layout and equipment will be different.
Maintaining local access to attached services
Note: This is not a supported configuration by Linuxserver.io - use at your own risk.When routing via Wireguard from another container using the
service option in docker, you might lose access to the containers webUI locally. To avoid this, exclude the docker subnet from being routed via Wireguard by modifying your wg0.conf like so (modifying the subnets as you require):```ini Interface PrivateKey =
Site-to-site VPN
Note: This is not a supported configuration by Linuxserver.io - use at your own risk.Site-to-site VPN in server mode requires customizing the
AllowedIPs statement for a specific peer in wg0.conf. Since wg0.conf is autogenerated when server vars are changed, it is not recommended to edit it manually.In order to customize the
AllowedIPs statement for a specific peer in wg0.conf, you can set an env var SERVER_ALLOWEDIPS_PEER_<peer name or number> to the additional subnets you'd like to add, comma separated and excluding the peer IP (ie. "192.168.1.0/24,192.168.2.0/24"). Replace <peer name or number> with either the name or number of a peer (whichever is used in the PEERS var).For instance
SERVER_ALLOWEDIPS_PEER_laptop="192.168.1.0/24,192.168.2.0/24" will result in the wg0.conf entry AllowedIPs = 10.13.13.2,192.168.1.0/24,192.168.2.0/24 for the peer named laptop.Keep in mind that this var will only be considered when the confs are regenerated. Adding this var for an existing peer won't force a regeneration. You can delete wg0.conf and restart the container to force regeneration if necessary.
Don't forget to set the necessary POSTUP and POSTDOWN rules in your client's peer conf for lan access.
Usage
docker run -d \
--name=wireguard \
--cap-add=NET_ADMIN \
--cap-add=SYS_MODULE `#optional` \
-e PUID=1000 \
-e PGID=1000 \
-e TZ=Etc/UTC \
-e SERVERURL=wireguard.domain.com `#optional` \
-e SERVERPORT=51820 `#optional` \
-e PEERS=1 `#optional` \
-e PEERDNS=auto `#optional` \
-e INTERNAL_SUBNET=10.13.13.0 `#optional` \
-e ALLOWEDIPS=0.0.0.0/0 `#optional` \
-e PERSISTENTKEEPALIVE_PEERS= `#optional` \
-e LOG_CONFS=true `#optional` \
-p 51820:51820/udp \
-v /path/to/wireguard/config:/config \
-v /lib/modules:/lib/modules `#optional` \
--sysctl="net.ipv4.conf.all.src_valid_mark=1" \
--restart unless-stopped \
lscr.io/linuxserver/wireguard:latestCheck the logs first
Nine times out of ten the logs tell you exactly what went wrong.
- In Portainer, go to Containers, click the container, then Logs. Or run
docker logs Wireguard - Exit codes help too:
137means killed, usually out of memory.126or127means the command inside the image is broken.
Port already in use
If deployment fails with "Bind for 0.0.0.0:51820 failed: port is already allocated", something else on your server is using that port.
- Find what's using it:
sudo ss -tlnp | grep :51820 - Stop the other service, or pick a different host port. In
51820:51820only the left number is yours to change, the right one belongs to the app.
Permission denied on volumes
If the logs show "permission denied", the app can't write to its data folder on the host.
- Fix the ownership:
sudo chown -R 1000:1000 /srv/lsio/wireguard/config(and the same for the other mapped folders) - Or set the
PUIDandPGIDvariables (defaults 1000:1000) to match your own user, found withid $USER
Image won't pull
Test the pull directly on the host: docker pull linuxserver/wireguard:latest
- "manifest unknown" means the tag no longer exists. This template uses
latest, so try pinning a specific version instead. - "toomanyrequests" is the Docker Hub rate limit. Log in with
docker loginto raise it. - "no space left on device" means a full disk. Reclaim space with
docker system prune
"exec format error"
This means the image was built for a different CPU architecture than your server.
- This image supports:
amd64, arm64 - Check yours with
uname -m: x86_64 is amd64, aarch64 is arm64. Raspberry Pi and other ARM boards are the usual culprits.
Container keeps restarting
The unless-stopped restart policy relaunches the app after every crash, so the real error can scroll past.
- Check the logs right after a restart, the last few lines before it died are the useful ones.
- Get the exit code with
docker inspect Wireguard --format '{{.State.ExitCode}}' - Still stuck? Redeploy once with the restart policy set to
noso the failure stays visible.
Required settings are blank
PERSISTENTKEEPALIVE_PEERS has no default value, and Wireguard may crash or misbehave if left empty.
- Fill it in on the deploy screen before hitting deploy.
Raise an issue
Found something which isn't working as it should? Here's how to report it.
- Bug within the app: Open an issue within Wireguard's repo
- Template not working: Open an issue on technorabilia/portainer-templates
- This website not working: Open an issue on lissy93/portainer-templates