Created
October 4, 2024 12:59
-
-
Save DarkVss/22e8f3ef38ef5a4c0f6216ace734d40c to your computer and use it in GitHub Desktop.
Wireguard new client creation
This file contains hidden or 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 | |
MASK=32 # wireguard network mask | |
DNS="8.8.8.8" | |
SERVER_IP="<SERVER_IP>" | |
SERVER_PORT="<SERVER_PORT>" # default is 51820 | |
cd /etc/wireguard/ | |
mkdir -p clients | |
read -p "> Client name: " clientName | |
while true; do | |
if [[ -n $clientName ]]; then | |
if ! test -f clients/$clientName.conf; then | |
break | |
else | |
echo "! Client with name as '$clientName' already exist" | |
fi | |
else | |
echo "! Client name can not be empty" | |
fi | |
read -p "> Client name: " clientName | |
done | |
read -p "> Client IP(for 10.0.0.X): " clientIP | |
while true; do | |
if [[ ! -n ${clientIP//[0-9]/} && clientIP -gt 1 && clientIP -lt 256 ]]; then | |
break | |
fi | |
echo "! Client IP must be integer between 2 and 255" | |
read -p "> Client Client IP(for 10.0.0.X): " clientIP | |
done | |
read -p "> Server public key: " serverPublicKey | |
clientPrivateKey="$(wg genkey)" | |
clientPublicKey="$(echo $clientPrivateKey | wg pubkey)" | |
CLIENT_CONFIG="# $clientName | |
[Interface] | |
PrivateKey = $clientPrivateKey | |
#PublicKey = $clientPublicKey | |
Address = 10.0.0.$clientIP/$MASK | |
DNS = $DNS | |
[Peer] | |
PublicKey = $serverPublicKey | |
AllowedIPs = 0.0.0.0/0, ::/0 | |
Endpoint = $SERVER_IP:$SERVER_PORT | |
PersistentKeepalive = 20" | |
echo "$CLIENT_CONFIG" > clients/$clientName.conf | |
echo " | |
# $clientName | |
[Peer] | |
PublicKey = $clientPublicKey | |
AllowedIPs = 10.0.0.$clientIP/$MASK" >> wg0.conf | |
echo "New config: | |
$CLIENT_CONFIG | |
Now restart Wireguard service" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment