Introduction
In this guide, we will walk you through the process of a VPN server setup on Ubuntu 22.04 using WireGuard. WireGuard is a modern VPN that is known for its simplicity, speed, and security. This guide is especially useful for those looking to enhance their privacy and security online by setting up their own VPN server. Setting up a VPN server on Ubuntu can be a cost-effective solution for protecting your online activities. With WireGuard, the setup process is streamlined and efficient, making it an excellent choice for both beginners and seasoned administrators. Let’s dive into the prerequisites before starting the server setup on Ubuntu.
Prerequisites
Before you begin the server setup on Ubuntu, ensure you have the following:
- A server running Ubuntu 22.04 with sudo privileges
- An active internet connection
- Basic command-line experience
- Updated system packages.
Step 1: Update and Upgrade System Packages
Before initiating the server setup on Ubuntu, update your package. This ensures all software is up to date and reduces the risk of security vulnerabilities.
sudo apt update && sudo apt upgrade -y
This command updates the package list and installs the newest versions of all packages currently installed. With your system updated, you’re ready to move on to installing WireGuard.
Step 2: Install WireGuard
To proceed with the server setup on Ubuntu, install WireGuard using the package manager. WireGuard is included in the Ubuntu repositories, making the installation straightforward.
sudo apt install wireguard -y
This command installs WireGuard and its dependencies, preparing your system for the VPN configuration. With WireGuard installed, the next step is to generate the necessary server keys.
Step 3: Generate Server Keys
Generate a private and public key pair for your WireGuard server. These keys will be used to establish secure connections.
wg genkey | tee /etc/wireguard/privatekey | wg pubkey > /etc/wireguard/publickey
This command creates a private key and saves it to /etc/wireguard/privatekey, while the public key is stored in /etc/wireguard/publickey. Now that you have your keys, it’s time to configure the WireGuard interface.
Step 4: Configure the WireGuard Interface
Create a configuration file for the WireGuard interface. This file will hold the server settings necessary for the VPN operation.
sudo nano /etc/wireguard/wg0.conf
This opens a text editor to create and edit the wg0.conf configuration file for WireGuard. Once the file is open, proceed to edit its contents.
Step 5: Edit the WireGuard Configuration File
In the wg0.conf file, specify your server setup details for Ubuntu. Include interface settings and peer configurations.
[Interface]
PrivateKey = <YourServerPrivateKey>
Address = 10.0.0.1/24
ListenPort = 51820
SaveConfig = true
[Peer]
PublicKey = <ClientPublicKey>
AllowedIPs = 10.0.0.2/32
Replace <YourServerPrivateKey> with the private key generated earlier. This configures the server IP address, port, and peer connection details. With the configuration file ready, the next step is to enable IP forwarding.
Step 6: Enable IP Forwarding
To allow traffic to flow between the VPN interface and other interfaces, enable IP forwarding on your server.
sudo sysctl -w net.ipv4.ip_forward=1
This command temporarily enables IP forwarding, allowing packets to be forwarded across the network. To make this change permanent, proceed to the next step.
Step 7: Make IP Forwarding Permanent
To ensure IP forwarding persists across reboots, modify the sysctl configuration file.
sudo nano /etc/sysctl.conf
Uncomment or add the line net.ipv4.ip_forward=1 to the file, then save and exit. With IP forwarding configured, it’s important to set up firewall rules next.
Step 8: Set Up Firewall Rules
To secure your server setup on Ubuntu, configure firewall rules to allow VPN traffic through the designated port.
sudo ufw allow 51820/udp
This command allows UDP traffic on port 51820, which is used by WireGuard for VPN connections. With the firewall configured, it’s time to start and enable the WireGuard service.
Step 9: Start and Enable WireGuard Service
Start the WireGuard service and enable it to start automatically on boot.
sudo systemctl start [email protected]
sudo systemctl enable [email protected]
These commands activate the WireGuard service and ensure it runs at startup, maintaining the VPN connection. Now, let’s verify the WireGuard setup to ensure everything is functioning correctly.













