Skip to content

Instantly share code, notes, and snippets.

@lmlsna
Last active December 17, 2022 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lmlsna/e13570ab615f2fcabede93b4ef78d14d to your computer and use it in GitHub Desktop.
Save lmlsna/e13570ab615f2fcabede93b4ef78d14d to your computer and use it in GitHub Desktop.
Setup wireguard interface over ssh and return public key
#!/bin/bash
apt-get update &>/dev/null
if [[ "$(lsmod | grep wireguard)" -n ]]; then
export HAS_WG_MODULE=1
else
apt-get install wireguard wireguard-tools -y &>/dev/null
modprobe wireguard
fi
if [[ $(which wg &>/dev/null ; echo $?) -eq 0 ]]; then
export HAS_WG_BIN=1
else
apt-get install wireguard-tools -y &>/dev/null
fi
cd /etc/wireguard
wg genkey | tee private_key | wg pubkey > public_key
chmod 400 private_key public_key
ip link add wg0 type wireguard &>/dev/null
ip addr add $1 dev wg0 &>/dev/null
wg set private-key private_key listen-port $2 allowed-ips $3 &>/dev/null
ip link set up wg0 &>/dev/null
wg showconf wg0 > wg0.conf
chmod 600 wg0.conf
echo "$(cat /etc/wireguard/public_key)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment