2

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.
##
127.0.0.1 somethig.com
127.0.0.1 localhost
255.255.255.255 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.

3

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

127.0.0.1 www3.mytestsite.com
0

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
0

Works for me on a 10.13.3 system.

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

However! Some applications (e.g. Google Chrome) may make their own queries to their own DNS servers (e.g. to 8.8.8.8) 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 93.184.216.34
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946
  • I added one more line: 0.0.0.0 asdf.com Now host asdf.com prints asdf.com has address 64.90.40.65 and dns-sd -G v4 asdf.com prints 19:59:37.634 Add 2 -1 asdf.com. 0.0.0.0 1 Does this mean that my system completely ignores /etc/hosts? – dnnagy Mar 6 '18 at 19:01

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.