To set up a WireGuard VPN on a VPS, you'll need to follow these steps:
- Install the WireGuard software on your VPS. The exact commands will vary depending on the Linux distribution you're using, but for Ubuntu, you can use the following command:
sudo apt-get update
sudo apt-get install wireguard
- Generate a public and private key pair for both the server and the client. On the server:
sudo wg genkey | tee serverprivatekey | wg pubkey > serverpublickey
On the client:
wg genkey | tee clientprivatekey | wg pubkey > clientpublickey
- Create a
wg0.conffile on both the server and the client. On the server, you'll need to specify the server's public key, the allowed IPs for clients, and the pre-shared key (PSK). Here's an example:
[Interface]
PrivateKey = (server private key)
Address = 10.0.0.1/24
ListenPort = 51820
[Peer]
PublicKey = (client public key)
AllowedIPs = 10.0.0.2/32
Endpoint = (client public IP):51820
PersistentKeepalive = 25
On the client, you'll need to specify the client's private key, the server's public key, the allowed IPs, and the endpoint of the server. Here's an example:
[Interface]
PrivateKey = (client private key)
Address = 10.0.0.2/24
[Peer]
PublicKey = (server public key)
Endpoint = (server public IP):51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25
- Start the WireGuard service on the server and the client:
sudo systemctl start wg-quick@wg0
sudo systemctl enable wg-quick@wg0
- Verify the connection on both the server and the client:
sudo wg show
You should see the peer's public key and the established connection.
For more information, refer to the WireGuard documentation.
References: