Connect VPN Automatically on Linux
In this article
Yes, you can make a VPN connect automatically on Linux at boot or when you log in. The easiest way is to use NetworkManager's built-in auto-connect option, or a systemd service for OpenVPN/WireGuard. This guide covers both methods, plus how to set it up for a specific network or on a headless server.
What You'll Need
- A Linux system with either NetworkManager (most desktop distros) or systemd (all modern distros).
- A VPN configuration file: either a .ovpn file (OpenVPN) or a .conf file (WireGuard).
- Your VPN provider's server address, username, and password (if not already in the config).
- Root or sudo access to install packages and edit system files.
Step-by-Step
Method 1: NetworkManager (Desktop with GUI)
- Install the necessary VPN plugin. For OpenVPN, run
sudo apt install network-manager-openvpn-gnome(Debian/Ubuntu) or the equivalent for your distro. For WireGuard, installnetwork-manager-wireguardif available. - Open the network settings: click the network icon in the system tray and select Network Settings or VPN Connections > Add a VPN connection.
- Click the + button to add a new VPN. Choose OpenVPN or WireGuard from the list.
- Import your config file: click Import from file and select your .ovpn or .conf file. Fill in any missing fields (username, password, gateway).
- After adding, click the gear icon next to the VPN connection to edit its settings.
- Go to the General tab and check the box that says Connect automatically (or Automatically connect to VPN). Also check All users may connect if you want it system-wide.
- Save the settings, then reboot or log out and back in. The VPN should connect automatically.
Method 2: Systemd Service (Headless or Server)
- Install the VPN client. For OpenVPN:
sudo apt install openvpn. For WireGuard:sudo apt install wireguard(or via your distro's package manager). - Place your config file in the correct directory. For OpenVPN, copy your .ovpn file to
/etc/openvpn/client/and rename it toclient.conf(or keep the .ovpn extension). For WireGuard, copy your .conf file to/etc/wireguard/(e.g.,wg0.conf). - If your config contains a username/password, create a separate auth file. For OpenVPN, create
/etc/openvpn/client/auth.txtwith the username on the first line and password on the second. Then addauth-user-pass /etc/openvpn/client/auth.txtto the config file. - Enable the systemd service. For OpenVPN:
sudo systemctl enable openvpn-client@client. For WireGuard:sudo systemctl enable wg-quick@wg0. - Start the service now to test:
sudo systemctl start openvpn-client@clientorsudo systemctl start wg-quick@wg0. - Check status:
sudo systemctl status openvpn-client@clientorsudo systemctl status wg-quick@wg0. It should showactive (running). - Reboot to confirm it starts automatically.
Method 3: Auto-connect Only on Specific Networks
- In NetworkManager, edit the VPN connection as in Method 1.
- Go to the General tab, and instead of checking Connect automatically, check Automatically connect to VPN when using this connection.
- Then select the network(s) from the list (e.g., your home Wi-Fi). This way the VPN only auto-connects when you're on that network.
Common Mistakes
- Using the wrong config path: OpenVPN expects configs in
/etc/openvpn/client/(not directly in/etc/openvpn/on newer versions). WireGuard expects them in/etc/wireguard/. - Forgetting to enable the service: Running
systemctl startonly starts it once. You must runsystemctl enableto make it start at boot. - Not setting permissions on auth files: If the auth file is readable by other users, OpenVPN may refuse to start. Set
sudo chmod 600 /etc/openvpn/client/auth.txt. - Using a GUI method on a headless server: NetworkManager's GUI doesn't exist on servers. Use the systemd method instead.
- Overlooking DNS leaks: Auto-connect doesn't guarantee DNS is routed through the VPN. Check your DNS settings in the VPN config or use a kill switch.
Tips
- Test your VPN connection manually before setting up auto-connect to ensure the config works.
- For OpenVPN, use the
--daemonflag in the config if you want it to run in the background (though systemd handles this). - Use
journalctl -u openvpn-client@clientto view logs if something goes wrong. - If your VPN provider offers a kill switch feature, enable it in the config to prevent traffic leaks if the VPN drops.
- On laptops, consider using the NetworkManager method with per-network auto-connect to avoid VPN on public Wi-Fi where it might be blocked.
Troubleshooting
- VPN doesn't connect at boot: Check if the service is enabled:
systemctl is-enabled openvpn-client@client. If it saysdisabled, runsudo systemctl enableagain. - NetworkManager doesn't auto-connect: Make sure the VPN connection is set to Connect automatically and that the base network (Wi-Fi/Ethernet) is also set to auto-connect. Sometimes the VPN waits for the network to be fully up.
- Authentication errors: Verify your username/password in the auth file. For OpenVPN, ensure the auth file has no extra spaces or newlines.
- Service starts but no internet: Check routing:
ip route. If the default route isn't through the VPN, your config may be missingredirect-gateway def1(OpenVPN) orAllowedIPs = 0.0.0.0/0(WireGuard). - VPN connects but DNS fails: Add
dhcp-option DNS 8.8.8.8to your OpenVPN config, or setDNS = 8.8.8.8in WireGuard's [Interface]. - If all else fails: Restart the service with
sudo systemctl restart openvpn-client@clientand watch the logs withsudo journalctl -u openvpn-client@client -fto see the exact error.
Frequently Asked Questions
Can I make the VPN auto-connect only when I'm on an untrusted network?
Yes, with NetworkManager you can set the VPN to auto-connect only on specific Wi-Fi networks. For a more dynamic approach, you can write a script that checks the network name and starts the VPN accordingly, but that's more advanced.
Will the VPN auto-connect if I'm using a wired Ethernet connection?
Yes, the same NetworkManager method works for Ethernet. Just ensure the Ethernet connection is set to auto-connect, and the VPN will follow.
Do I need to store my password in plain text?
OpenVPN's auth file is plain text, but you can restrict permissions to root-only. WireGuard uses cryptographic keys, so no password is stored. Some providers also support OTP or certificate-based auth.
Can I use this with any VPN provider?
Yes, as long as you have a config file and the required credentials. Most commercial VPN providers offer OpenVPN or WireGuard configs for Linux.
What if I want to stop the auto-connect temporarily?
For systemd, run sudo systemctl disable openvpn-client@client to stop it from starting at boot. For NetworkManager, uncheck the Connect automatically box.
Does auto-connect work with multiple VPNs?
Yes, you can set up multiple VPN connections and enable auto-connect on each. They will all try to connect, which may cause conflicts. It's better to have only one auto-connect at a time.