Requirements for OpenVPN autostart on systemd Linux
Before you begin setting up OpenVPN to autostart on systemd Linux, you’ll need a few prerequisites:
- OpenVPN
- 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:
auth-users-pass
to:
auth-users-pass .secrets
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.
username
password
OpenVPN for systemd Linux: Enable the systemd service
Run the following command to enable the OpenVPN service to run while booting:
sudo systemctl enable openvpn@client.service
Reload the daemons:
sudo systemctl daemon-reload
Start OpenVPN with the following command:
sudo service openvpn start
or
sudo systemctl start openvpn@client.service
Stop OpenVPN with the following command:
sudo service openvpn stop
or
sudo systemctl stop openvpn@client.service
Now it’s time to test if it worked. Reboot the system with the command:
reboot
To check the external IP run the command:
curl ipinfo.io
If ipinfo.io returns your VPN’s external ip, you’re done!