Setting up a WireGuard VPN Server on Raspberry Pi 4 with Ubuntu 22.04 LTS and Docker
In this article, we will walk you through the process of setting up a WireGuard VPN server on a Raspberry Pi 4 running Ubuntu 22.04 LTS and Docker. We will be using the image from linuxserver.io.
Prerequisites
Before we begin, make sure you have the following:
- A Raspberry Pi 4 with Ubuntu 22.04 LTS installed
- Docker and Docker Compose installed
- An understanding of basic networking concepts
Installing Docker Compose WireGuard Server
With Docker and Docker Compose already installed, we can now proceed with installing the WireGuard server. First, create a new directory for the WireGuard configuration:
sudo mkdir -p /opt/wireguard
Next, navigate to the newly created directory and create a new file named docker-compose.yml with the following content:
version: "2.1"
services:
wireguard:
image: linuxserver/wireguard
container_name: wireguard
cap_add:
- NET_ADMIN
device: /dev/net/tun
environment:
- PUID=1000
- PGID=1000
- TZ=America/Los\_Angeles
- SERVERURL=your\_server\_url
- SERVERPORT=51820
- PEERS=1
- PEERDNS=1
- ALLOWEDIPS=0.0.0.0/0,::/0
- INTERNAL\_SUBNET=10.13.37.0/24
- LOG\_QUiet=yes
ports:
- "51820:51820/udp"
volumes:
- /opt/wireguard/config:/config
- /lib/modules:/lib/modules
restart: unless-stopped
Make sure to replace your\_server\_url with the actual URL of your server. Also, adjust the other environment variables as needed.
With the docker-compose.yml file in place, we can now start the WireGuard server:
sudo docker-compose up -d
Configuring WireGuard Client
With the WireGuard server up and running, we can now proceed with configuring the client. First, generate a new WireGuard key pair:
wg genkey | tee privatekey | wg pubkey > publickey
Next, create a new file named wg0.conf with the following content:
[Interface]
PrivateKey = your\_private\_key
Address = 10.13.37.2/32
[Peer]
PublicKey = your\_server\_public\_key
Endpoint = your\_server\_url:51820
AllowedIPs = 0.0.0.0/0,::/0
PersistentKeepalive = 25
Make sure to replace your\_private\_key and your\_server\_public\_key with the actual keys, and your\_server\_url with the actual URL of your server.
With the wg0.conf file in place, we can now start the WireGuard client:
sudo wg-quick up wg0
Testing the Connection
With both the server and client configured, we can now test the connection. From the client, try pinging a remote host:
ping -c 3 remote\_host
If the ping is successful, then the VPN connection is working as expected.
In this article, we have shown you how to set up a WireGuard VPN server on a Raspberry Pi 4 running Ubuntu 22.04 LTS and Docker. We have also shown you how to configure the WireGuard client and test the connection.