While you can always use the system network preferences panel to change the DNS servers on your Mac, some geeks would prefer using the Terminal to get the job done. Using Terminal to change the DNS servers has its advantages, like it lets you remotely access the DNS servers with SSH, and it even works when the GUI is dead on your machine. Also, while troubleshooting your Mac, you may find it useful to be able to change DNS servers from the Terminal.
In this article we will show you how to use the
networksetup command to change the servers. This command brings with it a number of things you can do on your machine, but we will only be using it to modify the DNS servers.
Changing the DNS Servers Using Terminal on a Mac
Click on Launchpad in your dock, search for and click on Terminal, and it will launch for you.
When Terminal launches, type in the following command and press Enter. Please read the command parameters carefully to know what that command is going to do on your machine.
networksetup -setdnsservers Wi-Fi 184.108.40.206
- networksetup – the command that helps you modify your connection
- -setdnsservers – the flag that sets the DNS servers on your machine
- Wi-Fi – your network connection
- 220.127.116.11 – the new DNS server that will be used on your machine
As soon as you press Enter, you will be prompted to enter your admin account password. Enter the password and click on “Modify Configuration” to move forward with the process.
You will not get any confirmation that the DNS servers have been changed on your Mac. Instead you will simply see the normal Terminal window, and it is fine as long as there are no errors.
Not only can you set up a single DNS server with the above command, you can also have multiple servers to fall back on when one does not work. The following command can be used to set up multiple DNS Servers:
networksetup -setdnsservers Wi-Fi 18.104.22.168 22.214.171.124
As you can see, the servers have been separated by a space. You can add as many servers as you want to ensure your Internet does not stop working when one of these servers is down.
If you wish to know whether the DNS servers were successfully changed, you can simply issue the following command to check the status:
networksetup -getdnsservers Wi-Fi
You should be able to see the configured DNS servers in the Terminal Window. These are the current DNS servers that are working on your machine.
It is advised that you clear the DNS cache after you have changed the servers on your machine. Your Mac has a built-in command that lets you clear the DNS cache within a second.
Just type in the following command into Terminal and press Enter. You will be prompted to enter your password. Do so and you will be good to go.
sudo discoveryutil mdnsflushcache
While most users prefer using GUI to change settings on their Macs, there are times when you are required to use the command line, such as SSH, to get the task done. The above method should help you change DNS servers on your Mac right from the Terminal.