The Need for CLI Network Management
While the macOS graphical System Settings app is intuitive for joining a local coffee shop Wi-Fi, it is painfully slow when you need to rapidly switch DNS servers for web development, configure static IP addresses on a fleet of machines, or forcefully disconnect from a stubborn wireless network.
For systems administrators, developers, and power users, macOS includes a massively powerful command-line utility called networksetup. This single command allows you to configure almost every aspect of your Mac’s networking stack directly from the Terminal, enabling you to script automated network changes or perform rapid troubleshooting.
Understanding Network Interfaces
Before you can change a setting, you must know what macOS calls your network adapters (known as “Network Services”).
Open the Terminal and run:
networksetup -listallnetworkservices
You will see a list of human-readable names like Wi-Fi, Ethernet, and Thunderbolt Bridge. You must use these exact names (including the spaces, which must be wrapped in quotes) in the subsequent commands.
Common networksetup Commands
1. Managing DNS Servers
If your internet is behaving erratically, your ISP’s default DNS servers might be struggling. You can instantly force your Mac to use reliable public DNS servers (like Google’s 8.8.8.8 or Cloudflare’s 1.1.1.1).
To view your current DNS servers for Wi-Fi:
networksetup -getdnsservers Wi-Fi
To set new DNS servers (requires administrator privileges):
sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4
To clear custom DNS and return to your router’s default settings (DHCP):
sudo networksetup -setdnsservers Wi-Fi "Empty"
2. Managing Wi-Fi Connections
You can turn your Wi-Fi antenna on or off, and view available networks without touching the menu bar icon.
To find the hardware port name of your Wi-Fi antenna (usually en0):
networksetup -listallhardwareports
To toggle the Wi-Fi power off and back on (a great troubleshooting trick):
networksetup -setairportpower en0 off
networksetup -setairportpower en0 on
To connect to a specific Wi-Fi network (if you know the SSID and password):
networksetup -setairportnetwork en0 "CoffeeShopWiFi" "Password123!"
3. Configuring a Static IP Address
If you are setting up a Mac mini as a home server or connecting to a corporate intranet that does not use DHCP, you must configure a static IP address, Subnet Mask, and Router (Gateway).
sudo networksetup -setmanual Ethernet 192.168.1.50 255.255.255.0 192.168.1.1
To revert the interface back to automatic IP assignment (DHCP):
sudo networksetup -setdhcp Ethernet
Conclusion
The networksetup command is the absolute standard for macOS network administration. By bypassing the sluggish graphical interface, you can instantly manipulate DNS records, cycle wireless antennas, and configure robust IP routing directly from the terminal prompt.