Toggling macOS VPN from the Terminal

The native macOS VPN client

Every modern macOS version comes with a built-in VPN client, located in System Preferences > Network. It works by creating a new service of type VPN and giving it a name (e.g. MyVPN).

How to connect / disconnect from a specific VPN

The simples way to do this is to use the built-in networksetup util. For example, to connect to the VPN service MyVPN, you would call:

networksetup -connectpppoeservice "MyVPN"

Similarly, to disconnect, you would call:

networksetup -disconnectpppoeservice "MyVPN"

Since these commands are quite long, it is advised to make short aliases for them and add them to your .*rc file:

alias vpnc='networksetup -connectpppoeservice "MyVPN"'
alias vpndc='networksetup -disconnectpppoeservice "MyVPN"'