MacOS – Change hosts file does not have any effect

dnshostsmacosping

I have edited the /etc/hosts file on macOS 10.12.6. Then trying to flush the DNS services has no effect, why?

Here is what I have done:

cd /etc
sudo cat hosts
sudo cat -te 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^Ilocalhost$
255.255.255.255^Ibroadcasthost$
::1^Ilocalhost$
127.0.0.1^Ipagead46.l.doubleclick.net$
$
$

ping pagead46.l.doubleclick.net
PING pagead46.l.doubleclick.net (172.217.23.66): 56 data bytes
64 bytes from 172.217.23.66: icmp_seq=0 ttl=56 time=678.921 ms
64 bytes from 172.217.23.66: icmp_seq=1 ttl=56 time=35.715 ms
64 bytes from 172.217.23.66: icmp_seq=2 ttl=56 time=52.304 ms
64 bytes from 172.217.23.66: icmp_seq=3 ttl=56 time=48.495 ms
^C
--- pagead46.l.doubleclick.net ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 35.715/203.859/678.921/274.346 ms


dscacheutil -flushcache
ps -ef | grep mDNS
    0  1777     1   0  6:46PM ??         0:00.11 /usr/sbin/mDNSResponderHelper
   65  1916     1   0  8:17PM ??         0:00.07 /usr/sbin/mDNSResponder
  501  1929  1013   0  8:19PM ttys007    0:00.00 grep mDNS

dscacheutil -flushcache
sudo killall mDNSResponder
sudo killall mDNSResponderHelper
ps -ef | grep mDNS
   65  1933     1   0  8:20PM ??         0:00.03 /usr/sbin/mDNSResponder
  501  1936  1013   0  8:20PM ttys007    0:00.00 grep mDNS

sudo cat -te 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^Ilocalhost$
255.255.255.255^Ibroadcasthost$
::1^Ilocalhost$
127.0.0.1^Ipagead46.l.doubleclick.net$
$
$

ping pagead46.l.doubleclick.net
PING pagead46.l.doubleclick.net (172.217.23.66): 56 data bytes
64 bytes from 172.217.23.66: icmp_seq=0 ttl=56 time=43.226 ms
64 bytes from 172.217.23.66: icmp_seq=1 ttl=56 time=39.457 ms
64 bytes from 172.217.23.66: icmp_seq=2 ttl=56 time=36.937 ms
^C
--- pagead46.l.doubleclick.net ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 36.937/39.873/43.226/2.584 ms

cat -te ... shows a ^I for tabs and a $ for newline to show what is exactly
in the file.

None of my browsers (Safari or Chrome) seem to honor the new hosts entry.

I am most certainly doing something wrong here, any help is welcome.

Best Answer

I'd recommend Gas Mask (freeware, no affiliation) which not only eliminates the need to try hack it in terminal, but also allows you multiple versions of the hosts file, switchable on the fly, instant response.

enter image description here

To use, Create a new file - it will setup the defaults automatically. Copy/paste any existing setup you want to carry over from any file to any other. Add any new data in the same format - it's very lenient on formatting, tab or space.
Save, then activate.
You can swap profiles any time the app is running, otherwise your last chosen will persist.
Activating a profile makes it immediately available.
Note: the use of 127.0.0.1 rather than localhost for IPV6 is non-standard, but just seems to make the Mac behave better