How to Join a Network from Command Line in Mac OS X
The networksetup utility allows you to join any available network, whether or not it is a router connected through Ethernet, a wi-fi router that is or is not broadcasting an SSID, and whether or not it has any password encryption required.
Since most networking is done with wireless communications these days, we’ll focus on joining to wi-fi through the command line of OS X with networksetup utility.
At it’s most simple form, to connect to a non-protected network like a public wireless hotspot, just point at the SSID, and specify the proper networking interface to use like so:
networksetup -setairportnetwork en0 SSID
Yes, the syntax is to use -setairportnetwork even though OS X no longer refers to wi-fi as “AirPort”, that is just a hangover from prior versions. That may change in future versions of Mac OS but thus far it remains the same.
You may need to prefix the command with sudo to join to a different wireless connection, depending on the active user privileges.
To connect to any wireless network from the command line that has a password set, use the networksetup command as follows:
sudo networksetup -setairportnetwork en1 SSID PASSWORD
So in a practical example, let’s say we are connecting to a network named ‘Wireless’ and the password is set to ‘macsrule’ as so:
sudo networksetup -setairportnetwork en1 Wireless macsrule
This feature has been around in Mac OS X from Snow Leopard all the way through OS X Yosemite, but it may work on older versions of Mac OS X as well. You can read more about the networksetup command line tool by retrieving it’s manual page by executing the following command:
Or read through any of our many helpful tips using networksetup in OS X to perform a variety of network related functions on the Mac via the command line.