Hackernoon logoRunning openvpn as a systemd service. by@mikefettis

Running openvpn as a systemd service.

mike fettis Hacker Noon profile picture

@mikefettismike fettis

photo by: mike fettis

VPNs are all the rage. All the cool kids are using them. The coolest kids only use a terminal on their linux. How about running openvpn as a systemd service and have an interactive prompt to let you know that your vpn is running?

Enter systemd, and PS1.
The trick is to run the openvpn command as a systemd service. Then set it to auto-restart 15 seconds after failure. If we add a line to the opvn config file, auth-user-pass /home/linuxuser/pass.txt and then have the openvpn username and password in said txt file openvpn will be able to automatically connect to the vpn host. Finally, the icing on the cake is to update my PS1 (terminal prompt) to change color if the vpn is connected. This can be accomplished two different ways. 
1). If the traceroute resolves the first hop to a 10.* ip address then I know that my vpn is connected
2). If the service is in an active state then I know its running.

The in depth process is below.

There you have it, the vpn service will start on boot and will remain running all the time. If there is an issue then it will autorestart after 15 seconds. Just as a safeguard to make sure that all is good because we always want to know if the vpn is running ;) our terminal prompt will change colors based on the situation. A further enhancement can be done if you don’t want the entire terminal to change color and just hav ea symbol. This can be done if you look at the git integration that was done by, magicmonty. Hope this was a quick and easy read, and good luck with all your VPN-ing!



Join Hacker Noon

Create your free account to unlock your custom reading experience.