Skip to content

Instantly share code, notes, and snippets.

@robinlandstrom
Last active September 6, 2024 05:08
Show Gist options
  • Save robinlandstrom/b111240cd74ecab4d358f28b2d4fd8de to your computer and use it in GitHub Desktop.
Save robinlandstrom/b111240cd74ecab4d358f28b2d4fd8de to your computer and use it in GitHub Desktop.
Script to automatically add configration for a new peer to a wireguard server. It will then print a QR code to the console that can be used to add the config to the Android or OS X wireguard client.
#!/bin/bash
# MIT License
#
# Copyright (c) 2024 Robin Landström
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
readonly INTERFACE="wg0"
# Generate peer keys
readonly PRIVATE_KEY=$(wg genkey)
readonly PUBLIC_KEY=$(echo ${PRIVATE_KEY} | wg pubkey)
readonly PRESHARED_KEY=$(wg genpsk)
# Read server key from interface
readonly SERVER_PUBLIC_KEY=$(wg show ${INTERFACE} public-key)
# Get next free peer IP (This will break after x.x.x.255)
readonly PEER_ADDRESS=$(wg show ${INTERFACE} allowed-ips | cut -f 2 | awk -F'[./]' '{print $1"."$2"."$3"."1+$4"/"$5}' | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 -n | tail -n1)
# Add peer
wg set ${INTERFACE} peer ${PUBLIC_KEY} preshared-key <(echo ${PRESHARED_KEY}) allowed-ips ${PEER_ADDRESS}
# Logging
echo "Added peer ${PEER_ADDRESS} with public key ${PUBLIC_KEY}"
# Generate peer config QR code
cat << END_OF_CONFIG | qrencode -t ANSIUTF8
[Interface]
Address = ${PEER_ADDRESS}
PrivateKey = ${PRIVATE_KEY}
DNS = 8.8.8.8 (Your internal DNS server here)
[Peer]
PublicKey = ${SERVER_PUBLIC_KEY}
PresharedKey = ${PRESHARED_KEY}
AllowedIPs = 0.0.0.0/0
Endpoint = example.com:443 (Your external Wireguard endpoint here)
END_OF_CONFIG
@laktosterror
Copy link

Hello!
Tried your script, it dont add anything to my "server"-interface. only generating the keys and the QR.

Do you have time to help?

Best regards

@plicit
Copy link

plicit commented Apr 20, 2022

I think that this line:

wg set ${INTERFACE} peer ${PUBLIC_KEY} preshared-key <(echo ${PRESHARED_KEY}) allowed-ips ${PEER_ADDRESS}

adds the client Peer info to the live configuration of the running server, which can be confirmed with wg show. It does not touch the persistent /etc/wireguard/wg0.conf file by itself.

If SaveConfig = True is set for the wg server, then when the wireguard ${INTERFACE} shuts down, the live config will be saved to /etc/wireguard/${INTERFACE}.conf

You can also manually export the live config to a file with wg-quick save wg0

https://manpages.debian.org/unstable/wireguard-tools/wg.8.en.html

https://manpages.debian.org/unstable/wireguard-tools/wg-quick.8.en.html

@colemar
Copy link

colemar commented Dec 22, 2022

Thank you for the inspiration.
I believe I made an improved version: wg-peer

@sskras
Copy link

sskras commented Sep 5, 2024

ping @robinlandstrom: under what license (if any) can I put your script, please? (should I decide to distribute it)

I asked the same question @colemar today, and if you also support putting your work into Public Domain, I believe explicitly mentioning CC0 here would be nice (because of the reasons: https://chrismorgan.info/blog/unlicense/)

Then, a permissive license like MIT or BSD* would do fine for me too :)

@robinlandstrom
Copy link
Author

@sskras Added MIT license to the snip. Go ahead and use it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment