Auto Connect to VPN on Boot & Login in Mac OS X
If you use a VPN with a Mac for either work or personal reasons, you may wish to have the Mac automatically connect to the VPN service when the Mac is booted or upon login. Or maybe you just want the VPN to automatically reconnect if the connection drops and disconnects. This is easily accomplished with the help of a simple AppleScript, which will both automatically connect to the VPN upon system startup and login, and also check to see if the VPN is active, and reconnect if necessary.
Obviously you’ll need an active VPN service and VPN location setup in Mac OS for this script to work, otherwise the script will have nothing to connect to upon login and system boot. If you do not have or use a VPN (Virtual Private Network), then this trick will be of little use to a particular Mac.
How to Connect to VPN on Boot or Login in Mac OS X, Automatically
This auto-connect VPN script should work on any version of macOS or Mac OS X system software. Essentially what we’re doing is placing a connection script into Login Items so that it loads automatically on system start and user login events:
- Open “Script Editor” on the Mac, it is found within the /Applications/Utilities/ folder
- Go to the File menu and choose “New”
- Copy and paste the following AppleScript syntax into the new blank script editor:
- Replace “VPN NAME” with the name of the accompanying VPN network location as found in System Preferences Network control panel (you can also change the return number to check the network more or less often, in seconds)
- Go to the “File” menu again and choose “Save”
- Under the ‘File Format’ pulldown menu, choose “Application”
- Check the box next to “Stay open”
- Now choose “Save” and give the VPN script and obvious name (like ‘AutoVPN’) and put it somewhere easy to locate, like the Desktop or users home directory
- Next go to the Apple menu and select “System Preferences” and go to the “Users & Groups” control panel
- Choose the active user name, then select “Login Items”
- Drag and drop the ‘AutoVPN’ AppleScript application you created into the Login Items section so that it loads automatically on login and system start
tell application "System Events"
tell current location of network preferences
set myVPN to the service "VPN NAME"
if myVPN is not null then
if current configuration of myVPN is not connected then
Now anytime you reboot the Mac or login after being logged out, the VPN service will connect automatically. Likewise, if the service is disconnected for some reason or another, it will attempt to reconnect to the VPN automatically.
Variations of this handy AppleScript have been around for ages and for a variety of purposes, and while here it demonstrates how to automatically connect to a VPN on login and reconnect to a VPN if it loses connection, it can be adjusted to perform other actions or automatically connect and reconnect to other services, including wi-fi or ethernet networks.
Know of another useful trick to automatically connect to a VPN network on a Mac? Or have a better solution or script? Let us know in the comments.