Workaround for OpenVPN and Wireguard not connecting after boot

Started by jesmon, February 24, 2024, 10:03:24 AM

Previous topic - Next topic
I have been battling with a problem for a while where after a reboot my OpenVPN and Wireguard client connections would not connect and would not recover without manually stopping everything and then starting manually.

I looked at logs and scoured google without being able to work out the root cause (I am a novice and the issue is probably logged and I just didn't understand it!). Once the connections are up, they are solid and if they do drop out they will self-recover without issue. But, it was not wife and family friendly that if there was a power outage, I had to be home to manually restart the wireguard/openvpn connections to have internet (all external traffic is routed through VPN).

My workaround is to create a script that runs at startup and automatically stops the openvpn and wireguard connections and then starts them one at a time.

I created a script at /usr/local/etc/rc.syshook.d/start/ called 93-restart-vpn with the following code:


#!/bin/sh

# Log file path
log_file="/var/log/restart_vpn.log"

# Timestamp
timestamp=$(date +"%Y-%m-%d %T")

# Redirect stdout and stderr to the log file
exec >> "$log_file" 2>&1

echo "=== Script started at: $timestamp ==="

echo  "Restarting WireGuard and OpenVPN..."

# Wait 20 seconds before running the script
sleep 20

# Stop WireGuard and OpenVPN services - adjust the OpenVPN numbers to whatever client is relevant to your setup
pluginctl -s wireguard stop
pluginctl -s openvpn stop 3
pluginctl -s openvpn stop 4

# Wait for a few seconds
sleep 5

# Start WireGuard
pluginctl -s wireguard start

# Wait for a 15 seconds
sleep 15

# Start OpenVPN connection client3
pluginctl -s openvpn start 3

# Wait for a 15 seconds
sleep 15

# Start OpenVPN connection client4
pluginctl -s openvpn start 4

echo "done."




Make sure to make the script executable chmod +x 93-restart-vpn Next time you reboot, 20 seconds after reboot it will stop all wireguard services and the openvpn clients you specify and then bring them up one by one. I haven't played with the timings yet and they are probably very conservative but I am happy for it to be down for a minute after reboot.

Just posting if anyone has a similar issue and wants to use the same workaround. I would also be happy to get advice on improvements to this or a better way of solving my issue.