How to Connect to a VPN in macOS from the Command Line
macOS includes several command line utilities such as scutil and networksetup that have the ability to initiate a connection to a pre-configured VPN. However, for some odd reason, these utilities will not work with IKEv2/IPSEC VPN configurations. This has lead to some creative users developing ways of using AppleScript to accomplish this that rely on enabling the VPN status bar icon. However I have personally found the built in VPN status bar to be flakey. Thus, I began to search for a better solution — and I found one! This GitHub repository contains three things: a command line utility (vpnutil), a status bar application (VPNStatus), and a regular application (VPNApp), each allowing you to connect to any VPN you have configured in your current network profile. Let’s take a look at each of them.
vpnutil
Assuming you’ve downloaded the vpnutil.zip into your Downloads directory, we can change to the appropriate directory and unzip the file with the following command:
cd ~/Downloads; unzip vpnutil.zip
Now we can test the executable with:
./vpnutil
Which should yield the following:
Great. Now by executing the following command, we can see the currently configured VPN’s and their connection status:
./vpnutil list
In my example, I had one line of output, shown below, which is the VPN configuration for SurfShark — my VPN provider of choice:
Now to connect, we can simply do the following (be sure to replace NAME_OF_YOUR_VPN with the name of your VPN):
./vpnutil start 'NAME_OF_YOUR_VPN'
Which should yield something like this:
If we want, we can move the vpnutil
executable into a place that is in our $PATH
by executing the following (you will be prompted to enter your password. Make sure to type it correctly, as the characters will not show on the screen, then press enter):
sudo mv vpnutil /usr/local/bin/
Now we can do the following in any directory (notice the lack of ./):
vpnutil
This can be used in scripts as well, as one would expect.
VPNStatus
I have found this to be useful as well, as it has been more consistent for me than the built in status bar icon that can be enabled in preferences. It works similarly to the built in status bar, allowing you to connect and disconnect to/from any VPN you have configured. The app icon lights up green when the VPN is connected (as shown below):
VPNApp
Finally, included also is VPNApp, which serves the exact same function as VPNStatus, only instead of being in the menu bar it is a full application.
Conclusion
Big thanks to Alexandre Colucci for writing these useful utilities. You can check out his blog here.