Wireguard Admin Scripts

Published on 21 Jan 2025

Both of the scripts are copied and developed out from https://github.com/davidgross/ - so all creds to David Gross :)

Scripts to provision Wireguard clients. Written to run on the same Linux computer as the Wireguard server. Focus is on daily use of the basics - add and delete peers/clients.

There are mainly 1 bash script and 2 template scripts to help you administer your wireguard instance:

The make_install.sh generates two scripts based on user input and template scripts:

And last but not least a directory called 'clients' containing:

Considerations

I've had BIG headaches trying to configure a self-hosted VPN server, and I've had many errors in the process, so to help out some possible mistakes I will make a few bulletpoints to take into consideration that might help:

Pre requirements

  1. Wireguard, installed on server and basic wg0 is configured (https://www.wireguard.com/install/).
  2. qrencode, for more ease of use when generating and distributing client configurations. (apt install qrencode)
add-client
Description

Script to generate client keys and config files as well as set it up on the wireguard server. The script also makes a check if your client name already is in use. It generates a qr code in the terminal for easy setup on a mobile phone. It also saves a qr png for a more portable use. All client configurations are stored on the server, by default under '/etc/wireguard/clients/'.

Supports:

Variable Examples

WG_DIR='/etc/wireguard'
SERVER_ADDRESS='public ip'
SERVER_PORT='51820'
SERVER_WG_IF='wg0'
SERVER_PUBLIC_KEY='server_public.key'
SERVER_PUB_KEY=$(cat $WG_DIR/$SERVER_PUBLIC_KEY)
CLIENT_WG_IF='wg0'
CLIENT_DIR='/etc/wireguard/clients'
CLIENT_IP='192.168.5.' # Last octet is automagically filled with correct IP read from the file last-ip.txt
WG_TEMPLATE=$WG_DIR/wg0-template.conf
WG_TEMPLATE_PREKEY=$CLIENT_DIR/wg0-template-prekey.conf
LAST_IP=$CLIENT_DIR/last-ip.txt
CLIENT_NAME=''
CLIENTS=($(wg show $SERVER_WG_IF peers | awk '{print $2}' | tr -d '()' | sed '/^[[:blank:]]*$/d'))
WG_REREAD='YES'
WG_PREKEY='YES'
        
delete-client
Description

Script to delete the a client from the wireguard server. It also deletes any previously generated and stored configuration files such as public/private key and wg0.conf-file.

Variable Examples

WG_DIR='/etc/wireguard'
CLIENT_DIR='/etc/wireguard/clients'
SERVER_WG_CONF='wg0.conf'
SERVER_WG_IF='wg0'
ARRAY_FILE='/tmp/WG_array'
WG_DELETE_PEER='/tmp/WG_delete_peer'
        

Contributing

I'm open for any feedback. If you have any questions - don't hesitate to ask. Feel free to copy and use whatever you need in your own script. 😃