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 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 OS X
networksetup -setairportpower airport off
Note that you may need to specify the device port rather than ‘airport’, for example en1 or en0:
networksetup -setairportpower en0 off
Turn Wi-Fi (Airport) On via Command Line in Mac OS X
networksetup -setairportpower airport on
Again, you may need to specify device 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.