Installing WireGuard on Ubuntu
WireGuard is a modern VPN (Virtual Private Network) technology that uses state-of-the-art cryptography. Compared with other VPN software such as IPsec and OpenVPN, it’s generally faster, more secure and easier to configure. It runs on almost every platform, including Linux, Windows, Android and macOS. WireGuard is a peer-to-peer VPN, meaning it doesn’t use a client-server model: depending on its configuration, a peer can act as a traditional server or client. In this article we’ll show you how to set up a WireGuard VPN on Ubuntu 20.04.
Prerequisites
You’ll need an Ubuntu 20.04 server with root or sudo access.
Setting up the WireGuard server
We’ll start by installing WireGuard on the Ubuntu machine and setting it up to act as a server. We’ll also configure the system to route client traffic through WireGuard.
Install WireGuard on Ubuntu 20.04
WireGuard is available from the default Ubuntu repositories. To install it, run:
sudo apt install wireguard
This installs the WireGuard module and tools. WireGuard runs as a kernel module.
Configure WireGuard
You can configure and manage WireGuard interfaces with the wg and wg-quick command-line tools. Every device on a WireGuard VPN needs a private and a public key. Run the following command to generate the key pair:
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
The files are created in /etc/wireguard. Use cat or less to view them. Never share the private key with anyone, and always keep it safe. WireGuard also supports a pre-shared key, which adds an extra layer of symmetric-key cryptography. It’s optional and must be unique for each pair of peers. The next step is to configure the tunnel device that will carry the VPN traffic.
You can set up the device from the command line with the ip and wg commands, or by creating a configuration file in a text editor. Create a new file named wg0.conf:
sudo nano /etc/wireguard/wg0.conf
And add the following:
[Interface]
Address = 10.0.0.1/24
SaveConfig = true
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o ens3 -j MASQUERADE
You can name the interface anything you like, but names such as wg0 or wgvpn0 are recommended. The settings in the Interface section mean:
- Address: a comma-separated list of IPv4 or IPv6 addresses for the wg0 interface. Use addresses from a range reserved for private networks (10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16).
- ListenPort: the port WireGuard listens on.
- PrivateKey: the private key generated by
wg genkey. To see it, run:
sudo cat /etc/wireguard/privatekey
- SaveConfig: when set to true, the interface’s current state is saved to the configuration file when it shuts down.
- PostUp: a command or script run when bringing the interface up. Here we use iptables to enable masquerading, which lets traffic leave the server and gives VPN clients internet access.
Make sure you replace ens3 after -A POSTROUTING with the name of your public network interface. You can find it with:
ip -o -4 route show to default | awk '{print $5}'
- PostDown: a command or script run when bringing the interface down; here it removes the iptables rules.
The wg0.conf and privatekey files shouldn’t be readable by regular users. Use chmod to set their permissions to 600:
sudo chmod 600 /etc/wireguard/{privatekey,wg0.conf}
When you’re done, bring the wg0 interface up using the settings in the configuration file:
sudo wg-quick up wg0
The output will look something like this:
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.0.0.1/24 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE
To check the interface’s status and configuration, run:
sudo wg show wg0
interface: wg0
public key: r3imyh3MCYggaZACmkx+CxlD6uAmICI8pe/PGq8+qCg=
private key: (hidden)
listening port: 51820
You can also confirm the interface’s state with ip a show wg0:
ip a show wg0
4: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1420 qdisc noqueue state UNKNOWN group default qlen 1000
link/none
inet 10.0.0.1/24 scope global wg0
valid_lft forever preferred_lft forever
WireGuard can also be managed with systemd. To bring the WireGuard interface up at boot, run:
sudo systemctl enable wg-quick@wg0
Server networking and firewall configuration
IP forwarding must be enabled for NAT to work. Open /etc/sysctl.conf:
sudo nano /etc/sysctl.conf
Add or uncomment the following line:
net.ipv4.ip_forward=1
Save the file and apply the change:
sudo sysctl -p
net.ipv4.ip_forward = 1
If you use UFW to manage your firewall, open UDP traffic on port 51820:
sudo ufw allow 51820/udp
Your Ubuntu machine is now set up as a server.
Setting up Linux and macOS clients
Installation instructions for every supported platform are at wireguard.com/install. On Linux, install the package with your distribution’s package manager; on macOS, use brew. Once it’s installed, follow the steps below to configure the client. Setting up a Linux or macOS client is much the same as setting up the server. First, generate the public and private keys:
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
Create wg0.conf:
sudo nano /etc/wireguard/wg0.conf
And add the following:
[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.0.0.2/24
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP_ADDRESS:51820
AllowedIPs = 0.0.0.0/0
The Interface settings mean the same as on the server:
- Address: a comma-separated list of IPv4 or IPv6 addresses for the wg0 interface.
- PrivateKey: to see the key on the client machine, run:
sudo cat /etc/wireguard/privatekey
The Peer section contains:
- PublicKey: the public key of the peer you want to connect to (the contents of the server’s
/etc/wireguard/publickey). - Endpoint: the IP address or hostname of the peer you want to connect to, followed by a colon and the port the remote peer listens on.
- AllowedIPs: a comma-separated list of IPv4 or IPv6 addresses from which incoming traffic for the peer is allowed and to which outgoing traffic for the peer is sent. We use 0.0.0.0/0 because we’re routing all traffic and want the server peer to send packets from any IP.
To configure more clients, just repeat these steps with a different private IP address.
Add the client peer to the server
The last step is to add the client’s public key and IP address to the server. Run the following on the Ubuntu server:
sudo wg set wg0 peer CLIENT_PUBLIC_KEY allowed-ips 10.0.0.2
Make sure you replace CLIENT_PUBLIC_KEY with the public key generated on the client machine:
sudo cat /etc/wireguard/publickey
Change the client’s IP address if it’s different. Windows users can copy the public key from the WireGuard app. When you’re done, go back to the client and bring up the tunnel interface.
Summary
We’ve shown you how to install WireGuard on Ubuntu 20.04 and configure it as a VPN server.
We hope you found this useful. Share your comments and questions below, and follow Mizbandade for more.

