Skip to content

Instantly share code, notes, and snippets.

@sshaplygin
Created May 28, 2023 10:38
Show Gist options
  • Save sshaplygin/971f951327549beac1132a37d942db59 to your computer and use it in GitHub Desktop.
Save sshaplygin/971f951327549beac1132a37d942db59 to your computer and use it in GitHub Desktop.
create new user for wireguar server
#!bin/bash
NEW_USER=$NEW_USER_NAME
LAST_IP=$LAST_LOCAL_IP_SERVER_CONFIGURE
cd /etc/wireguard && mkdir "${NEW_USER}"
wg genkey | tee /etc/wireguard/"${NEW_USER}"/privatekey | wg pubkey | tee /etc/wireguard/"${NEW_USER}"/publickey
echo "configured keys for new user:" "${NEW_USER}"
IP="10.0.0.${LAST_IP}/32"
CLIENT_PUBLIC_KEY="$(cat /etc/wireguard/"${NEW_USER}"/publickey)"
arr=(
''
"# ${NEW_USER}"
'[Peer]'
"PublicKey = ${CLIENT_PUBLIC_KEY}"
"AllowedIPs = ${IP}"
);
printf '%s\n' "${arr[@]}" >> wg0.conf
echo "updated wg0.conf"
CLIENT_PRIVATE_KEY="$(cat /etc/wireguard/"${NEW_USER}"/privatekey)"
DNS='8.8.8.8'
SERVER_PUBLIC_KEY="$(cat /etc/wireguard/publickey)"
SERVER_IP=$SERVER_IP
SERVER_PORT=$SERVER_PORT
arr=(
'[Interface]'
"PrivateKey = ${CLIENT_PRIVATE_KEY}"
"Address = ${IP}"
"DNS = ${DNS}"
''
'[Peer]'
"PublicKey = ${SERVER_PUBLIC_KEY}"
"Endpoint = ${SERVER_IP}:${SERVER_PORT}"
'AllowedIPs = 0.0.0.0/0'
'PersistentKeepalive = 20'
);
printf '%s\n' "${arr[@]}" >> /etc/wireguard/"${NEW_USER}/${NEW_USER}.conf"
systemctl restart wg-quick@wg0.service
echo "restarted wg0.service"
systemctl status wg-quick@wg0.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment