30.11.2025

How to Install and Configure WireGuard VPN Client on Ubuntu Linux: Step-by-Step Secure Setup

WireGuard is a modern, lightning-fast VPN that makes securing your internet connection surprisingly simple. Unlike traditional VPNs that can be slow and hard to configure, WireGuard is lightweight, efficient, and incredibly easy to set up. Originally built for Linux, it now works on Windows, macOS, BSD, iOS, and Android.

In this step-by-step guide, we’ll show you how to install and configure WireGuard on Ubuntu 20.04. You’ll learn how to generate cryptographic keys, create a working VPN configuration, and start protecting your connection in just a few minutes.

Installing the WireGuard Client App on Ubuntu

Before setting up the WireGuard client, it's crucial to ensure that your Ubuntu server is up to date. Start by logging into your server via SSH and running the following commands to update your system's package list and upgrade the existing packages:

sudo apt update && sudo apt upgrade -y

Once the system is up to date, you can install the WireGuard client by running:

sudo apt-get install wireguard

This command installs WireGuard along with all the required dependencies.

Generating Private and Public Keys

WireGuard uses a pair of cryptographic keys (private and public) to establish secure communication between two endpoints. To generate a key pair, you'll need to run the following command:

Generate the private key:

wg genkey > private.key

Generate the public key based on the private key

wg pubkey < private.key > public.key

Here, private.key is your private key, which must be kept secure and private, while public.key is shared with the peer (server) for encryption purposes.

Configuring the Client

The next step is to configure the WireGuard client by creating a configuration file. This file defines the settings for the connection, including the private key, server's public key, and allowed IP addresses. To create the configuration file, open it using a text editor such as nano:

sudo nano /etc/wireguard/wg0.conf

In this configuration file, you’ll define the following parameters:

[Interface]
PrivateKey = <contents-of-client-privatekey>
Address = 10.0.0.1/24
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820

[Peer]
PublicKey = <contents-of-server-publickey>
AllowedIPs = 10.0.0.2/32

WireGuard Startup

Now that your configuration file is set up, it’s time to start the VPN connection. To do so, use the following command:

sudo wg-quick up wg0

This command starts the VPN connection based on the configuration you just created.

To test the connection, you can ping the server's IP address (which was configured in the AllowedIPs section) from the client:

ping 10.0.0.1

Checking the Connection Status

You can check the status of the WireGuard connection using the following command:

sudo wg show

This command will display the details of the active VPN connection, including the peer’s public key, the data transfer statistics, and the connection’s status.

Automating the VPN Connection at Boot (Optional)

If you want your VPN connection to start automatically when the system boots, you can enable the WireGuard service using systemd. To do so, run the following commands:

sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0

This ensures that the WireGuard service starts automatically on system boot and keeps the VPN connection persistent.

Common Issues / Troubleshooting

1. VPN doesn’t start

Make sure your configuration file is correctly named (wg0.conf) and located in /etc/wireguard/.

Check that you’re running the command with sudo wg-quick up wg0.

Verify that your network interface names match your system (e.g., eth0 vs ens3).

2. Connection drops or no traffic

Ensure AllowedIPs is set correctly. For full tunneling, use 0.0.0.0/0. For split tunneling, specify the exact subnets you want routed.

Check firewall rules. WireGuard requires UDP port 51820 (or your custom port) to be open.

3. Key mismatch errors

Double-check that you’re using the correct private key for the client and the correct public key for the peer (server).

Regenerate the keys if you suspect a corruption or copy-paste error.

4. Ping doesn’t work

Verify IP addresses in the configuration match between client and server.

Make sure the server is running and listening on the specified port.

Confirm there’s no firewall blocking the traffic.

Security Recommendations

1. Keep your private key safe

Never share private.key. Treat it like a password.

Store it in a secure directory with restricted permissions:

sudo chmod 600 /etc/wireguard/private.key
sudo chown root:root /etc/wireguard/private.key

2. Firewall configuration

Configure your firewall to allow WireGuard traffic only on the necessary port (default UDP 51820):

sudo ufw allow 51820/udp
sudo ufw enable

Restrict access from unknown IPs if possible.

3. Keep WireGuard updated

Regularly update your system and WireGuard packages to benefit from the latest security fixes:

sudo apt update && sudo apt upgrade -y

4. Review your configuration periodically

Check that your AllowedIPs, NAT rules, and interface settings are correct.

Remove unused keys or peers to minimize potential attack surfaces.

Conclusion

Congratulations! You have successfully installed and configured WireGuard on your Ubuntu 20.04 client machine, establishing a secure and encrypted VPN connection to your server. WireGuard stands out as a modern, fast, and lightweight VPN solution designed to deliver high performance without compromising security. Its streamlined and straightforward configuration process makes it accessible for both beginners and experienced users alike.

With WireGuard, you can confidently protect your online activities, whether you’re accessing corporate resources remotely, ensuring privacy while browsing public networks, or securely connecting multiple devices across different locations. Thanks to its minimal codebase and efficient cryptography, WireGuard offers robust security with reduced latency, making it an excellent choice for personal and professional VPN needs.

By implementing WireGuard, you enhance your network’s privacy and integrity, safeguarding your data from eavesdropping and unauthorized access. Keep your VPN client updated and periodically review your configuration to maintain optimal security and performance.

FAQ