Setting up a Wireguard VPN Server on Raspberry Pi 4B running Ubuntu 22.04
In this article, we will walk you through the process of setting up a Wireguard VPN server on a Raspberry Pi 4B running Ubuntu 22.04. We will cover the key concepts and provide detailed instructions to help you get started.
Prerequisites
Before we begin, make sure you have the following:
- A Raspberry Pi 4B with Ubuntu 22.04 installed
- A basic understanding of the command line
- An internet connection
Installing Wireguard
Unfortunately, the apt package manager does not have the latest version of Wireguard available. To install Wireguard, we will need to add the Wireguard repository to our system.
sudo add-apt-repository ppa:wireguard/wireguard
sudo apt update
sudo apt install wireguard
Generating Keys
Wireguard uses public key cryptography to secure communication between peers. We will need to generate a private and public key for our server and each client that will connect to it.
sudo apt install wg-quick
wg genkey | sudo tee /etc/wireguard/privatekey
sudo chmod 600 /etc/wireguard/privatekey
sudo cat /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
Configuring the Server
Now that we have our keys, we can configure our server. We will need to edit the Wireguard configuration file and add our private key, public key, and the IP address range that we want to use for our VPN clients.
sudo nano /etc/wireguard/wg0.conf
Add the following to the file:
[Interface]
Address = 10.0.0.1/24
PrivateKey =
ListenPort = 51820
Replace
Adding Clients
To add a client, we will need to generate a private and public key for the client and add a new section to the Wireguard configuration file.
wg genkey | sudo tee /etc/wireguard/clients/client1-privatekey
sudo chmod 600 /etc/wireguard/clients/client1-privatekey
sudo cat /etc/wireguard/clients/client1-privatekey | wg pubkey | sudo tee /etc/wireguard/clients/client1-publickey
Add the following to the Wireguard configuration file:
[Peer]
PublicKey =
AllowedIPs = 10.0.0.2/32
Replace
Starting the Server
Now that we have our configuration file set up, we can start the Wireguard server.
sudo systemctl start wg-quick@wg0
sudo systemctl enable wg-quick@wg0
Connecting Clients
To connect a client, we will need to generate a configuration file for the client and transfer it to the client device.
sudo nano /etc/wireguard/clients/client1.conf
Add the following to the file:
[Interface]
Address = 10.0.0.2/32
PrivateKey =
[Peer]
PublicKey =
Endpoint = :51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
Replace
In this article, we covered the key concepts of setting up a Wireguard VPN server on a Raspberry Pi 4B running Ubuntu 22.04. We generated keys, configured the server, added clients, and started the server. We also covered how to connect clients to the VPN server.
References
Types of references included: online resources.