Quick BASH function for adding WireGuard peers

Posted February 15, 2019

Last updated February 22, 2019 | 2327d16


1 minute read

If you’re like me and diving into WireGuard as it picks up more attention, you might be adding lots of WireGuard peers to a server as you’re figuring it out and playing around with it. On my server box, I added a function to my .bashrc to make it a bit easier to add peers.

If you’d like, copy the following (three, and simple at that) lines into your .bashrc:

1
2
3
wgadd () {
        sudo wg set wg0 peer $1 allowed-ips 10.20.30.$2/32
}

Then either log out or run source ~/.bashrc (or source from wherever your file is), and then run it with this example command:

wgadd aSM/g5dtoYAC9OgeWUWnRYxt0oeyRQu3DejJKEIdNSk= 89

This would run the command sudo wg set command on the WireGuard interface wg0 and add a peer with the public key of aSM/g5dtoYAC9OgeWUWnRYxt0oeyRQu3DejJKEIdNSk= allowed with the IP of 10.20.30.89.

You can adjust any of the bits of the function (mostly the interface name of wg0 and the IP range of 10.20.30.x) if needed on your system.