Connecting to Wi-Fi through CLI on macOS.

Connecting to Wi-Fi through CLI on macOS.

As we all know, macOS is a variant of UNIX that resembles Linux in certain ways. Despite using separate kernels, macOS and Linux share a large number of commands. Working remotely on a couple of machines' wireless networks can be annoying at times. The question is how to switch a dozen Macs Wi-Fi networks through the Command line.

The airport is a utility to set up, monitor, and manage advanced features, such as security modes, wireless channels, and IPv6 configuration as well as managing AirPort base stations.

Running airport command will trigger an error.

$ airport
zsh: command not found: airport

How to Access & Use airport Command Line Tool on Mac OS.

Yes, the command line airport utility does exist; it's just hidden.
With that in mind, here's how to get the really valuable but little-known airport utility, as well as how to utilize it for basic networking activities.

You'll want to make a symbolic link to the airport command in /usr/local/bin using ln -s .

Under MacOS Catalina, Big Sur, Monterey, Ventura, and Sonoma.

$ sudo ln -s 

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport

View available Wi-Fi networks.

Now that you have a quick and easy way to access airport .
Let's start exploring this tool.
The -I flag and -s are one of the most used and informative commands.

-I: prints current wireless status, e.g. signal info, BSSID (Basic Service Set Identifier), port type etc ...
-s: performs a wireless broadcast scan.

Here; is detailed information on wireless signal quality, noise, security, and other Wi-Fi network attributes.

airport -I

     agrCtlRSSI: -4x
     agrExtRSSI: 0
    agrCtlNoise: -x7
    agrExtNoise: 0
          state: running
        op mode: station 
     lastTxRate: 1x0
        maxRate: 1x4
lastAssocStatus: 0
    802.11 auth: open
      link auth: wpa2-psk
          BSSID: sx:cx:dx:7x:xx:xx
           SSID: connectome
            MCS: 15
        channel: 11

The command below will get a list of available wireless networks as well as their SSID (service set identifier), BSSID hardware address, security encryption type, and channel.
And as we can see from the above; we are connected to wifi SSID connectome.

 airport -s
    SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
connectome     x#:sx:cx:dx:7x:xx:xx -71  11,+1    Y  xx WPA2(PSK/AES/AES) 
freewifijoinin x#:d5:5t:e4:z5:h9:20 -28           Y  xx WPA2(PSK/AES/AES) 

Find out the device name of our Wi-Fi.

The networksetup command is used to configure any given client’s network settings.
We can use -listallhardwareports flags as well as
-listnetworkserviceorder to gather information on our network interface.

$ networksetup -listallhardwareports                

Hardware Port: Wi-Fi
Device: en0
Ethernet Address: xx:xx:xx:xx:xx:xx

Hardware Port: Bluetooth PAN
Device: en2
Ethernet Address: xx:xx:xx:xx:xx:xx

Hardware Port: Thunderbolt 1
Device: en1
Ethernet Address: xx:xx:xx:xx:xx:xx

Hardware Port: Thunderbolt Bridge
Device: bridge0
Ethernet Address: xx:xx:xx:xx:xx:xx

VLAN Configurations


$ networksetup -listnetworkserviceorder                

An asterisk (*) denotes that a network service is disabled.
(1) iPad USB
(Hardware Port: iPad USB, Device: en5)

(2) xxxx
(Hardware Port: Wi-Fi, Device: en0)

(3) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en2)

As you can see it displays a list of network services, along with the corresponding port and device for each. An asterisk (*) next to a service means the service is inactive.

Here; our Wi-Fi device is en0 .
We can now connect ourselves to any Wi-Fi network we have access to.

Connecting to Wi-Fi with password.

Based on all the above information, we'll try to connect and authenticate ourselves to the Wi-Fi network with SSID connectome.

Let's run.

networksetup -setairportnetwork en0 freewifijoinin wifi_password

We are now connected to SSID freewifijoinin.

Turn off WI-FI on/off.

Turn OFF your WI-FI network.

$ networksetup -setairportpower en0 off

Turn ON your WI-FI network.

$ networksetup -setairportpower en0 on