Install WireGuard in Ubuntu 24.04

WireGuard is a safe and fast VPN protocol, now included in the Linux kernel.


WireGuard is a newer VPN protocol that many describe as both faster and more secure than OpenVPN and IPsec. With WireGuard, you can achieve speeds almost as high as without a VPN. Its security mainly stems from strong encryption, but it also benefits from the protocol consisting of only about 4,000 lines of code, making it easy to audit.

Installing WireGuard

Installing WireGuard is easy since it's included in the Linux kernel. We only need to install the user-land tools:

Command
sudo apt install wireguard-tools

Configuring the WireGuard server

Start by creating a private and a public key.

Multiple commands
umask 077
wg genkey | tee privatekey | wg pubkey > publickey

Then, create a configuration file in /etc/wireguard/wg0.conf. Paste the value from the file privatekey into the PrivateKey directive in the configuration file.

/etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24
Address = fd86:ea04:1115::1/64
SaveConfig = true
PostUp = iptables -A FORWARD -i wg0 -o wg0 -m conntrack --ctstate NEW -j ACCEPT; iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o ens1 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -o wg0 -m conntrack --ctstate NEW -j ACCEPT; iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o ens1 -j MASQUERADE
ListenPort = 51820
PrivateKey = <ServerPrivateKey>

To allow IP forwarding, you must also add the following line to /etc/sysctl.conf:

Now, run this command to activate the new setting:

Setting up the firewall

Allow SSH connections (port 22) and the port used by WireGuard (port 51820).

To verify the new firewall settings:

Starting WireGuard

To start the service, run:

Make sure WireGuard starts automatically on every server boot.

Check the status of the WireGuard service.

This should output something similar:

Adding clients

So far, you haven't added any clients, so no one can connect to the WireGuard server.

On a client computer, install WireGuard.

Then, generate a key pair, still on the client computer.

Create a configuration file on the client in /etc/wireguard/wg0.conf. Note that PrivateKey is the client's private key. PublicKey is the server's public key, which was generated on the server. EndPoint is the IP address or hostname of the server.

Adding the client to the server

On the server, add the highlighted lines to /etc/wireguard/wg0.conf, where PublicKey (in the Peer section) is the private key that was generated on the client.

Bring down WireGuard and start it back up again for the changes to take effect.

Connect the client

On the client, connect to the server.

Check the status.

This will output something similar to this:

You can also try to ping the tunnel's internal IP address of the server:

If the ping command worked, everything is set up, and the tunnel is active.

Last updated

Was this helpful?