VPN Connections don’t appear to work when you first boot up

When first getting to the desktop, I wouldn’t have a working connection to the internet. It was strange, as it worked before. However - a few things had changed. Firstly I noticed that Network Manager had updated. Secondly I had distrohopped to Fedora.

Some background, I’m a Surfshark user and I have the VPNs set up manually using Wireguard. These files can be downloaded from Surfshark’s website and only a few tweaks are needed to get them working, before importing them into the Network Manager GUI.

So I’m not sure if Fedora or the update caused the DNS on my VPN to stop working, but I did notice that if I disconnected the VPN, then reconnected, it worked as normal.

I messed about with systemd-resolved and the NetworkManager services for over an hour but just couldn’t get the DNS to work first time, every time. What makes matters worse, is when my VPN wasn’t connected, I got spammed by CloudFlare on Surfshark’s website, which is really annoying when you’re trying to troubleshoot.

The (not so) elegant fix

I really only found one way after hours of messing around with services and configuration changes. Delay the VPN starting. It’s a bit hacky and it doesn’t answer the question as to why. But sometimes, you need something to get the job done so you can move on with your life!

Firstly, I disabled my preferred VPN location from automatically connecting in the Network Manager GUI. Then I created a very small script:

#!/bin/bash

# Sleep for a bit
sleep 5

# Connect to VPN
nmcli connection up [vpn connection name in here]

I’m currently on hyprland, but if you’re on a different Desktop Environment or Window Manager, the principle is the same. Autostart the script when you login.

And with that, all seems to work again. The issue has to be timing - the network service is either not ready or the VPN tries to connect before the ethernet (if that’s possible?) - I’m really not sure. But being a complex system and avoiding what I see as a race condition, a small delay seems to fix things nicely.