WireGuard is a modern, fast and secure VPN protocol that is easier to configure than OpenVPN or IPsec. In this tutorial, we will show you how to install and configure a WireGuard server under Linux.
Debian/Ubuntu
sudo apt update && sudo apt install wireguard
CentOS/RHEL
sudo yum install epel-release elrepo-release -y
sudo yum install kmod-wireguard wireguard-tools -y
After the installation, we can start setting up the server.
WireGuard uses public/private key pairs for authentication.
mkdir -p /etc/wireguard && cd /etc/wireguard
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
privatekey
: Is required later for the server configuration.publickey
: s required by the clients to authenticate the serverCreate the configuration file for the interface:
nano /etc/wireguard/wg0.conf
Add the following configuration (replace <SERVER_PRIVATE_KEY>
and <SERVER_PUBLIC_IP>
accordingly):
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <SERVER_PRIVATE_KEY>
SaveConfig = true
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
Address
: The internal VPN IP address of the server.ListenPort
: The port on which WireGuard is listening.PrivateKey
: The previously generated private key.PostUp
& PostDown
: Rules for NAT so that clients can access the Internet via the server.Activate IP forwarding:
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0
Check the status:
systemctl status wg-quick@wg0
If errors occur, the logs can be viewed:
journalctl -xe -u wg-quick@wg0
Each client requires its own key pair.
Generate key
On the client:
wg genkey | tee privatekey | wg pubkey > publickey
Add the client to the server:
wg set wg0 peer <CLIENT_PUBLIC_KEY> allowed-ips 10.0.0.2/32
Save the changes:
wg show
Create client configration file:
Create the file wg-client.conf
on the client:
[Interface]
PrivateKey = <CLIENT_PRIVATE_KEY>
Address = 10.0.0.2/24
DNS = 8.8.8.8
[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = <SERVER_PUBLIC_IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
Load the configuration with:
wg-quick up wg-client.conf
Check the connection:
ping 10.0.0.1
If ufw
is used:
ufw allow 51820/udp
For iptables
:
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
iptables -A FORWARD -i wg0 -j ACCEPT
To save these permanently:
iptables-save > /etc/iptables/rules.v4
So that WireGuard starts automatically on reboot:
systemctl enable wg-quick@wg0
Your WireGuard server is now set up and ready for use. Clients can connect and communicate securely via the VPN tunnel. If problems occur, a look at the logs (wg show or journalctl -xe -u wg-quick@wg0) will help.
Van René Wittenberg | March 7, 2025 | Tags: VPNOpenVPN
Als u op zoek bent naar meer: Wij bieden krachtige rootservers, gameservers, webhosting, TeamSpeak-servers en reselleropties.
Meer informatie over onze producten