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:
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:
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:
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:
- Change channel
./airport -c CHANNEL
- Get current connection info
- Scan for Wi-Fi networks
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.