Skip to main content

Command Palette

Search for a command to run...

🔐 Create Your Own Secure VPN: A Step-by-Step Guide to Setting Up WireGuard on a VPS

Updated
5 min read
🔐 Create Your Own Secure VPN: A Step-by-Step Guide to Setting Up WireGuard on a VPS
M

I’m Insaf Nilam, a full-stack developer passionate about crafting clean, efficient, and future-ready software. I love solving complex problems, exploring new tech stacks, and sharing my learnings through blogs. When I’m not coding, I’m probably tweaking deployments, experimenting with microservices, or geeking out over cloud architecture.

In today’s digital age, online privacy is more important than ever. While commercial VPNs are convenient, they often come with trust issues—your data passes through third-party servers. Setting up your own VPN server gives you full control over your traffic, security, and privacy.

In this guide, we’ll walk you through setting up WireGuard, a modern, fast, and secure VPN, on a VPS.


Why WireGuard?

WireGuard is becoming the go-to VPN protocol for personal and enterprise use. Here's why:

  • Simplicity: Small codebase (~4,000 lines), making it easy to audit.

  • Speed: Extremely fast and lightweight, often outperforming OpenVPN.

  • Security: Uses state-of-the-art cryptography with minimal attack surface.

  • Cross-platform: Works on Linux, Windows, macOS, iOS, and Android.


Why Run Your Own VPN?

Running your own VPN server comes with several benefits:

  • Full control over your data: No third-party logging or tracking.

  • Customizable: Decide firewall rules, DNS, and routing policies.

  • Educational: Gain hands-on experience with networking, encryption, and Linux administration.

  • Cost-effective: A single VPS (~$5/month) can serve your personal VPN needs.

Limitations:

  • Limited to a single location unless you deploy multiple VPS instances.

  • Requires basic sysadmin knowledge.

  • Lacks commercial VPN features like automatic “kill switches” or multi-location servers.


Prerequisites

Before starting, ensure you have:

  • A VPS running Ubuntu 22.04 LTS

  • Root or sudo access

  • Basic familiarity with Linux command-line operations

Tip: Consider reviewing VPS hardening practices for extra security.


Step 1: Install WireGuard

SSH into your VPS and run:

sudo apt update
sudo apt install wireguard -y

Step 2: Generate Server Keys

WireGuard uses public/private key pairs for authentication.

wg genkey | sudo tee /etc/wireguard/privatekey
sudo chmod 600 /etc/wireguard/privatekey
sudo cat /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey

Keep the private key secure. The public key will be shared with clients.


Step 3: Configure the Server

Create the WireGuard interface and assign an IP:

sudo ip link add wg0 type wireguard
sudo ip addr add 10.0.0.1/24 dev wg0
sudo wg set wg0 private-key /etc/wireguard/privatekey
sudo ip link set wg0 up

Step 4: Add a Client Peer

On the Client

  1. Install WireGuard from the official website.

  2. Create a new tunnel to generate client keys.

Example client configuration:

[Interface]
# The private key for this client.
PrivateKey = <client-private-key>

# The client's IP address on the WireGuard VPN network.
# The /24 subnet mask means it's part of a network that can have up to 254 devices.
Address = 10.0.0.2/24

# Sets the DNS server to use for all traffic routed through this interface.
# 1.1.1.1 is Cloudflare's public DNS. You can also use your server's IP (e.g., Address = 10.0.0.1) if it's a DNS server.
DNS = 1.1.1.1

[Peer]
# The public key of the server you are connecting to.
PublicKey = <server-public-key>

# Defines which IP addresses will be routed through the VPN tunnel.
# 0.0.0.0/0, 128.0.0.0/1 is the long way to say "all IPv4 traffic."
# It's better to just use 0.0.0.0/0 to route all IPv4 traffic.
# Using ::/0 would add all IPv6 traffic.
AllowedIPs = 0.0.0.0/0

# The public IP address and port of the server.
# The server's port WireGuard is listening on (check with `sudo wg`).
Endpoint = <server-ip>:<wg-listening-port>

# This sends a small "keep-alive" packet to the server every 25 seconds.
# This is crucial for clients behind a NAT (like a home router) to prevent the
# connection from being dropped due to inactivity.
PersistentKeepalive = 25

AllowedIPs = 0.0.0.0/0 routes all IPv4 traffic through the VPN.

On the Server

Add the client as a peer:

sudo wg set wg0 peer <client-public-key> allowed-ips 10.0.0.2/32

Step 5: Enable WireGuard at Boot

Enable the service and IP forwarding:

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

ip route get 1.1.1.1 # Traces the route a packet takes to reach the internet.

Set up NAT with iptables:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo apt install iptables-persistent -y
sudo netfilter-persistent save

Save the configuration for persistence:

sudo wg showconf wg0 > /etc/wireguard/wg0.conf # switch as root
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0

Reboot your server to confirm persistence.


Step 6: Test the Connection

On the client:

ping 10.0.0.1   # Test internal VPN connectivity
curl ifconfig.me # Should show your VPS public IP

If both commands succeed, your VPN is working perfectly.


Step 7: Disable IPv6 to Avoid Leaks

WireGuard typically routes IPv4 only. If IPv6 is enabled on the client, your real IP can leak.

On Windows:

  1. Control Panel → Network and Internet → Network Connections

  2. Right-click your network adapter → Properties

  3. Uncheck Internet Protocol Version 6 (TCP/IPv6)

On Linux, you can disable IPv6 in /etc/sysctl.conf:

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
sudo sysctl -p

Step 8: Wrap-up & Security Recommendations

  • Keep your VPS firewall enabled (UFW or iptables).

  • Regularly update your VPS and WireGuard to patch vulnerabilities.

  • Optionally, configure WireGuard to handle IPv6 safely.

  • For official guidance and additional configuration tips, refer to the WireGuard Quickstart Guide.


Conclusion

Running your own WireGuard VPN gives you privacy, security, and full control—at a fraction of the cost of commercial providers.

Not only does this setup protect your online activity, but it also helps you learn valuable networking and Linux skills.

You’re not just connecting securely—you’re learning how the internet works under the hood.

More from this blog

I

Insaf’s Dev Journal

28 posts