GeneC
GeneC

Reputation: 103

VPN killswitch using UFW, but now openvpn3 no longer can start automatically

I successfully implemented this, which blocks all internet connections on my Linux machine UNLESS it connects via a specific VPN : https://www.comparitech.com/blog/vpn-privacy/how-to-make-a-vpn-kill-switch-in-linux-with-ufw/

If I manually execute openvpn3 session-start --config ~/Desktop/config.ovpn, it successfully connects via the VPN.

I used to have this command in a script (that has #!/bin/bash as header) which ran at device bootup without any issues, UNTIL I configured ufw for the killswitch above (now ufw runs on device bootup).

I use openvpn3 so using instructions in the above tutorial for openvpn commands didn't work at all.

I even tried using a sleep in my bash script to get it to wait a while until after bootup. Doesn't work. But if I issue the connection command manually in the command prompt, it works.

Please help! I need it to connect automatically. Much appreciated!

Upvotes: 0

Views: 275

Answers (1)

GeneC
GeneC

Reputation: 103

After spending a whole day on this, I figured out a solution. I found an article that guided me : https://www.howtogeek.com/687970/how-to-run-a-linux-program-at-startup-with-systemd/

I set up a service item using systemd (systemctl) just for that command to connect. Here is what my entry looks like :

#/etc/systemd/system/connectvpn.service

[Unit]
Description=Connect VPN

After=ufw.service network.target
Requires=ufw.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/connect
#/usr/local/bin/connect

#!/bin/bash
openvpn3 session-start --config /home/xyz/Desktop/config.ovpn

Working nicely now, connects to the VPN on bootup.

Upvotes: 0

Related Questions