I tried to use /etc/hosts file to redirect some websites to localhost. To do so, I opened it in terminal using sudo nano /etc/hosts then I modified the file, and saved it. As the last step I flushed the DNS cache with sudo killall -HUP mDNSResponder.

Here's what my hosts file looks like:

$ cat /etc/hosts
# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
## somethig.com localhost broadcasthost
::1 localhost

This simply doesn't work. I tried to reboot, without luck. I also did a lot of research but did not find any working answers on the internet. Does the OS still use this file, or will this never really work?

I'm using macOS 10.13.


I had the hosts and IP addresses separated by tabs, switching to just one space between them worked for me: www3.mytestsite.com

OSXDaily has a different command set for High Sierra - maybe worth a try...

sudo killall -HUP mDNSResponder; sleep 2; echo macOS DNS Cache Reset | say

or if it's being sticky...

sudo killall -HUP mDNSResponder && echo macOS DNS Cache Reset

Works for me on a 10.13.3 system.

% grep example /etc/hosts example.com
% perl -MSocket -e 'printf "%vx\n", inet_aton "example.com"'
% dns-sd -G v4 example.com
DATE: ---Tue 06 Mar 2018---
10:49:47.455  ...STARTING...
Timestamp     A/R Flags if Hostname                               Address
10:49:47.457  Add     2 -1 example.com.                 

However! Some applications (e.g. Google Chrome) may make their own queries to their own DNS servers (e.g. to or applications could easily ignore the /etc/hosts file if they only make DNS requests and do not go through the system resolver.

% host example.com
example.com has address
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946
  • I added one more line: asdf.com Now host asdf.com prints asdf.com has address and dns-sd -G v4 asdf.com prints 19:59:37.634 Add 2 -1 asdf.com. 1 Does this mean that my system completely ignores /etc/hosts?
    – dnnagy
    Mar 6 '18 at 19:01
  • The host command has never looked at /etc/hosts. Nor has dig. Their job is to perform the DNS queries themselves. You're better off trying to use ping or curl -v http://asdf.org, which should use the system resolver and honor /etc/hosts. Mar 31 '20 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.