networksetup: Change Wi-Fi and DNS From Terminal
Everything in the Network pane of System Settings has a command-line twin: networksetup. It can join a Wi-Fi network, change DNS servers, and switch interfaces on and off — which means it can also do those things inside a script, over SSH, or across ten Macs at once. Here are the subcommands that cover real work.
First: learn your service and device names
networksetup addresses networks two ways — by service name ("Wi-Fi", "Thunderbolt Ethernet") or by device name (en0, en5). Get both lists before anything else:
# service names, in the order macOS tries them
networksetup -listallnetworkservices
# services mapped to device names like en0
networksetup -listallhardwareports
# current address, router, and DNS for one service
networksetup -getinfo "Wi-Fi"
On most Mac laptops Wi-Fi is en0, but never assume — docks and adapters shuffle device names, and the hardware ports list is the truth.
Set and clear DNS servers
The most common real-world use: pointing a service at specific DNS resolvers.
# use Cloudflare DNS for Wi-Fi
sudo networksetup -setdnsservers Wi-Fi 1.1.1.1 1.0.0.1
# check what's set
networksetup -getdnsservers Wi-Fi
# undo: clear manual DNS, back to the router's default
sudo networksetup -setdnsservers Wi-Fi empty
The literal word empty is the documented way to remove manual servers — after it, -getdnsservers reports there aren't any DNS servers set, meaning DHCP supplies them again. Changes apply immediately, no restart needed. The same pattern works for search domains with -setsearchdomains.
Join Wi-Fi and toggle the radio
# turn Wi-Fi off, then on (en0 = the Wi-Fi device here)
networksetup -setairportpower en0 off
networksetup -setairportpower en0 on
# join a network by name
networksetup -setairportnetwork en0 "HomeNetwork" "wifi-password"
The power toggle is the classic "have you tried turning Wi-Fi off and on" — scriptable, and handy over SSH when the menu bar is out of reach. -setairportnetwork joins immediately and remembers the network like the GUI would. One caution: the password is typed in plain text and lands in your shell history. For a sensitive network, join once from the Wi-Fi menu instead, and save the command form for automation where the credentials are disposable.
Getting and setting more
networksetup -getcomputersleep/-getcurrentlocation— read misc settings; most-get…subcommands have a matching-set….sudo networksetup -setnetworkserviceenabled "Wi-Fi" off— disable a whole service (undo withon).networksetup -listpreferredwirelessnetworks en0— every remembered network, in priority order.
Setter subcommands need administrator rights — run them with sudo or macOS will prompt for authorization. Getters run as any user, so exploration is safe.
Network locations: profiles for all of it
If you regularly change DNS or service settings when moving between networks, stop re-typing and use locations — named snapshots of every network setting:
# see the locations that exist, and which is active
networksetup -listlocations
networksetup -getcurrentlocation
# create one, then switch between them
sudo networksetup -createlocation "Office" populate
sudo networksetup -switchtolocation "Office"
# undo: switch back to the default location
sudo networksetup -switchtolocation "Automatic"
populate copies the default services into the new location so you're editing a working setup rather than starting empty. Every DNS and interface change you make while a location is active belongs to that location — switch back to Automatic and the office DNS servers stay behind, waiting for next time. Delete an experiment with sudo networksetup -deletelocation "Office" once you've switched away from it — locations are cheap to make and cheap to discard.
For the hundred other macOS settings that don't need a script, Mainspring provides labelled, reversible toggles — 90+ hidden tweaks, each one undoable with a click.
Try Mainspring free →Signed & notarized by Apple · 1-day free trial · $29 once
Verify the result
After a DNS change, confirm your Mac is actually using the new resolvers and clear any cached lookups — our guide to flushing the DNS cache covers the follow-through.