macOS provides a command line interface called,
networksetup which allows you to make all sorts of network configurations.
One of the configurations I’ve found helpful is switching Wi-Fi networks from the command line in Terminal.
To switch Wi-Fi networks using networksetup, you’ll use the option,
-setairportnetwork. But before you can do that, you need to first find your Wi-Fi’s network interface.
Finding your Wi-Fi’s network interface
The quickest way to find your Wi-Fi’s network interface is to use another option for networksetup called
into Terminal should yield something like this,
Hardware Port: Wi-Fi Device: en0 Hardware Port: Bluetooth PAN Device: en4 Hardware Port: Thunderbolt 1 Device: en1
From this list, you might’ve already guessed, the interface for the Wi-Fi network is
Your results may vary on your machine. Just look for the title,
Hardware Port: Wi-Fi and that should be your Wi-Fi’s network interface.
Switching Wi-Fi network from the Terminal with networksetup
Now that you have your Wi-Fi’s network interface, you’ll plug it into the
-setairportnetwork option like this,
networksetup -setairportnetwork en0 "Pizza Time" "PizzaN0mN0m!"
As you can see, the
-setairportnetwork option accepts three values. The first two are required and the third is optional.
The first value is the network interface, which in the example above, I’ve set it to
The next value where I’ve got, “Pizza Time” is the name of the network.
The last—and optional—value is the password for the network you’re trying to connect to.
Upon executing this command in Terminal, you should see the Wi-Fi icon in the top right hand corner of macOS flicker on and off as it attempts to switch networks.
If successful, you should see that you’re connect to the network you’ve passed to
Validate current connected Wi-Fi network
You could also validate the current connected Wi-Fi network using
networksetup by using the
To do this, you’ll want to use the option like this,
networksetup -getairportnetwork en0
Again, you’ll want to designate the network interface you’re checking for, which in my case is
This should yield results like this,
Current Wi-Fi Network: Pizza Time
Practical application of connecting to a Wi-Fi network from Terminal
You might be wondering, “What is the practical application of trying to connect or change a Wi-Fi network from the Terminal?”.
And that’s a super valid question, that I honestly didn’t think I needed to know how to do.
But the reason why I had to figure it out is because I have a couple of wireless networks at my house. Depending on where I’m sitting at the house, the strength of the signal varies.
Working remotely, this means my Zoom call experiences could also vary based on which network I’m on.
I’m usually at my desk when I take Zoom calls, which means I know which network I always want to be connected to for the best call experience.
To do this, I use Keyboard Maestro to run a macro every single time Zoom gains focus.
The macro first checks to see if I’m on my preferred network.
If it isn’t Keyboard Maestro will run the
networksetup command to switch networks. This is possible because one of Keyboard Maestro’s actions is to be able to run shell scripts which means it can run command line commands from within Keyboard Maestro.