networksetup

On this page 7

networksetup is the built-in macOS command-line utility that reads and changes persistent network configuration: network services, IP addressing, DNS servers, proxies, Wi-Fi power and network locations. In the Terminal & scripting toolkit of macOS, it is the scriptable counterpart of the Network pane in System Settings, so anything an administrator would click there can usually be expressed as one repeatable command.

The tool lives at /usr/sbin/networksetup and writes through Apple’s SystemConfiguration framework, which means its changes survive reboots and appear immediately in the graphical settings. That persistence is the key difference from low-level tools such as ifconfig, whose changes are temporary and lost at restart.

How networksetup manages network services

networksetup works on network services, not directly on hardware interfaces. A service is a named configuration such as “Wi-Fi”, “Ethernet” or “Thunderbolt Bridge” that binds a hardware port (for example the device en0) to settings like DHCP, DNS and proxies. Most commands therefore take the service name as their first argument, quoted when it contains spaces.

Changes are stored in the system preferences database at /Library/Preferences/SystemConfiguration/preferences.plist, and the configd daemon applies them to the live interfaces. Because the tool edits system-wide settings, almost every write operation needs administrator rights, so scripts typically run it with sudo or from a management agent that already runs as root.

The distinction between service names and device names explains the most common confusion. The following table maps the identifiers that networksetup commands expect.

IdentifierExampleWhere it is used
Network service“Wi-Fi”, “Ethernet”DNS, DHCP, proxy and IP commands
Hardware portWi-Fi, Thunderbolt Ethernet Slot 0Output of -listallhardwareports
Device nameen0, en1, en7Wi-Fi power and joining commands
Network locationAutomatic, OfficeLocation creation and switching
Identifiers used by networksetup

Essential networksetup commands

The command set is large, but a small group of verbs covers most administrative work. Commands start with a single hyphen, and running networksetup -help or networksetup -printcommands lists the full catalog on the installed macOS version.

CommandPurposeNeeds admin rights
-listallnetworkservicesLists services; an asterisk marks disabled onesNo
-listallhardwareportsMaps ports to device names and MAC addressesNo
-getinfo "Wi-Fi"Shows IP address, subnet mask and routerNo
-setdnsservers "Wi-Fi" 1.1.1.1Sets DNS servers; “Empty” clears themYes
-setmanual / -setdhcpSwitches between static and DHCP addressingYes
-setwebproxy, -setautoproxyurlConfigures HTTP proxies or a PAC fileYes
-setairportpower en0 offTurns the Wi-Fi radio on or offUsually
-ordernetworkservicesSets service priority orderYes
Frequently used networksetup commands

Read commands return plain, line-oriented text that is easy to parse with awk or grep in zsh scripts. Write commands normally print nothing on success, so scripts should verify the result with a matching get command rather than assume the change was applied.

macOS ships several network utilities with overlapping but distinct roles. Choosing the right one depends on whether a change must persist and on how deep into the configuration store a task reaches.

ToolMain rolePersistence
networksetupService-level settings matching System SettingsPersistent
scutilDirect access to the dynamic configuration storeMostly runtime
ifconfigLow-level interface parametersLost at restart
ipconfigDHCP state and interface addressesRuntime queries
wdutilWi-Fi diagnostics, replacing the old airport toolDiagnostic only
Command-line network tools in macOS

A practical rule follows from this comparison: use networksetup for configuration, scutil --dns and ipconfig for inspection of the live state, and ifconfig only for short experiments. For fleets, configuration profiles delivered by MDM remain the preferred way to enforce settings, and networksetup fills the gaps profiles do not cover.

Using networksetup in scripts and automation

networksetup is most valuable inside shell scripts, launch daemons and MDM policies, where it turns manual steps into repeatable actions. Reliable scripts follow a few patterns that prevent surprises on machines with unusual hardware.

  • Discover service names dynamically with -listallhardwareports instead of hard-coding “Wi-Fi”, because adapters and localized names differ between Macs.
  • Check the exit status and re-read the value, since some commands report errors only as text.
  • Use network locations (-createlocation, -switchtolocation) to keep alternative profiles such as office and home settings separate.
  • Avoid storing Wi-Fi passwords in plain script files; prefer configuration profiles for credentials.

The decision table below summarizes which approach fits typical administrative situations.

SituationRecommended approachReason
One Mac needs new DNS serversnetworksetup -setdnsserversQuick, persistent and visible in settings
Hundreds of Macs need a proxyMDM configuration profileEnforced and reported centrally
Temporary test of an MTU valueifconfig or -setMTUEasy to revert
Troubleshooting DNS resolutionscutil --dnsShows the resolver order actually in use
Choosing an approach for common tasks

Limitations and common mistakes

networksetup is powerful but not a complete network manager. It cannot configure every VPN type, and settings locked by a configuration profile may be reapplied by the management system shortly after a script changes them.

Recent macOS releases also restrict access to Wi-Fi network names for privacy reasons, so commands such as -getairportnetwork may return incomplete output on current systems. Another frequent error is passing a device name where a service name is expected, which produces a message that the service is not recognized. Finally, clearing DNS requires the literal word “Empty”; passing an empty string leaves the old servers in place.

In everyday language

Every Mac has a Network page in its settings. There you pick how it connects, which servers it asks for website addresses and whether it uses a proxy. networksetup is a typed version of that page. Instead of clicking, you type one line in Terminal.

Think of it as a remote control for network settings. It does the same job as the buttons, but a script can press them for you. The changes stay in place after a restart, just like changes made with the mouse.

Seen in the real world

An IT technician at a school with 60 MacBook laptops must move every machine to new internal DNS servers before a weekend migration. Clicking through System Settings on each laptop would take most of a day, so the technician writes a short zsh script. It finds the Wi-Fi service with networksetup -listallhardwareports, runs networksetup -setdnsservers with the two new addresses and then checks the result with -getdnsservers.

The management system pushes the script to all laptops as a root policy. Within an hour, the reports show that 58 machines applied the change and two failed because they used a USB Ethernet adapter with a different service name. After the script is adjusted to handle any active service, the migration completes without a single manual visit.