CCCPaste

VM Wireguard Script

#!/bin/bash

# VPN Setup #Script for VM w Auto Detection

# =============================
# EDIT THIS SECTION for your VM subnet and Proxmox host IP
HOST_IP="192.168.1.76"           # Proxmox host vmbr0 IP
VM_SUBNET="192.168.1.0/24"       # VM network/subnet
WG_CONF="/etc/wireguard/wg0.conf" # Path to your existing WireGuard config on host
# =============================

echo "(1) Installing Packages..."
apt update
apt install -y wireguard-tools resolvconf iptables iproute2 curl sudo 

echo "(2) IP Forwarding..."
sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/99-ipforward.conf
sysctl --system

echo "(3) Wireguard NAT Setup..."
iptables -t nat -A POSTROUTING -s $VM_SUBNET -o wg0 -j MASQUERADE
apt install -y iptables-persistent
netfilter-persistent save
wg-quick up $WG_CONF

echo "(4) Configuring Gateway & DNS..."
VM_IF=$(ip -o link show | awk -F': ' '{print $2}' | grep -v lo | head -n1)
ip route replace default via $HOST_IP dev $VM_IF
# Set DNS to host VPN DNS (adjust if needed)
echo "nameserver 10.2.0.1" > /etc/resolv.conf


echo "Setup Complete!"