Enable and Disable AirPort Wireless from the Command Line in Mac OS X
Sometimes the easiest fix when troubleshooting an AirPort wireless connection problem is to just turn AirPort on and off. Instead of using the menu item or System Preferences, we can enable and disable AirPort very quickly directly from the Mac OS X Terminal.
To do this, we’re going to use the ‘networksetup’ command. Note that this uses the “AirPort” reference, even with newer versions of Mac OS X where Wi-Fi is no longer called AirPort, so ignore that naming convention change from Apple and just know that both pertain to the Macs wireless networking abilities.
Turn Wi-Fi Off via Command Line in Mac OS X
The network device name will determine how the proper syntax is entered.
networksetup -setairportpower airport off
The device name could be airport, en0, en1, etc, depending on the Mac hardware and the version of OS X. Thus, you may need to specify the device port rather than ‘airport’, for example en1 or en0:
networksetup -setairportpower en0 off
You can use the -getairportpower flag to check the port if you are unsure.
Turn Wi-Fi (Airport) On via Command Line in Mac OS X
Just like turning wi-fi off from the command line, you can also toggle it back on again. As before, pay attention to the device name:
networksetup -setairportpower airport on
And again, you may need to specify device en0 or en1 instead of ‘airport’, like so:
networksetup -setairportpower en0 on
You won’t see any confirmation in the Terminal that the command succeeded or failed, but if you watch the AirPort menu icon you will see the bars disappear indicating the wireless interface is turned off, or reappear indicating that wireless is activated again.
We can also string the commands one after the other to power cycle the wireless interface on a Mac:
Quickly Power Cycle Wi-Fi with Mac OS X’s networksetup Tool
networksetup -setairportpower airport off; networksetup -setairportpower airport on
The AirPort wireless card seems to respond faster to the command line networksetup tool than any other method, making this an ultrafast method of power cycling the wireless interface. This is often enough to resolves basic wireless router connectivity issues like IP conflicts or malfunctioning DHCP requests.
I have enough regular encounters with one particularly flakey router that I created an alias to power cycle my AirPort card, you can do this by adding the following to your .bash_profile just be sure it is on one line:
alias airportcycle='networksetup -setairportpower airport off; networksetup -setairportpower airport on'
Now like any other alias, you only type ‘airportcycle’ and the wireless interface will immediately turn itself off and on again.
Disabling and reenabling AirPort is not the same as connecting to a wireless network from the command line, although you can do that too also by using the networksetup tool.