How to keep OpenVPN in systemctl always connected when the internet is disconnected or the IP address is changed from the ISP will prevent this from happening
Note: Some scripts may not work due to `systemd failed` or duplication of `systemd timer` and `restart=always` services.
To ensure OpenVPN has systemd installed, if it's not already there, keep the OpenVPN Client and systemd updated.
Steps:
1. Install the OpenVPN client on Ubuntu/Debian, and always install and update systemd/systemctl to ensure continuous VPN operation in the background.
2. To run OpenVPN with systemd/systemctl, create: [example name: client] to activate.
# Copy the .ovpn file to the OpenVPN config directory
sudo cp /path/to/client1.ovpn /etc/openvpn/client/client1.conf
# Note: systemd expects .conf extension, not .ovpn
# Set restrictive permissions (it contains private keys)
sudo chmod 600 /etc/openvpn/client/client1.conf
# Enable and start the service
# The service name is openvpn-client@[config-name-without-extension]
sudo systemctl enable openvpn-client@client1
sudo systemctl start openvpn-client@client1
# Check status
sudo systemctl status openvpn-client@client1
3. Add "Restart=always" to the systemd/systemctl [openvpn-client] file. Stop the VPN before editing: "nano /usr/lib/systemd/system/openvpn-client@.service", save with "ctrl + o", and exit. After saving, reload the daemon with the command "systemctl daemon-reload".
Adding "Restart=always" prevents OpenVPN in systemd from crashing unnecessarily and avoids redirecting the internet connection to the ISP after losing internet connection to the VPN.
If you want to switch to a new VPN, stop the script in crontab, stop systemd (OpenVPN client), and follow step 1.
4. Several reasons can cause this, such as: disconnecting the internet cable, Wi-Fi, and changing IP addresses from your ISP. To avoid being disconnected from the internet via VPN, create a Linux script shell to check the VPN access status and resolve issues related to VPN internet connection disconnections.
Create a .sh file, copy and paste the script into it, and save the file as follows:
#If connecting via Ethernet and Wi-Fi, change the interface, for example: enp6s0
if nmcli connection show --active | grep "enp6s0" > /dev/null
then
if ping -c 1 8.8.8.8 > /dev/null
then
exit 0
else
systemctl restart openvpn-client@client.service > /dev/null
fi
else
exit 0
fi
*After creating the "sh" script file, make the script executable.*
Run: chmod +x [filename].sh
Note: DNS 8.8.8.8 checks whether an IP address can access the internet via the VPN. If available or unavailable, it resolves these issues to maintain a consistently working VPN connection.
5. Configure crontab to make the script check VPN access and fix the internet connection drop for 1 minute before reconnecting online.
That's all. OpenVPN can now maintain a constant VPN connection. No further action is required. Wait for the script to complete for 1 minute before reconnecting to the VPN again if internet access is lost. This will definitely help.
If you want to switch to a new VPN, stop the script in crontab, stop systemd (OpenVPN client), and follow step 1.
Comments
Post a Comment