Last active
November 14, 2023 08:57
-
-
Save vmrfriz/165be8ff8716d4e01d229387b9e255a5 to your computer and use it in GitHub Desktop.
Ubuntu wireguard installation script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Install repo | |
sudo apt install -y software-properties-common | |
sudo add-apt-repository ppa:wireguard/wireguard | |
# Update server | |
apt update | |
apt upgrade -y | |
# Change SSH port | |
sed -i 's/#Port 22/Port 5522/g' /etc/ssh/sshd_config | |
systemctl restart sshd | |
# Change net.ipv4.ip_forward option | |
sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf | |
sysctl -p | |
# Install packages | |
apt install -y curl wget micro wireguard wireguard-tools wireguard-dkms qrencode | |
# Server key | |
mkdir ~/wireguard | |
cd ~/wireguard | |
wg genkey | tee server | wg pubkey > server.pub | |
# Setup Wireguard | |
IP_START=1 | |
IP_LAST=$IP_START | |
SERVER_IP=$(curl -s https://ipinfo.io/ip) | |
VPN_PORT=54321 | |
# wg0.conf | |
CONNECTION_NAME=$(ip -o -4 route show to default | awk '{print $5}') | |
echo "[Interface] | |
Address = 10.10.10.$IP_START/24 | |
SaveConfig = true | |
ListenPort = $VPN_PORT | |
PrivateKey = $(cat server) | |
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o $CONNECTION_NAME -j MASQUERADE | |
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o $CONNECTION_NAME -j MASQUERADE | |
" > /etc/wireguard/wg0.conf | |
IP_LAST=$((IP_LAST + 1)) | |
chmod 600 /etc/wireguard/wg0.conf | |
createUser(){ | |
wg genkey | tee $1 | wg pubkey > $1.pub | |
chmod 600 $1 | |
chmod 600 $1.pub | |
echo "# $1 | |
[Peer] | |
PublicKey = $(cat $1.pub) | |
AllowedIPs = 10.10.10.$IP_LAST/32 | |
" >> /etc/wireguard/wg0.conf | |
echo "[Interface] | |
Address = 10.10.10.$IP_LAST/32 | |
PrivateKey = $(cat $1) | |
DNS = 1.1.1.1 | |
[Peer] | |
PublicKey = $(cat server.pub) | |
Endpoint = $SERVER_IP:$VPN_PORT | |
AllowedIPs = 0.0.0.0/0 | |
PersistentKeepalive = 25" > "${1}.conf" | |
chmod 600 "${1}.conf" | |
IP_LAST=$((IP_LAST + 1)) | |
qrencode -t ansiutf8 < $1.conf | |
echo "Config for '${1}' saved to $(pwd)/${1}.conf" | |
} | |
# Client keys | |
CONFIGS=() | |
echo "Enter config names. Empty string to end:" | |
while | |
read line | |
[[ $line ]] | |
do | |
CONFIGS+=("$line") | |
done | |
for i in "${CONFIGS[@]}" | |
do | |
createUser "$i" | |
done | |
# wg syncconf wg0 /etc/wireguard/wg0.conf | |
wg-quick up wg0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment