How to Build Your Own Wireguard VPN in Five Minutes (2024)

/ #Security
How to Build Your Own Wireguard VPN in Five Minutes (1)
David Clinton
How to Build Your Own Wireguard VPN in Five Minutes (2)

You may already understand how important a good VPN can be for maintaining the security and privacy of your mobile communications.

Whether you need to use your phone for banking over a public airport or coffee shop WiFi connection, or you're worried about the wrong people listening in on your online interactions, the tunneled encryption a good VPN gives you can be invaluable.

The trick, however, is finding a VPN that really is "good" – and one that's both convenient and affordable.

There are plenty of commercial VPN services out there, and configuring one of those for your phone or laptop is usually simple enough.

But such services come with two potential down-sides: they're often expensive, with payments averaging around $10 monthly, and you can never be quite 100% sure that they aren't (accidentally or on purpose) leaking or misusing your data.

Also, cheaper VPNs often limit your data use and the number of devices you can connect.

If you like watching video versions of tutorials to supplement your learning, feel free to follow along here:

What WireGuard Delivers

But if you happen to have a cloud-based Linux server running anyway, building a WireGuard VPN can be a simple and free way to add some serious, compromise-free security and privacy to your life.

If you plan to limit the VPN to just devices owned by you and a few friends, you'll probably never even notice any extra resource load on your server. Even if you had to fire up and pay for a dedicated AWS EC2 t2.micro reserved instance, the annual costs should still come out significantly cheaper than most commercial VPNs. And, as a bonus, you'll get complete control over your data.

Right now I'm going to show you how all that would work using the open source WireGuard software on an Ubuntu Linux server.

Why WireGuard? Because it's really easy to use, is designed to be particularly attack resistant, and it's so good at what it does that it was recently incorporated into the Linux kernel itself.

The actual work to make this happen really will take only five minutes - or less. Having said that, planning things out, troubleshooting for unexpected problems and, if necessary, launching a new server might add significant time to the project.

How to Set Up Your Environment

First off, you'll need to open the UDP port 51820 in whatever firewall you're using. Here's how that would look for the security group associated with an AWS EC2 instance:

How to Build Your Own Wireguard VPN in Five Minutes (3)

Now, on the Linux server, using a sudo shell, we'll begin by installing the WireGuard and resolvconf packages.

Technically, we probably won't need resolvconf here, but since that's what you'd need if you wanted to set up a Linux machine as a WireGuard client I thought I'd throw that in here, too.

apt install wireguard resolvconf

How to Generate Encryption Keys

The wg genkey command generates a new private encryption key and saves it as a file in the /etc/wireguard directory. This directory was automatically created when we installed WireGuard.

The chmod command sets the appropriate restrictive permissions for that private key file.

Like everything in Linux, there are other ways to get this done, but just make sure you do it right.

wg genkey | sudo tee /etc/wireguard/private.keychmod go= /etc/wireguard/private.key

Next, we'll use the value of our private key to generate a matching public key – which will also be saved to the /etc/wireguard directory. The goal is to add the server's public key to the WireGuard configuration on all the client devices we'll be using, and then to add those clients' public keys to the server configuration here.

Private keys should never leave the machines for which they're created – and should always be carefully protected.

cat /etc/wireguard/private.key | wg pubkey | sudo tee

How to Configure the WireGuard Server

We're now ready to create a server configuration file. Following convention, I'll name the file wg0.conf, but you can give it any name you'd like. You can also have multiple configurations (with different filenames) existing at the same time.

Here's what our configuration will look like:

[Interface]Address = 10.5.5.1/24ListenPort = 51820# Use your own private key, from /etc/wireguard/privatekeyPrivateKey = your_key[Peer]# Workstation public keyPublicKey = your_key# VPN client's IP address in the VPNAllowedIPs = 10.5.5.2/32[Peer]# laptop public keyPublicKey = your_key# VPN client's IP address in the VPNAllowedIPs = 10.5.5.3/32

Notice that this file has three sections: an Interface, and two peers. The Interface section defines the private NAT network address that our server will use. That's the private address the clients will connect to – after first requesting access through the server's public IP address, of course.

You don't have to follow my addressing, as long as you use a valid private IP range that doesn't overlap on any network blocks being used by either your server or client.

Matching the UDP security group rule I set up earlier in AWS, I'm defining the ListenPort as 51820. But I could choose a different address to add a tiny bit more security if I want.

Finally, I would paste the server's Private Key as the value of PrivateKey so WireGuard will be able to authenticate incoming client requests.

The first peer section contains nothing more than the public key and assigned private IP address of one client. The second peer section does the same for a second client machine.

Getting those public keys from the client is the most manual task involved in this whole setup. But, since this is your own VPN, you can usually find a way to copy and paste directly into your server configuration so you don't need to painfully type the whole thing in.

That should be everything. I'll use the wg-quick command to bring the VPN to life. up tells WireGuard to read the wg0.conf configuration we just made and use it to build a new VPN interface.

wg-quick up wg0

Running wg will show us that it worked. Finally, I'll run systemctl enable to tell Linux to load this WireGuard interface automatically each time the server reboots.

systemctl enable wg-quick@wg0

How to Configure WireGuard Clients

That's all we'll need from the server end of things. Getting your client device set up with WireGuard is either going to be much easier or more or less the same.

What does that mean? Well, if you're working with Windows, macOS, Android or iOS, then there are links to GUI apps available from this wireguard.com/install page. Those apps will generate key pairs for you. You'll only need to enter the server's IP address or domain and its public key. You'll then take the client's public key and add it to the server wg0.conf file the way I showed you earlier.

However, if it's a Linux PC or laptop client you want to add, then it's a bit more complicated. You'll basically follow all the steps you saw for the server configuration, including the key generation. You'll even create a configuration file named wg0-conf (if that's the name you like). But here's how that config file should look:

[Interface]# The address your computer will use on the VPNAddress = 10.5.5.2/32DNS = 8.8.8.8# Load your privatekey from filePostUp = wg set %i private-key /etc/wireguard/privatekey# Also ping the vpn server to ensure the tunnel is initializedPostUp = ping -c1 10.47.47.1[Peer]# VPN server's wireguard public keyPublicKey = your_key# Public IP address of your VPN server (USE YOURS!)Endpoint = 54.160.21.183:51820# 10.0.0.0/24 is the VPN subnetAllowedIPs = 10.47.47.0/24# PersistentKeepalive = 25

The Interface section represents the client machine this time, while the Peer section down below refers to the server. Let's begin with Interface. The private IP address should match the address you give this particular client in the configuration on the server.

If you need your client to by-pass a local DNS server, you can specify a custom DNS server here. This one is the one provided by Google.

Instead of hard-coding your local private key into your configuration file the way we did on the server, you could tell WireGuard to read the privatekey file whenever it loads. This is probably a bit of a security best-practice – and we could just as easily have done it on the server, too. Finally, the configuration script will test our connection with the PostUp ping command.

The Peer – or server – configuration requires the server's public key, which is added here.

The Endpoint is where you tell WireGuard where to find the server. Nothing will work without this one! That would require the server's public IP – or it's domain name – followed by the port you've chosen. Again, 51820 is the WireGuard default.

Finally, the AllowedIPs setting defines the network address range you'll be using, and the optional PersistentKeepalive value can prevent dropped connections.

You launch WireGuard on the client exactly the same why you did on the server, using wg-quick up wg0. Again, though, all those steps will only be necessary for Linux clients. You can use the apps for other platforms.

Wrapping Up

So that's that. Just as I said, a working VPN in around five minute's work. You've now got one less excuse for protecting your online privacy and securing your communications.

For more technology goodness, please do subscribe to my YouTube channel and, when you've got a moment, check out the many Linux, security, data analytics, and AWS books and courses available through my bootstrap-it.com website.

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

How to Build Your Own Wireguard VPN in Five Minutes (4)
David Clinton

I'm an AWS solutions architect, Linux server professional, and author of books and Pluralsight courses on Linux, AWS, Docker, and IT security.

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

How to Build Your Own Wireguard VPN in Five Minutes (2024)

FAQs

How to Build Your Own Wireguard VPN in Five Minutes? ›

Simple & Easy-to-use

WireGuard aims to be as easy to configure and deploy as SSH. A VPN connection is made simply by exchanging very simple public keys – exactly like exchanging SSH keys – and all the rest is transparently handled by WireGuard. It is even capable of roaming between IP addresses, just like Mosh.

How to create your own VPN with WireGuard? ›

Connecting WireGuard VPN on Android
  1. Download and install the WireGuard app.
  2. Open the app and tap the floating icon on the bottom right of your phone.
  3. Tap the SCAN FROM QR CODE option.
  4. Allow the required permissions and scan the QR code.
  5. Once done, enter a new tunnel name and tap on Create Tunnel.
Mar 5, 2022

What is the fastest VPN for WireGuard? ›

Quick Comparison Table: WireGuard VPN Features
Lowest PriceWireGuard Speed
🥇ExpressVPN$6.67/monthVery fast (Lightway)
🥈CyberGhost$2.19/monthVery fast
🥉Private Internet Access$2.19/monthFast
NordVPN$5.99/monthVery fast (NordLynx)
1 more row
May 16, 2024

Is WireGuard easy to setup? ›

Simple & Easy-to-use

WireGuard aims to be as easy to configure and deploy as SSH. A VPN connection is made simply by exchanging very simple public keys – exactly like exchanging SSH keys – and all the rest is transparently handled by WireGuard. It is even capable of roaming between IP addresses, just like Mosh.

How do you make a tunnel WireGuard? ›

Tunnel Configuration
  1. Navigate to VPN > WireGuard > Tunnels.
  2. Click Add Tunnel.
  3. Fill in the options using the information determined earlier: Enable: Checked. Description: Remote Access. Listen Port: 51820. Interface Keys: Click Generate to create a new set of keys. Interface Addresses: 10.6. 210.1/24.
  4. Click Save.
Apr 3, 2024

How can I make my own VPN at home? ›

How to Set Up Your Own VPN Server at Home
  1. Choose the right platform and software. ...
  2. Set up a home server for your VPN. ...
  3. Configure your VPN network settings. ...
  4. Install necessary security features. ...
  5. Test your new home VPN setup. ...
  6. Run regular maintenance.
Feb 28, 2024

How much RAM do I need for WireGuard? ›

The OS recommends as a min a 1ghz cpu, 1gb of ram and 1.5gb of storage (Source).

Is anything better than WireGuard? ›

Tailscale does more than WireGuard, so that will always be true. We aim to minimize that gap, and Tailscale generally offers good bandwidth and excellent latency, particularly compared to non-WireGuard VPNs.

What is the No 1 fastest VPN in the world? ›

The three fastest VPNs at a glance
  • Surfshark: the fastest VPN. Budget-friendly prices, blistering speeds, and sleek apps ideal for VPN newbies—it's no wonder that Surfshark has retained the top spot when it comes to performance. ...
  • NordVPN: best VPN service overall. ...
  • Proton VPN: Super fast and even has a free plan.
May 21, 2024

Why is WireGuard so much faster than OpenVPN? ›

However, WireGuard is newer and faster than OpenVPN, because it was designed with modern devices and processors in mind. It is also easier to maintain. NordVPN is one of the providers that use the WireGuard protocol – so it's no wonder that it's one of the fastest services out there.

Why not to use WireGuard? ›

Why you shouldn't use WireGuard. WireGuard prioritizes speed, ease of use, and network security, but, some might say, at the expense of privacy. WireGuard does lack some standard features and practices many other protocols offer to enhance user privacy protection, such as: Dynamic IP addresses.

Is WireGuard completely free? ›

WireGuard is a communication protocol and free and open-source software that implements encrypted virtual private networks (VPNs), and was designed with the goals of ease of use, high speed performance, and low attack surface.

How much does WireGuard cost? ›

Since WireGuard and OpenVPN are free software, there is no expense associated with using them. Though there are some free solutions, you'll still need to pay for a VPN subscription. Since WireGuard and OpenVPN are free software, there is no expense associated with using them.

How to setup WireGuard VPN server at home? ›

  1. Step 1: Expose Wireguard VPN Server to the Internet. Your Public IP Address. ...
  2. Step 2: Setup Wireguard VPN Server. Install the wireguard software and dependencies. ...
  3. Step 3: Setup client connections. ...
  4. Step 4: Setup clients. ...
  5. Step 5: Test Connection.
Sep 29, 2023

How to build a VPN tunnel? ›

  1. Overview.
  2. Step 1: Create a VPN Gateway.
  3. Step 2: Create a Customer Gateway.
  4. Step 3: Create a VPN Tunnel.
  5. Step 4: Load the Configuration of the Local Gateway.
  6. Step 5: Configure a Routing Table.
  7. Step 6: Activate a VPN Tunnel.
Jan 9, 2024

Is WireGuard TCP or UDP? ›

By default, WireGuard uses UDP only.

Can I use WireGuard as VPN? ›

WireGuard is an extremely fast yet secure VPN protocol that can also be used as a standalone VPN. In fact, it's considered the fastest VPN protocol available today, making it a better option than IPsec/IKEv2 or OpenVPN when you're looking for speed and performance.

How do I create a WireGuard interface? ›

Configure a Wireguard interface (wg)
  1. Install required packages. The most straightforward method, and the one recommended in WireGuard documentation, is to use wg-quick . ...
  2. Create Server Keys and Interface Config. ...
  3. Use with network interfaces. ...
  4. As OpenRC service. ...
  5. Enable IP Forwarding. ...
  6. Running with modloop.

Is WireGuard VPN free? ›

WireGuard is a communication protocol and free and open-source software that implements encrypted virtual private networks (VPNs), and was designed with the goals of ease of use, high speed performance, and low attack surface.

How do I setup a WireGuard VPN on my router? ›

Go to Advanced > VPN Server > WireGuard, and tick the Enable box of WireGuard.
  1. View the default WireGuard VPN settings, as shown above. ...
  2. Specify a name for this account. ...
  3. • ...
  4. On the account list, you can click the button to modify the VPN server settings, connect to the server, or delete the account.

References

Top Articles
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 5985

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.