How to adjust network settings in OS X via the command line

In addition to using the system preferences, you can adjust Wi-Fi network configuration in OS X via Terminal.

If you need to adjust network settings on your Mac, the quickest route is to do so through the OS X system preferences and menu extras. However, you can also make these changes -- adding and removing ports, creating new locations, switching to DHCP from manual configurations, changing MTU sizes, and managing 802.1x profiles, among others -- via the command line.

The utility for doing this is the command "networksetup," which can be used to quickly apply a change to a specific network property. This tool covers every aspect of the Mac's network setup and you can look at all of its options by simply running networksetup by itself in Terminal to see a list of all supported command options. The list is quite extensive, but when it comes to basic Wi-Fi management, there are a few options that are relatively easy to remember and put to use.

  • Turning Wi-Fi on or off
    With the following options, you can get the power status of the current Wi-Fi card and change it if needed:

    networksetup -getairportpower <device name>
    networksetup -setairportpower <device name> <on off>

  • Setting the AirPort network
    With these options you can change the network AirPort uses (see below for how to scan for Wi-Fi networks via the command line):

    networksetup -getairportnetwork <device name>
    networksetup -setairportnetwork <device name> <network> [password]

  • Managing preferred wireless networks
    These options can be used to list your preferred Wi-Fi networks, add or remove networks to the list, or clear the entire list:

    networksetup -listpreferredwirelessnetworks <device name>
    networksetup -addpreferredwirelessnetworkatindex <device name> <network> <index> <security type> [password]
    networksetup -removepreferredwirelessnetwork <device name> <network>
    networksetup -removeallpreferredwirelessnetworks <device name>

With these options, "<network>" is the name of the Wi-Fi network you are interested in, and the "<device name>" will likely be "en1" (the default for the Wi-Fi card in Macs), but you can look this up by running the following command and noting the device name next to each network port listed:

networksetup -listnetworkserviceorder

Additional options
Though you can exercise the above options through the networksetup utility, the tool does have its limitations, such as the inability to scan for available Wi-Fi networks and adjust the Wi-Fi channel. To fill this gap, Apple offers a command called "airport" that is hidden deep in the system's frameworks folder at the following location:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport

With the airport command you can change channels, disconnect from any Wi-Fi network, and get information on the current connection, but more importantly, you can scan for available Wi-Fi networks. If you don't want to go digging for this buried command every time, you can make an alias for it, create a link to it, or copy it from its location to a more convenient place, such as the /usr/bin/ directory. Here, though, we will change to the command's parent directory and run it from there by using the following:

cd /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/

Wi-Fi configuration via the command line.
Here I used these Terminal commands to list the Wi-Fi networks and change the system to use a specified one (click for larger view). Screenshot by Topher Kessler/CNET

Once at this directory, you can run various options in the command line to look up information and make changes to the Wi-Fi connection:

  1. Change channel

    ./airport -c CHANNEL

  2. Disconnect

    ./airport -z

  3. Get current connection info

    ./airport -I

  4. Scan for Wi-Fi networks

    ./airport -s

You can use both the output from the airport command and the networksetup utility to adjust properties such as connecting to a new Wi-Fi network. First, scan for networks with the "airport -s" option and then use the SSID of the listed networks with the "networksetup -setairportnetwork <device name> <network> [password]" command.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Show Comments Hide Comments