Score:1

Connect to VPN server when system starts

us flag

I can connect to my VPN server with command:

 sudo openvpn confyg.ovpn

Everything runs fine, but what is the best way to start openvpn client and connect to server when system starts?

dMatija avatar
cn flag
What VPN provider?
us flag
Provider is https://openvpn.net . Connecting to different providers makes different connect commands?
ar flag
Which version of Ubuntu/Linux are you using? Are you using a desktop or a server? Do you want a GUI solution?
heynnema avatar
ru flag
@vico Just curious... why did you accept the more complicated systemd answer, over my simpler GUI answer?
Score:2
ru flag

OK, here we go...

  • open the Network Settings panel
  • click on the + icon that's just to the right of VPN

enter image description here

  • select import from file
  • locate your confyg.ovpn file

enter image description here

  • in terminal type nm-connection-editor
  • select Wired Connection
  • click on the edit/gear icon

enter image description here

  • select the General tab
  • enable Automatically connect to VPN
  • select the previously imported configuration file in the popup
  • save the connection
  • reboot

enter image description here

Score:1
ar flag

Use systemd

In order to configure OpenVPN to autostart using systemd, complete the following steps:

Run the command:

sudo nano /etc/default/openvpn

and uncomment, or remove, the “#” in front of

AUTOSTART="all"

then press Ctrl+O followed by Enter to save the changes and Ctrl+X to exit the text editor.

Copy the .ovpn file with the desired server location to the ‘/etc/openvpn’ folder:

sudo cp /location/whereYouDownloadedConfigfilesTo/confyg.ovpn /etc/openvpn/    

Edit the .ovpn file you copied in the previous step and change the line ‘auth-user-pass’ to ‘auth-user-pass pass’:

sudo nano /etc/openvpn/confyg.ovpn

then press Ctrl+O followed by Enter to save the changes and Ctrl+X to exit the text editor.

In the ‘/etc/openvpn’ folder, create a text file called pass:

sudo nano /etc/openvpn/pass

and enter your VPN Account ID on the first line and VPN passwordon the 2nd line, then press Ctrl+O followed by Enter to save the changes and Ctrl+X to exit the text editor.

Change the permissions on the pass file to protect the credentials:

sudo chmod 400 /etc/openvpn/pass

Rename the .ovpn file to ‘client.conf’:

sudo mv /etc/openvpn/confyg.ovpn /etc/openvpn/client.conf

Enable the Openvpn@ service with the following command:

sudo systemctl enable [email protected]

Reload the daemons:

sudo systemctl daemon-reload

Start the OpenVPN service:

sudo service openvpn@client start

Reboot and test if it is working by checking the external IP.

Adapted from: https://www.ivpn.net/knowledgebase/linux/linux-autostart-openvpn-in-systemd-ubuntu/

Hope this helps

heynnema avatar
ru flag
My GUI method seems a lot simpler :-)
ar flag
@heynnema There is no doubt about the GUI method is simpler. I have used it in the past. Your answer is very well written. I like GuI answers that include screenshots. They are easier to follow. I have another solution using `crontab` that is a bit simpler than the systemd based solution.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.