Step 1: Install WireGuard
# Update system
sudo apt update && sudo apt upgrade -y
# Install WireGuard
sudo apt install wireguard resolvconf -y
Step 2: Generate Server Key Pair
# Navigate to WireGuard directory
cd /etc/wireguard/
# Generate private key and save
sudo umask 077
wg genkey | sudo tee privatekey | wg pubkey | sudo tee publickey
# Display keys (save these securely)
echo "=== SERVER PRIVATE KEY ==="
sudo cat privatekey
echo -e "\n=== SERVER PUBLIC KEY ==="
sudo cat publickey
Save both keys in a secure location!
Step 3: Create Server Configuration
Create configuration file:
sudo nano /etc/wireguard/wg0.conf
Add the following content (replace [SERVER_PRIVATE_KEY] with your private key from Step 2):
[Interface]
PrivateKey = [SERVER_PRIVATE_KEY]
Address = 10.8.0.1/24
ListenPort = 51820
SaveConfig = true
# DNS configuration
DNS = 1.1.1.1, 8.8.8.8
# Firewall and routing rules
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
Example with actual values:
[Interface]
PrivateKey = ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdef=
Address = 10.8.0.1/24
ListenPort = 51820
SaveConfig = true
DNS = 1.1.1.1, 8.8.8.8
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
Step 4: Enable IP Forwarding
# Edit sysctl configuration
sudo nano /etc/sysctl.conf
# Uncomment or add this line:
net.ipv4.ip_forward=1
# Apply changes
sudo sysctl -p
Step 5: Configure Firewall
# Allow WireGuard port
sudo ufw allow 51820/udp
# Allow SSH (don't forget!)
sudo ufw allow ssh
# Enable UFW
sudo ufw enable
# Check status
sudo ufw status
Step 6: Start WireGuard Service
# Start WireGuard interface
sudo wg-quick up wg0
# Enable auto-start on boot
sudo systemctl enable wg-quick@wg0
# Check status
sudo wg show
Step 7: Generate Client Configuration
7.1 Generate Client Keys
# Generate client keys
wg genkey | tee client_private.key
cat client_private.key | wg pubkey > client_public.key
echo "=== CLIENT PRIVATE KEY ==="
cat client_private.key
echo -e "\n=== CLIENT PUBLIC KEY ==="
cat client_public.key
7.2 Add Client to Server
# Add client to server configuration
sudo wg set wg0 peer $(cat client_public.key) allowed-ips 10.8.0.2/32
# Save configuration permanently
sudo wg-quick save wg0
7.3 Create Client Config File
Create ubuntu-client.conf file:
[Interface]
PrivateKey = [CLIENT_PRIVATE_KEY]
Address = 10.8.0.2/24
DNS = 1.1.1.1, 8.8.8.8
[Peer]
PublicKey = [SERVER_PUBLIC_KEY]
Endpoint = [SERVER_IP]:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
How to get SERVER_IP:
# For public IP (if you have one)
curl -4 ifconfig.me
# For local IP
ip addr show | grep inet
Step 8: Enable Services for Remote Access
8.1 Enable SSH (if not already enabled)
# Install SSH server if not present
sudo apt install openssh-server -y
# Enable SSH service
sudo systemctl enable ssh
sudo systemctl start ssh
# Check status
sudo systemctl status ssh
8.2 Optional: Enable VNC for GUI access
# Install VNC server
sudo apt install x11vnc -y
# Set VNC password
x11vnc -storepasswd
# Create VNC service
sudo nano /etc/systemd/system/x11vnc.service
Service file content:
[Unit]
Description=x11vnc service
After=display-manager.service network.target syslog.target
[Service]
Type=simple
ExecStart=/usr/bin/x11vnc -forever -display :0 -auth guess -passwd /home/$(whoami)/.vnc/passwd
ExecStop=/usr/bin/killall x11vnc
Restart=on-failure
[Install]
WantedBy=multi-user.target
# Enable VNC service
sudo systemctl daemon-reload
sudo systemctl enable x11vnc
sudo systemctl start x11vnc
Step 9: Testing
9.1 Test WireGuard Server
# Check interface
ip addr show wg0
# Check WireGuard status
sudo wg show
# Test port listening
sudo netstat -tulnp | grep 51820
9.2 Test from Client
After setting up the client, test the connection:
# Ping server
ping 10.8.0.1
# SSH through VPN
ssh username@10.8.0.1
Step 10: Automated Setup Script
Create setup-wireguard-ubuntu.sh file:
#!/bin/bash
echo "=== WireGuard Auto Setup for Ubuntu ==="
# Install dependencies
sudo apt update
sudo apt install wireguard resolvconf -y
# Generate keys
cd /etc/wireguard
sudo umask 077
sudo wg genkey | sudo tee privatekey | sudo wg pubkey | sudo tee publickey
SERVER_PRIVATE_KEY=$(sudo cat privatekey)
SERVER_PUBLIC_KEY=$(sudo cat publickey)
# Create config
sudo tee /etc/wireguard/wg0.conf > /dev/null <<EOF
[Interface]
PrivateKey = $SERVER_PRIVATE_KEY
Address = 10.8.0.1/24
ListenPort = 51820
SaveConfig = true
DNS = 1.1.1.1, 8.8.8.8
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
EOF
# Enable IP forwarding
echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# Setup firewall
sudo ufw allow 51820/udp
sudo ufw allow ssh
# Start service
sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0
echo "=== Setup Complete! ==="
echo "Server Public Key: $SERVER_PUBLIC_KEY"
echo "WireGuard interface: wg0"
echo "Server VPN IP: 10.8.0.1"
Make executable and run:
chmod +x setup-wireguard-ubuntu.sh
sudo ./setup-wireguard-ubuntu.sh
Step 11: Management Commands
Useful commands:
# Start/Stop WireGuard
sudo wg-quick up wg0
sudo wg-quick down wg0
# Check status
sudo wg show
sudo systemctl status wg-quick@wg0
# View logs
sudo journalctl -u wg-quick@wg0 -f
# Add client manually
sudo wg set wg0 peer [CLIENT_PUBLIC_KEY] allowed-ips 10.8.0.2/32
# Remove client
sudo wg set wg0 peer [CLIENT_PUBLIC_KEY] remove
Troubleshooting
If you encounter issues:
# Restart service
sudo wg-quick down wg0
sudo wg-quick up wg0
# Check kernel module
lsmod | grep wireguard
# Check firewall
sudo ufw status
# Check routing
ip route show
# Check interface status
ip link show wg0
Common Issues and Solutions:
- Interface not starting:
# Check if module is loaded
sudo modprobe wireguard
# Check for errors in config
sudo wg-quick down wg0
sudo wg-quick up wg0
- Connection issues:
# Check if port is open
sudo ufw status
# Verify keys match
sudo wg show
- No internet access through VPN:
# Check IP forwarding
cat /proc/sys/net/ipv4/ip_forward
# Check NAT rules
sudo iptables -t nat -L
With this setup, your Ubuntu laptop is ready to be securely accessed remotely through WireGuard VPN connection!
Top comments (0)