Is there a way to randomize my selected OpenVPN-servers?

I have a ProtonVPN account and connect to VPN servers at all times for internet privacy. Unfortunately, ProtonVPN does not offer a Linux-based client at this point, so they advise to connect to their servers via OpenVPN.

I installed the packages openvpn network-manager-openvpn and network-manager-openvpn-gnome to the official guide to load their .ovpn configuration files.

The connection works fine, but whenever I connect to a VPN on Ubuntu, I have to manually select a specific server from the preconfigured .ovpn files. Instead, on their Android client, for example, I can just tap a button and connect to a random server without even thinking about it.

Is there any way to make the network manager select a random .ovpn config, instead of me having to select one manually? (I'm not afraid of the command line, but preferrably without adding another PPA to my system.)


A simple command line script could work like this:

#!/bin/bash
RANGE=200

number=$RANDOM
let "number %= $RANGE"
fname="us${number}.ovpn"
openvpn "$fname"

Assuming that:

  • Your OpenVPN config files are named following a scheme like us176.ovpn, i.e. a two-letter country code and a running number.
  • The available OpenVPN config files run from us1 to us200.

You would have to adapt the range, and add the directory where the config files are stored. Depending on the exact file name format, which I don't know for ProtonVPN, you may have to make other modifications, but you get the idea. Note that you would need to execute the script as sudo for OpenVPN to work like that.

EDIT: In case your VPN requests a username and password on connection, you can modify the last line of the script like this:

openvpn --config $fname --auth-user-pass /dir/to/userpass.txt

where userpass.txt contains the username and password on two separate lines, if you are comfortable with storing it like that.


I never did that but as far as i understand the openvpn manual you can write multiple connection profiles / remote vpn-server-ip's in your .ovpn config file. When combined with –remote-random the software openvpn will choose one for you, otherwise it will select the first server that is specified in the list. I do not know if this will work with NetworkManager but it should work when you run the command from termial like
sudo openvpn my.config.file.ovpn.


Quote form the maual:

--remote host [port] [proto]

Remote host name or IP address. On the client, multiple --remote options may be specified for redundancy, each referring to a different OpenVPN server. Specifying multiple --remote options for this purpose is a special case of the more general connection-profile feature.

See the <connection> documentation below.

The OpenVPN client will try to connect to a server at host:port in the order specified by the list of --remote options. proto indicates the protocol to use when connecting with the remote, and may be "tcp" or "udp".

The client will move on to the next host in the list, in the event of connection failure. Note that at any given time, the OpenVPN client will at most be connected to one server.

[...]

<connection> Define a client connection profile.

Client connection profiles are groups of OpenVPN options that describe how to connect to a given Open‐VPN server. Client connection profiles are specified within an OpenVPN configuration file, and each profile is bracketed by <connection> and </connection>.

Here is an example of connection profile usage:

client dev tun

<connection>

remote 198.19.34.56 1194 udp

remote 198.19.34.56 443 tcp

remote 198.19.34.56 443 tcp http-proxy 192.168.0.8 8080 http-proxy-retry

[...]

An OpenVPN client will try each connection profile sequentially until it achieves a successful connection.

--remote-random can be used to initially "scramble" the connection list.