🌐 VPN Setup for Remote Machines with WireGuard
This guide helps you securely connect your remote machines using WireGuard VPN, allowing you to share files (NFS, etc.) as if they were on the same private network.
1️⃣ Install WireGuard on all machines
Section titled “1️⃣ Install WireGuard on all machines”Run the following on each machine (server and clients):
sudo apt updatesudo apt install -y wireguard2️⃣ Configure the VPN Server (Main machine X.X.X.X)
Section titled “2️⃣ Configure the VPN Server (Main machine X.X.X.X)”Create the configuration file:
sudo nano /etc/wireguard/wg0.confPaste the following:
...[Interface]Address = 10.0.0.1/24PrivateKey = <SERVER_PRIVATE_KEY>ListenPort = 51820
# Allow forwarding and NATPostUp = sysctl -w net.ipv4.ip_forward=1PostUp = iptables -A FORWARD -i wg0 -j ACCEPTPostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEPostDown = iptables -D FORWARD -i wg0 -j ACCEPTPostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]# Client machinePublicKey = <CLIENT_PUBLIC_KEY>AllowedIPs = 10.0.0.2/323️⃣ Configure the VPN Client (Other machine X.X.X.X)
Section titled “3️⃣ Configure the VPN Client (Other machine X.X.X.X)”Create the configuration file:
sudo nano /etc/wireguard/wg0.confPaste the following:
...[Interface]Address = 10.0.0.2/24PrivateKey = <CLIENT_PRIVATE_KEY>
[Peer]# VPN ServerPublicKey = <SERVER_PUBLIC_KEY>Endpoint = X.X.X.X:51820 # Replace with your VPN server IPAllowedIPs = 10.0.0.0/24PersistentKeepalive = 25🔑 Generate Keys on Each Machine
Section titled “🔑 Generate Keys on Each Machine”On each machine, run:
wg genkey | tee privatekey | wg pubkey > publickeyUse the generated keys in your configurations:
privatekey→<PRIVATE_KEY>publickey→ to give to the peer
🚀 Start and Enable VPN on Both Machines
Section titled “🚀 Start and Enable VPN on Both Machines”To start the VPN connection:
sudo wg-quick up wg0To enable the VPN automatically on boot:
sudo systemctl enable wg-quick@wg0✅ Verification
Section titled “✅ Verification”Test the VPN connection:
- From client:
Terminal window ping 10.0.0.1 - From server:
Terminal window ping 10.0.0.2