Set macOS DNS Server IP Using Terminal
DNS is the service that translates fully qualified hostnames into an IP address. We can change the DNS setting from the command line of macOS by using the networksetup command or the scutil command.
While networksetup and scutil have many advanced and complex uses, setting DNS with networksetup is actually quite easier and faster than doing it with scutil.
To set the DNS manually we can use the networksetup command, followed by the -setdnsservers flag, followed by the network service and finally the DNS-IP, like this:
networksetup -setdnsservers <Network Service> <DNS-IP>
DNS setting is interface specific, each interface has it's own DNS servers set to it. To list the interfaces (network services) use:
root [~] $ networksetup -listallnetworkservices An asterisk (*) denotes that a network service is disabled. Ethernet iPhone USB Wi-Fi
Set The DNS
networksetup -setdnsservers Ethernet 188.8.131.52
networksetup -setdnsservers Wi-Fi 184.108.40.206
You can also set fallback DNS servers, like this:
networksetup -setdnsservers Ethernet 220.127.116.11 18.104.22.168 22.214.171.124
Clear Custom DNS Servers
If you are using DHCP then your router can take care of DNS for you, and you can clear the settings by actually omitting the IP numbers while executing the same flag, like this:
networksetup -setdnsservers Ethernet
If you configured your IP manually then you need to have at least one DNS server configured. Without it, the interface won't be able to resolve domain names.