Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Connect VPN Automatically on Linux

· 5 min read · Home Networking

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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)

  1. 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, install network-manager-wireguard if available.
  2. Open the network settings: click the network icon in the system tray and select Network Settings or VPN Connections > Add a VPN connection.
  3. Click the + button to add a new VPN. Choose OpenVPN or WireGuard from the list.
  4. Import your config file: click Import from file and select your .ovpn or .conf file. Fill in any missing fields (username, password, gateway).
  5. After adding, click the gear icon next to the VPN connection to edit its settings.
  6. 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.
  7. Save the settings, then reboot or log out and back in. The VPN should connect automatically.

Method 2: Systemd Service (Headless or Server)

  1. Install the VPN client. For OpenVPN: sudo apt install openvpn. For WireGuard: sudo apt install wireguard (or via your distro's package manager).
  2. Place your config file in the correct directory. For OpenVPN, copy your .ovpn file to /etc/openvpn/client/ and rename it to client.conf (or keep the .ovpn extension). For WireGuard, copy your .conf file to /etc/wireguard/ (e.g., wg0.conf).
  3. If your config contains a username/password, create a separate auth file. For OpenVPN, create /etc/openvpn/client/auth.txt with the username on the first line and password on the second. Then add auth-user-pass /etc/openvpn/client/auth.txt to the config file.
  4. Enable the systemd service. For OpenVPN: sudo systemctl enable openvpn-client@client. For WireGuard: sudo systemctl enable wg-quick@wg0.
  5. Start the service now to test: sudo systemctl start openvpn-client@client or sudo systemctl start wg-quick@wg0.
  6. Check status: sudo systemctl status openvpn-client@client or sudo systemctl status wg-quick@wg0. It should show active (running).
  7. Reboot to confirm it starts automatically.

Method 3: Auto-connect Only on Specific Networks

  1. In NetworkManager, edit the VPN connection as in Method 1.
  2. Go to the General tab, and instead of checking Connect automatically, check Automatically connect to VPN when using this connection.
  3. 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 start only starts it once. You must run systemctl enable to 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 --daemon flag in the config if you want it to run in the background (though systemd handles this).
  • Use journalctl -u openvpn-client@client to 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

  1. VPN doesn't connect at boot: Check if the service is enabled: systemctl is-enabled openvpn-client@client. If it says disabled, run sudo systemctl enable again.
  2. 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.
  3. Authentication errors: Verify your username/password in the auth file. For OpenVPN, ensure the auth file has no extra spaces or newlines.
  4. Service starts but no internet: Check routing: ip route. If the default route isn't through the VPN, your config may be missing redirect-gateway def1 (OpenVPN) or AllowedIPs = 0.0.0.0/0 (WireGuard).
  5. VPN connects but DNS fails: Add dhcp-option DNS 8.8.8.8 to your OpenVPN config, or set DNS = 8.8.8.8 in WireGuard's [Interface].
  6. If all else fails: Restart the service with sudo systemctl restart openvpn-client@client and watch the logs with sudo journalctl -u openvpn-client@client -f to 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.