wg-connect
Bring a WireGuard tunnel up and down on BusyBox-based systems without
wg-quick, systemd, or bash.
Requirements
wg- WireGuard userspace toolip- iproute2 or BusyBoxipgrep- BusyBox grep (with-Esupport)- POSIX
/bin/sh
Root privileges are required (the script creates and configures network interfaces).
Installation
make install # to /usr/local
make install PREFIX=/usr # to /usr
make install DESTDIR=/tmp/pkg # for packaging
Or manually:
install -m 0755 wg-connect /usr/local/bin/wg-connect
install -m 0644 wg-connect.1 /usr/local/share/man/man1/wg-connect.1
Usage
wg-connect up <config> # bring a tunnel up
wg-connect down [name] # tear a tunnel down
up requires a config argument, resolved as follows:
| Argument | Resolves to |
|---|---|
myvpn |
/etc/wireguard/myvpn.conf |
myvpn.conf |
./myvpn.conf, then /etc/wireguard/myvpn.conf |
./foo.conf |
./foo.conf (path with / - used as-is) |
/any/path/c.conf |
/any/path/c.conf (absolute path) |
The interface name is derived from the config file's basename (minus
.conf). For example, wg-connect up myvpn creates interface myvpn,
and wg-connect up /etc/wireguard/home.conf creates interface home.
down accepts an optional name to tear down a specific tunnel.
Without a name it defaults to wg0.
Config format
Standard WireGuard [Interface] + [Peer] sections. wg-quick
extensions (Address, DNS, etc.) are accepted. The script strips
them before passing the config to wg setconf and handles them itself.
[Interface]
Address = 10.0.0.2/24
PrivateKey = oBKGh1W0UeO7R2aV5pLkdMn8Xq3TcFyRbzJwZsPvCg=
DNS = 1.1.1.1
[Peer]
PublicKey = xTIBA5rboUvnH4htjbDs6TFeFHS4k0mrKV4xJCzO0H8=
PresharedKey = /pUcv4j6DZ1UGm0PwR7aBr9Lk2sFdXq3OcVy5Jh8Tg=
Endpoint = 203.0.113.45:51820
AllowedIPs = 0.0.0.0/0
Multiple [Peer] sections are supported; AllowedIPs and Endpoint
values are accumulated across all of them.
Note: if you run multiple tunnels, each must have a unique ListenPort
(or omit it entirely). The script detects port conflicts before
setting up the interface.
What it does
up
- Resolves the config path and derives the interface name.
- Checks for port conflicts with any existing WireGuard interface.
- Parses the config for
Address,DNS,Endpoint,AllowedIPs, andListenPort. - Adds explicit routes for each peer endpoint through the current default gateway, so encrypted UDP packets are not caught in the tunnel's own routing.
- Creates the WireGuard interface and applies the config.
- Assigns the
Address(appending/32if no CIDR is given). - Brings the interface up.
- Installs routes for each
AllowedIPsentry.0.0.0.0/0replaces the default route. - If
DNSis set, backs up/etc/resolv.confand writes the VPN nameserver. - Saves state to
/tmp/wg-connect.<iface>.statefor teardown.
If any step fails, the trap handler rolls back everything created so far (interface, endpoint routes, state file).
down
- Reads
/tmp/wg-connect.<name>.state. - Restores the original
/etc/resolv.conf. - Removes the endpoint-specific routes.
- Deletes the interface (which also removes its addresses and routes).
- Restores the original default route if one was saved.
- Removes the state file.
If the state file is missing but the interface still exists, down
falls back to cleaning up the leftover interface.
Limitations
- IPv6 addresses and routes are skipped (BusyBox
ipmay lack-6support). DNSsupports a single nameserver only.- PostUp / PostDown / PreUp / PreDown hooks are not executed.
- No firewall rules are added. If you need a kill switch, configure
iptablesseparately.