1

I've spent hours attempting this via the if-up.d method as described in: Question 258580

The script just won't trigger and I realized that this method is outdated because Ubuntu no longer uses etc/network/interfaces - since v. 17.10 we are using Netplan.

Then I spent a couple more hours researching and trying the 'systemd service' method as described in:Question 1184648 which appears to be the more modern method, but my service fails during execution and I don't know why.

I don't know where to go from here. It would sure be nice if my ProtonVPN could connect automatically whenever I have a network connection. Please advise, sympathize, or anesthetize.

hoatzin
  • 568
  • 2
    if-up.d is deprecated. Look up how to use NetworkManager.dispatcher. See man networkmanager-dispatcher – user535733 Aug 17 '22 at 18:39
  • @user535733 networkmanager-dispatcher looks to have good potential. The manual is great, but it is the only informative reference to the command I can find. I'm not experienced enough to make it work without even one example. – hoatzin Aug 17 '22 at 22:29
  • While looking at networkmanager-dispatcher I came across networkd-dispatcher which is the dispatcher service for systemd-networkd. This appears to be a simpler system, but I see that my system is not currently utilizing systemd-networkd. I will look into this more. – hoatzin Aug 17 '22 at 22:32
  • 2
    Background: All Ubuntu systems use either NetworkManager or networkd. Just one; they don't mix. Be sure to use the appropriate dispatcher for the one you are using. – user535733 Aug 18 '22 at 00:58
  • @user535733 I see. I was wondering about that. I better stick with the NetworkManager options since that is what came with my system. – hoatzin Aug 18 '22 at 11:53
  • If any of the the answers below works for you, feel free to up vote the answer and accept the answer as correct by clicking on the gray check mark next to the answer and turn it green ✅. This will mark your problem as solved and help others. – user68186 Dec 14 '22 at 05:57

2 Answers2

1

This answer is about starting ProtonVPN when connected to network, and not about the general question.

Use the OpenVPN Configuration file

See the ProtonVPN's site for how to manually configure OpenVPN for ProtonVPN.

This guide will walk you through how to create, download and set up the OVPN file from ProtonVPN.

Once you have done everything on that site open the app Advanced Network Configuration. If you can't find this app by searching for apps, then open a terminal and enter:

nm-connection-editor

Select the network you want to use VPN in. For example, Ethernet Connection, and click on the gear icon at the bottom left corner of the window.

enter image description here

Go to the General Tab and then select the check box next to Automatically connect to VPN.

Then use the drop-down menu next to it to select the ProtonVPN set up you have already done.

Repeat for each WiFi network you want to use VPN.

See 20.04LTS NetworkManager VPN - Always on for more.

A Script to run at login

Write a script that checks if there is any internet connection and if ProtonVPN has no active connections. If these conditions are met, the script will start ProtonVPN using the commandline client.

Create a script called /home/your_username/bin/start_proton. You may need to create the folder /home/your_username/bin/ to keep all your scripts. The content of the script is:

#!/bin/bash
# Sccript:/home/username/bin/start_proton 
# Purpose: Check if there is internet and if proton is not running, then start VPN

STATUS=$(protonvpn-cli status | grep No) # Checks VPN is not running ping -c1 -4 google.com > /dev/null # Check Internet is working if [[ $? -eq 0 && $STATUS == "No active Proton VPN connection." ]]; then echo $STATUS protonvpn-cli connect --fastest else echo "FAIL: Either no Internet Or VPN is running already" fi

Note: I have used the --fastest option for the VPN server choice. You may edit that line and use other options that suits you.

Make the script executable by Right-Clicking the script in Files going to its Properties > Permission Tab.

Test the script

make sure you are connected to the Internet and ProtonVPN is not running.

Open a terminal and enter the command:

start_proton

If all goes well you will see ProtonVPN starting up and connecting to its server.

Add to Startup Applications

Now open the Startup Applications app and click on the Add button. Fill up the fields as below:

Name: Start VPN
Command: /home/username/bin/start_proton
Comment: starts VPN if there is any Internet

Replace username with your username for this computer.

Click on the Save button to save the new entry. Make sure the check box next to Start VPN is checked.

Next time when you start your computer and login ProtonVPN will start.

Hope this helps

user68186
  • 33,360
  • Thanks for this information. Manually configuring OpenVPN looks daunting. I wonder why neither the ProtonVPN GUI app nor CLI version allow autoconnect. Seems silly to me. Regardless, your suggestion could resolve my problem so I will look into it. – hoatzin Aug 18 '22 at 11:58
  • @hoatzin If you want ProtonVPN to start when connected to a specific WiFi, say Starbucks, it can be done by a script that will start when you login (via Startup Applications). The script will call the command-line version of the ProtonVPN client. If you want this, ask a new question and leave a link to that question below. – user68186 Aug 18 '22 at 13:12
  • That setup sounds nice, but connecting when I login only covers about 5% of the instances when I need to re-connect to VPN. Most reconnections happen for me when the computer wakes from suspend or when I change network devices, that's why I was really hoping to trigger a script upon network connection. It doesn't sound like that's going to happen. – hoatzin Aug 22 '22 at 11:59
  • You can try the solutions to this question and add protonvpn-cli c -f in the appropriate place. – user68186 Aug 22 '22 at 12:37
  • By the grace of google I found this. The script will need some updates and changes to customize to your username and VPN server specifications. I have not tried it, but it may work. – user68186 Aug 22 '22 at 15:44
1

Answering only this part

It would sure be nice if my ProtonVPN could connect automatically whenever I have a network connection.

You can use the builtin feature of Network Manager to do this.

Edit the connection you are using and tick the box Automatically Connect to VPN.

enter image description here

Organic Marble
  • 23,641
  • 15
  • 70
  • 122
  • I tried this. It worked well until I rebooted or suspended. Upon waking or reboot I would get the error: "A secondary connection of the base connection failed". Then I would have to uncheck "Automatically connect to VPN" before it would connect. It doesn't make any sense to me. – hoatzin Aug 22 '22 at 11:53
  • At this point, this is the best method and it works awesome. – hoatzin Mar 11 '23 at 01:46