Requirements for OpenVPN autostart on systemd Linux
Before you begin setting up OpenVPN to autostart on systemd Linux, you’ll need a few prerequisites:
- A VPN provider
- A Linux distribution (distro) with systemd
Configure OpenVPN for systemd Linux
Now, let’s configure OpenVPN to autostart for systemd Linux. First open a terminal. We need to change the default behavior of OpenVPN. With the editor Nano, run the command:
sudo nano /etc/default/openvpn
Remove the ‘#’ infront of ‘AUTOSTART=”all”‘ so that OpenVpn allows to start the .conf files. After that press “Ctrl X” to exit Nano and answer “Y” to save the file.
configure openvpn to autostart – Systemd Linux
Run the following commands to show a list of possible vpn.ovpn files:
cd /etc/openvpn ls
Next, you need to decide which vpn.ovpn file you are going to auto connect to. The one you pick needs to be renamed to client.conf, because that’s the standard way of doing it in the OpenVPN world. Run this command:
sudo cp vpnserver.ovpn client.conf
Where it says “vpnserver.ovpn” replace it with an existing file, the name will differ from provider to provider. For example, de123.nordvpn.com.tcp443.ovpn if you’re using a VPN which requires you to login, like NordVPN does. Use this fix:
sudo nano client.conf
Change the line that says:
Press “Ctrl X” and “Y” in order to save your changes.
sudo nano .secrets
Insert your username on the first line and password one the second then “Ctrl X” and “Y” to save the file.
OpenVPN for systemd Linux: Enable the systemd service
Run the following command to enable the OpenVPN service to run while booting:
sudo systemctl enable firstname.lastname@example.org
Reload the daemons:
sudo systemctl daemon-reload
Start OpenVPN with the following command:
sudo service openvpn start
sudo systemctl start email@example.com
Stop OpenVPN with the following command:
sudo service openvpn stop
sudo systemctl stop firstname.lastname@example.org
Now it’s time to test if it worked. Reboot the system with the command:
To check the external IP run the command:
If ipinfo.io returns your VPN’s external ip, you’re done!