Setup Wireguard VPN Server on CentOS
This tutorial is going to show you how to set up your own WireGuard VPN server on CentOS.
WireGuard is made specifically for the Linux kernel. It runs inside the Linux kernel and allows you to create fast, modern, and secure VPN tunnel.
TL;DR
Prerequisites
This tutorial assumes that the VPN server and VPN client are both going to be running on CentOS 7/8 operating system.
Step 1: Install WireGuard on CentOS Server and Desktop
Log into your CentOS server, then run the following commands to install WireGuard.
|
|
Then use the same commands to install WireGuard on your local CentOS computer (the VPN client).
Step 2: Generate Public/Private Keypair
Server
Client
Step 3: Create WireGuard Configuration File
Server
|
|
Paste in this content:
Secure the file like so:
|
|
Client
|
|
Paste in this content:
|
|
Secure the config like so:
|
|
Step 4: Enable IP Forwarding on the Server
|
|
Add the following line at the end of this file.
|
|
|
|
Step 5: Configure IP Masquerading on the Server
Run the following command to enable IP masquerading in the server firewall.
This will hide your VPN network from the outside world. So the Internet can only see your VPN server’s IP, but can’t see your VPN client’s IP, just like your home router hides your private home network.
Step 6: Install a DNS Resolver on the Server
Since we specify the VPN server as the DNS server for client, we need to run a DNS resolver on the VPN server. We can install the bind9 DNS server.
Edit the BIND main configuration file /etc/named.conf.
|
|
In the options clause, you can find the following two lines.
This makes named listen on localhost only. If you want to allow clients in the same network to query domain names, then comment out these two lines. (add double slashes at the beginning of each line)
Find the following line.
|
|
Add the 10.10.10.0/24 network range so that VPN clients can send DNS queries. Note that you need to end each network range with a semicolon.
|
|
Save and close the file. Restart BIND9 for the changes to take effect.
|
|
Then you need to run the following command to allow VPN clients to connect to port 53.
|
|
Step 7: Open WireGuard Port in Firewall
Run the following command to open UDP port 51820 on the server.
Step 8: Start WireGuard
Server
Run the following command on the server to start WireGuard.
|
|
To stop it, run
|
|
You can also use systemd service to start WireGuard.
|
|
If the start failed, you should check the log to find out what’s wrong.
|
|
Enable auto-start at system boot time with the following command.
|
|
Now WireGuard server is ready to accept client connections.
Client
Start WireGuard.
|
|
Enable auto-start at system boot time.
|
|
By default, all traffic on the VPN client will be routed through the VPN server. Sometimes you may want to route only a specific type of traffic, based on the transport layer protocol and the destination port. This is known as policy routing.
Policy routing is configured on the client computer, and we need to stop the VPN connection first.
|
|
Then edit the client configuration file.
|
|
For example, if you add the following 3 lines in the [interface] section, then WireGuard will create a routing table named “1234” and add the ip rule into the routing table. In this example, traffic will be routed through VPN server only when TCP is used as the transport layer protocol and the destination port is 25, i.e, when the client computer sends emails.
Save and close the file. Then start WireGuard client again.
|
|
VPN Kill Switch
By default, your computer can access the Internet via the normal gateway when the VPN connection is disrupted. You may want to enable the kill switch feature, which prevents the flow of unencrypted packets through non-WireGuard interfaces.
Stop the WireGuard client process.
|
|
Edit the client configuration file.
|
|
Add the following two lines in the [interface] section.
Like this:
|
|
Save and close the file. Then start the WireGuard client.
|
|
If you want to remove a peer from your server, this will work:
|
|
That’s it. The same content posted in many places on the internet.
Happy networking! 😎