diff --git a/BashScripts/install-wireguard-server.sh b/BashScripts/install-wireguard-server.sh new file mode 100644 index 0000000..4d37802 --- /dev/null +++ b/BashScripts/install-wireguard-server.sh @@ -0,0 +1,117 @@ +# Install using: sudo su -c "bash <(wget -qO- /url/to/install-wireguard-server.sh)" + +# Make sure script is ran as root +if [[ $EUID -ne 0 ]]; then + exec sudo /bin/bash "$0" "$@" +fi +read -e -p "Enter lan NIC: " -i $(ip route | grep default | sed -e 's/^.*dev.//' -e 's/.proto.*//') NIC_NAME +read -e -p "Enter VPN subnet: " -i "192.168.200" VPN_SUBNET +read -e -p "Enter LAN subnet: " -i "192.168.0" LAN_SUBNET +read -e -p "Enter VPN public hostname: " -i "home.myspace.nu" VPN_PUBLIC_HOST + +if [ $(dpkg-query -W -f='${Status}' wireguard 2>/dev/null | grep -c "ok installed") -eq 0 ]; +then + echo "Installing Wireguard" + apt install wireguard +fi + +mkdir -m 0700 /etc/wireguard/ > /dev/null 2>&1 +cd /etc/wireguard/ + +if [ ! -f privatekey ] +then + echo "Generating private and public keys" + umask 077; wg genkey | tee privatekey | wg pubkey > publickey +fi +SERVER_PRIVATEKEY=$(cat "privatekey") +SERVER_PUBLICKEY=$(cat "publickey") +# sudo ufw allow 41194/udp + +if ! grep -q "net.ipv4.ip_forward" "/etc/sysctl.d/10-wireguard.conf"; then + echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.d/10-wireguard.conf + echo 'net.ipv6.conf.all.forwarding=1' | sudo tee -a /etc/sysctl.d/10-wireguard.conf + sysctl -p /etc/sysctl.d/10-wireguard.conf +fi + +sudo tee /etc/wireguard/wg0.conf.base > /dev/null < /etc/wireguard/wg0.conf + +if ! systemctl is-active --quiet "wg-quick@wg0.service" ; then + systemctl enable wg-quick@wg0 + systemctl start wg-quick@wg0 + sudo systemctl status wg-quick@wg0 + wg + ip a show wg0 +fi + + +sudo tee /usr/local/bin/wg-adduser.sh > /dev/null < /dev/null <> "/etc/wireguard/wg0.conf.clients" + +[Peer] +# Client: \$WGCLIENT +PublicKey = \$CLIENT_PUBLICKEY +AllowedIPs = \$ip/32 +LOE +cat /etc/wireguard/wg0.conf.base /etc/wireguard/wg0.conf.clients > /etc/wireguard/wg0.conf +cat /etc/wireguard/client-\$WGCLIENT.conf +wg-quick down wg0 && wg-quick up wg0 +EOL +chmod 777 /usr/local/bin/wg-adduser.sh + +sudo tee /usr/local/bin/wg-restart.sh > /dev/null <