43

Im using Ubuntu 13.04 with Gnome and I recently set up a (open)VPN. Is there any way to enable it by default? Each time I boot or even lost my connection, I have to enable the VPN manually. Is there an option I am missing?

8 Answers 8

72

Through the Network Manager indicator nm-applet (the GNOME or Unity network tray applet installed by default), you can configure NetworkManager to automatically connect to a VPN when a network is connected.

  1. Click on the network tray applet and click "Edit connections...", or run nm-connection-editor.
  2. Select a network connection and click "Edit..."
  3. Check "Automatically connect to VPN when using this connection" and select the desired VPN in the drop-down list.
  4. Save.

nm-connection-editor screenshot


When this is enabled, there is a bug in NetworkManager that can break the "automatically connect to this network" function. (Edit: this bug has now been marked as "fix released" in Ubuntu 16.04). If NetworkManager tries to automatically connect and fails, you will see a line like the following in /var/log/syslog:

<error> [1401130450.367538] [nm-vpn-connection.c:1374] get_secrets_cb(): Failed to request VPN secrets #2: (6) No agents were available for this request.

It seems that NetworkManager fails to obtain the user's VPN password from gnome-keyring-daemon. One workaround is to let NetworkManager store the password in plaintext in the configuration file in /etc/NetworkManager/system-connections/. To do this:

  1. Open a terminal.
  2. sudoedit /etc/NetworkManager/system-connections/<VPN>, where <VPN> is the configuration file for your VPN (the filename is usually the name you assigned to your VPN).
  3. Change the line password-flags=1 to password-flags=0
  4. Save and exit.

NetworkManager will now store the VPN password itself (see man nm-settings for details), and the network autoconnect will work once again.

4
  • 5
    That doesnt exist in gnome network manager. Aug 16, 2013 at 9:05
  • 4
    When opening nm-connection-editor you can find this when editing a connection in the general tab.
    – Lode
    Oct 25, 2014 at 12:36
  • 1
    This causes the connection itself not to autoconnect for me. Is there any solution to this? Jul 14, 2015 at 8:23
  • @Barta That is a bug in NetworkManager. I've added a workaround to my answer.
    – Vincent Yu
    Jul 15, 2015 at 2:21
8
+50

Take a look at vpnautoconnect.

vpnautoconnect is a daemon that allow you to reconnect automatically (at startup too) a vpn create with network manager.It can reconnect very quickly and monitor the bandwith, It works with pptp and openvpn connection.

For more info, and to download visit the Website.

Try This as well:

use the AUTOSTART feature in /etc/default/openvpn

Or

Figure out the UUID of your VPN connection.

nmcli con list | grep -i vpn

The UUID is the second column with the letters, numbers and dashes.

Start the connection in a terminal. Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command(s) below:

nmcli con up uuid <put you UUID here>

Set this to run at start-up.

Go to Dash, type and choose Startup Applications, click Add, and add the nmcli command above (with the UUID). Click "Add". In the name type what ever name you want to use, and in Command put the whole nmcli line above. Click "Add" again. Now, reboot and try it.

Source:SourceForge

6
  • I installed and configured it, rebooted my computer but it doesn't work. Aug 7, 2013 at 17:02
  • I've added some more info in my answer.
    – Mitch
    Aug 15, 2013 at 16:53
  • Is there any documentation available for vpnautoconnect? Their website is an abandoned SourceForge project page, and the package does not provide man pages. Tough it sounds promising. Jul 14, 2015 at 8:22
  • I can't seem to find any, but I'll keep looking.
    – Mitch
    Jul 14, 2015 at 8:56
  • Action list has been renamed to show in 15.04 or 15.10
    – Croll
    Feb 5, 2016 at 21:49
3

vpnautoconnect did not work for me in 12.04 LTS, and I dont seem to be the only one.

I researched it a bit and combined some existing code to make my first bash script. It checks if a given VPN connection is active, and will connect if not. If it is connected it will sleep for a given time, e.g. 1 minute, and repeat the process indefinitely.

#! /bin/bash

while true
do
   connection="Auto Ethernet"
   vpn_connection="My VPN connection"
   run_interval="60"

   active_connection=$(nmcli dev status | grep "${connection}")
   active_vpn=$(nmcli dev status | grep "${vpn_connection}")

   if [ "${active_connection}" -a ! "${active_vpn}" ];
   then
      nmcli con up id "${vpn_connection}"
   fi

   sleep $run_interval
done

Instructions:

  1. Create a blank text file named e.g. vpn-auto-connector.sh (I saved it in my home folder. Right click file and choose Properties->Permissions and check "Allow executing file as program". (You may have to store the file somewhere else and/or alter the read/write/execture permissions as well if your computer has multiple users.)

  2. Copy the code from above to the file you created. Replace the values of the following three variables:

    connection="Auto Ethernet"

    vpn_connection="My VPN connection"

    run_interval="60"

    They can be found by opening the network manager. In my case connection="Auto Ethernet" is my active wired connection (haven't tested with wireless) and vpn_connection="My VPN connection" is the name of my VPN connection. run_interval="60" is the time interval in seconds for when to repeat the script.

  3. Open Applications->System Tools->Preferences->Startup Applications. Add a suitable name, e.g. "VPN Auto Connector", and for the command choose the .sh file you saved earlier. Now the bash script will run at startup and will keep checking if the VPN connection is active. You can try it by disconnecting the VPN connection and it should be activated automatically again.

1
  • Great thanks. This script is easy to understand.
    – Evan Hu
    May 8, 2016 at 14:05
2

I would recommend checking out the script in this article:

#!/bin/bash

# YourVPN here is the name of desired vpn connection to monitor
# edit this line:
##################
VPNNAME=YourVPNUUID
# enter desired time between checks here (in seconds)
SLEEPTIME=15
##################

nice=0

for (( ; ; )); do

# creating infinite loop

tested=$(nmcli con status uuid $VPNNAME | grep -c UUID)
#possible results:
# 0 - no connection - need to start
# 1 - working connection, continue.

case $tested in
"0")
echo "Not connected - starting"

#increase nice counter
nice=$[nice+1]

#if "nice start" fails for 3 times
if [ $nice -ge 3 ];
then
#TRY to knock hard way, resetting the network-manager (sometimes it happens in my kubuntu 12.04).
      echo "HARD RESTART!"
      nmcli nm enable false
      nmcli nm enable true
      sleep 5
      nmcli con up uuid $VPNNAME
      nice=0
else
#not yet 3 falures - try starting normal way
      echo "trying to enable."
      nmcli con up uuid $VPNNAME
fi

;;

"1")
echo "VPN seems to work" 

;;
esac

sleep $SLEEPTIME

done

To find out the value of YourVPNUUID for $VPNNAME simply run the following;

nmcli con list | grep -i vpn
0

Vpnautoconnect works fine in Ubuntu 12.04, 13.04 and 13.10 (I think that in the 13.10 the "connect automatically" option in the Network-Manager has been fixed and works now)

So, if you have troubles to install it or get it work, or don't find the "openvpn" option in the drop down menu of Network-Manager to create an open vpn connection, you can follow this tuto, that gives you All the steps, very clear and easy to apply.

Have a look HERE

And let me know :-)

0

Here is a very robust and reliable script that will:

  • Check if you are connected to any VPN
  • If not, connect to the most recently active VPN

This is especially useful if you routinely connect to different VPNs since you do not have to specify a single VPN connection to reconnect to.

#!/bin/bash

nmcli --mode multiline --fields uuid,vpn connection status | grep -qc "^VPN:.*yes" 1>/dev/null 2>&1

if [ $? -ne 0 ]; then
        last_vpn_uuid=`nmcli --mode tabular --fields uuid,type,timestamp connection list | grep -E "\s+vpn\s+" | sort -k3 -nr | head -n1 | awk '{ print $1 }'`

        nmcli connection up uuid "$last_vpn_uuid"
fi
1
  • This is really interesting, but it throws several errors on: $ nmcli -version nmcli tool, version 0.9.10.0 $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 15.04 Release: 15.04 Codename: vivid
    – gare
    Sep 14, 2015 at 1:09
0

I am on Ubuntu 14.04 LTS on several machines. Due to the fact, that I have some special prerequisites:

  1. I do not want to save the password in the password manager
  2. I am using a VirtualBox guest which loses VPN on network reconnect
  3. Password dialogue opens multiple times on reconnect
  4. I am using a backup VPN server (failover)
  5. Using a configuration file for my uuids

I am working with this very proven and very reliable script:

There are - however - some caveats with network manager applet which I could temporarily solve (see Troubleshooting section of the article).

vpnautoconnect does not work for me.

0

answer by @vincentYo looks straight forward , but i couldn't get it done ,so i have done as below.

step1 : create a shell script and add vpn command

vim auto_vpn.sh

#!/bin/sh vpnc --enable-1des

step2 : open a sudoer file and tell kernel not to ask password for this file .

sudo vim /etc/sudoers

most of the things inside this file will be commented , just add like below

username ALL=(root) NOPASSWD:your_shell_script.sh

example:

sharath ALL=(root) NOPASSWD:/home/sharath/workspace/work/src/auto_vpn.sh

step3 : set up a cron job ,which will trigger this shell script , below cron job will run every minute(bcz my internet keeps on and off, so every minute)

* * * * * sudo /home/sharath/workspace/work/src/auto_vpn.sh

i am using this from many days ,working fine in ubuntu 16.04 LTS ..Chears!!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .