macOS — Change DNS Server IP Addresses Using the Command Line.
Learn how to inspect, configure, verify, and restore DNS settings on macOS using the native `networksetup` command-line utility.
Domain Name System (DNS) resolution is an essential part of network connectivity. Whenever your Mac accesses a website or connects to a service using a hostname, DNS helps translate that hostname into an IP address.
macOS allows you to configure DNS servers through System Settings, but you can also manage DNS configuration directly from Terminal.
In this tutorial, we'll use the native networksetup command-line utility to inspect existing DNS settings, configure custom DNS resolvers, verify the changes, and restore automatic DNS configuration.
This approach is useful for system administrators, developers, network troubleshooting, and anyone who prefers managing macOS network settings from the command line.
1. Identify available network services
Before modifying DNS configuration, identify the network service you want to configure.
Run:
$ networksetup -listallnetworkservicesExample output:
An asterisk (*) denotes that a network service is disabled.
Wi-Fi
USB 10/100/1000 LAN
Thunderbolt BridgeThe service name is important. networksetup expects the configured network service name, not necessarily the hardware interface name.
For example, Wi-Fi is a network service name, whereas en0 is commonly a hardware interface identifier.
Do not assume your Mac uses en0 for Wi-Fi.
Verify the mapping:
$ networksetup -listallhardwareportsExample output:
Hardware Port: Wi-Fi
Device: en0
Ethernet Address: xx:xx:xx:xx:xx:xx
Hardware Port: Thunderbolt Ethernet
Device: en5
Ethernet Address: xx:xx:xx:xx:xx:xxUse the actual service name returned by your system in the commands that follow.
2. Check the current DNS configuration
To inspect the DNS servers configured for Wi-Fi:
$ networksetup -getdnsservers "Wi-Fi"The command may return one or more DNS server IP addresses.
For example:
192.168.1.1
8.8.8.8If no manual DNS servers are configured, macOS may report:
There aren't any DNS Servers set on Wi-Fi.This does not necessarily mean DNS resolution is unavailable. Your Mac may receive DNS server information automatically through DHCP.
To inspect the broader resolver configuration currently used by macOS:
$ scutil --dnsThis provides additional information about DNS resolvers, search domains, and resolver configuration.
3. Change DNS servers using Terminal
The networksetup utility provides the -setdnsservers option to configure DNS servers for a specific network service.
The syntax is:
$ sudo networksetup -setdnsservers "NETWORK_SERVICE" DNS_SERVER_1 DNS_SERVER_2Replace NETWORK_SERVICE with the actual service name and provide the IP addresses of your preferred DNS resolvers.
Example: Configure Cloudflare DNS
Cloudflare provides the following public DNS resolver addresses:
| Protocol | Primary | Secondary |
|---|---|---|
| IPv4 | 1.1.1.1 | 1.0.0.1 |
To configure Cloudflare DNS for Wi-Fi:
$ sudo networksetup -setdnsservers "Wi-Fi" 1.1.1.1 1.0.0.1Enter your administrator password when prompted.
Example: Configure Google Public DNS
Google Public DNS provides these IPv4 resolver addresses:
| Protocol | Primary | Secondary |
|---|---|---|
| IPv4 | 8.8.8.8 | 8.8.4.4 |
Configure them with:
$ sudo networksetup -setdnsservers "Wi-Fi" 8.8.8.8 8.8.4.4Example: Configure Quad9 DNS
Quad9 provides these IPv4 resolver addresses:
| Protocol | Primary | Secondary |
|---|---|---|
| IPv4 | 9.9.9.9 | 149.112.112.112 |
Configure them with:
$ sudo networksetup -setdnsservers "Wi-Fi" 9.9.9.9 149.112.112.112
Important: These are alternative configurations. Choose the resolver provider you intend to use rather than combining unrelated providers without a specific reason.
4. Verify the DNS configuration
After changing DNS servers, inspect the service configuration again:
$ networksetup -getdnsservers "Wi-Fi"Expected output for Cloudflare:
1.1.1.1
1.0.0.1You can also inspect the active resolver configuration:
$ scutil --dnsTo test DNS resolution for a domain:
$ dscacheutil -q host -a name snubmonkey.comAlternatively, query a specific DNS server directly:
$ dig @1.1.1.1 snubmonkey.com
The dig command helps distinguish between a resolver-specific problem and a broader system DNS configuration issue.
5. Flush the macOS DNS cache
Changing DNS servers does not necessarily remove previously cached DNS answers.
If you are troubleshooting stale DNS resolution, flush the system DNS cache:
$ sudo dscacheutil -flushcacheYou can also signal the macOS DNS responder to reload:
$ sudo killall -HUP mDNSResponderThese commands are troubleshooting measures, not mandatory steps after every DNS configuration change.
6. Restore automatic DNS configuration
If you want to remove manually configured DNS servers and allow the network service to obtain DNS information automatically, use the special empty argument.
$ sudo networksetup -setdnsservers "Wi-Fi" emptyVerify the result:
$ networksetup -getdnsservers "Wi-Fi"You should see:
There aren't any DNS Servers set on Wi-Fi.This removes manually configured DNS entries for that service. It does not disable DNS resolution; DHCP or another network configuration mechanism can supply the DNS servers.
7. Configure DNS on an Ethernet service
The same commands work with Ethernet and other supported network services.
First, identify the service name:
$ networksetup -listallnetworkservicesThen configure DNS using the actual Ethernet service name.
For example:
$ sudo networksetup -setdnsservers "USB 10/100/1000 LAN" 1.1.1.1 1.0.0.1Verify:
$ networksetup -getdnsservers "USB 10/100/1000 LAN"If your Mac uses both Wi-Fi and Ethernet, configure each service separately when necessary.
8. Automate DNS configuration with a shell script
For administrators who regularly switch between DNS providers, a small shell script can simplify the process.
The following example configures Cloudflare DNS for the Wi-Fi service.
#!/bin/bash
set -euo pipefail
SERVICE="Wi-Fi"
DNS_PRIMARY="1.1.1.1"
DNS_SECONDARY="1.0.0.1"
if [[ "$EUID" -ne 0 ]]; then
echo "Run this script with sudo."
exit 1
fi
if ! /usr/sbin/networksetup -listallnetworkservices |
grep -Fxq "$SERVICE"; then
echo "Network service not found: $SERVICE"
exit 1
fi
echo "Configuring DNS for: $SERVICE"
/usr/sbin/networksetup -setdnsservers \
"$SERVICE" \
"$DNS_PRIMARY" \
"$DNS_SECONDARY"
echo
echo "Current DNS configuration:"
/usr/sbin/networksetup -getdnsservers "$SERVICE"
echo
echo "DNS configuration completed."Save the script as set-dns.sh.
Make it executable:
$ chmod +x set-dns.shExecute it:
$ sudo ./set-dns.shThe script checks that it is running with administrator privileges and that the requested network service exists before modifying DNS settings.
Change the SERVICE and DNS variables to suit your environment.
Troubleshooting and Tips
A few things are worth checking when DNS configuration does not behave as expected.
1. Incorrect network service name
Use networksetup -listallnetworkservices to identify the exact service name. Preserve spaces and capitalization by enclosing the name in quotation marks.
2. VPN software or configuration profiles
A VPN client, mobile device management profile, or other managed network configuration can influence DNS resolution. A manually configured service DNS setting does not guarantee that all applications will use that resolver.
3. Multiple network interfaces
Wi-Fi, Ethernet, USB adapters, and VPN interfaces can have separate DNS configurations. Inspect the active resolver configuration with scutil --dns rather than assuming one service represents the entire system.
4. DNS changes do not require a rebootnetworksetup applies the configuration directly. In normal circumstances, neither restarting macOS nor restarting the network service is necessary.
5. DNS configuration is not the same as changing your IP address
The commands in this tutorial configure DNS resolver addresses. They do not change your Mac's local IPv4 address, subnet mask, or default gateway.
networksetup provides a simple, reliable way to inspect, configure, and restore DNS settings on macOS directly from the command line.
Understanding how network services, interfaces, and DNS resolvers relate makes managing and troubleshooting macOS network configurations much easier.
We hope this was helpful.
Keep learning. Keep exploring. Stay secure.