Friday, November 16, 2012

Automatic connection to VPN server in Ubuntu Precise 12.04

Ubuntu Precise 12.04's NetworkManager service can be used to access VPN connections, and can be configured to connect at boot-time without having to add any additional packages to the system. Using the following script, I access my FreeBSD OpenVPN server (configuration was covered in a previous post) at boot time.

Save the following script to /etc/NetworkManager/dispatcher.d/02vpn :
#! /bin/bash

REQUIRED_CONNECTION_NAME="virt0"
VPN_CONNECTION_NAME="baitis vpn"


activ_con=$(nmcli con status | grep "${REQUIRED_CONNECTION_NAME}")
activ_vpn=$(nmcli con status | grep "${VPN_CONNECTION_NAME}")
if [ "${activ_con}" -a ! "${activ_vpn}" ];
then
    nmcli con up id "${VPN_CONNECTION_NAME}"
fi
Don't forget to mark the script as executable with chmod
chmod +x /etc/NetworkManager/dispatcher.d/02vpn

VPN_CONNECTION_NAME should correspond to whatever VPN connection you have configured using Network Manager. If, for some reason, you can't configure VPN connections in Network Manager, you may need to install the appropriate plug-in:
apt-get install network-manager-openvpn network-manager-openvpn-gnome

No comments: