WireGuardVPNNetwork8 min read

Install WireGuard VPN

Set up a VPN server with WireGuard for secure and private access to your VPS.


WireGuard is a modern, fast, and secure VPN protocol. Ideal for accessing your VPS privately or connecting networks.

Step 1 — Install WireGuard

bash
sudo apt update
sudo apt install wireguard -y

Step 2 — Generate server keys

bash
wg genkey | tee /etc/wireguard/server_private.key | wg pubkey > /etc/wireguard/server_public.key
chmod 600 /etc/wireguard/server_private.key

Step 3 — Configure the server

Create /etc/wireguard/wg0.conf:

bash
sudo nano /etc/wireguard/wg0.conf

Content:

ini
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
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

[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

Step 4 — Enable IP forwarding

bash
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Step 5 — Open firewall port

bash
sudo ufw allow 51820/udp

Step 6 — Start WireGuard

bash
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
sudo wg show

Step 7 — Configure the client

On your local machine, generate keys:

bash
wg genkey | tee client_private.key | wg pubkey > client_public.key

Create the client configuration:

ini
[Interface]
Address = 10.0.0.2/24
PrivateKey = CLIENT_PRIVATE_KEY
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_VPS_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Step 8 — Add more clients

For each new client, generate a key pair and add a [Peer] block on the server:

bash
sudo wg set wg0 peer NEW_CLIENT_PUBLIC_KEY allowed-ips 10.0.0.3/32

Verify connection

bash
# On the server
sudo wg show

# On the client (after connecting)
ping 10.0.0.1
curl ifconfig.me  # Should show the VPS IP

WireGuard gives you secure, encrypted access to your Baires Host VPS from anywhere.


Was this guide helpful?