Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OSX changing /etc/hosts has no effect even after killing mDNSResolver

Tags:

macos

dns

hosts

I have not had a similar issue in Windows (10) and nothing I've searched on docs seems to indicate why, if this does not work, that that is the case

  • I open up terminal and edit /etc/hosts (which I've done many times on a PC and a few years back on an OSX too)
  • Just for grins if that doesn't work I type in sudo killall -HUP mDNSResponder
  • Then for more grins I reboot

This has absolutely no effect. Can anyone point me to why? Thanks.


UPDATE: The embarrassing fact of the matter is that the lines I entered in /etc/hosts were in reverse, i.e. domain first, as:

mydomain.com  192.168.33.10    #wrong
192.168.33.10    mydomain.com  #what it should have been

The accepted answer, however, is well-written and appreciated.

like image 391
Oliver Williams Avatar asked Nov 28 '22 06:11

Oliver Williams


2 Answers

I've seen two common problems with using /etc/hosts on macOS (/OS X):

  • Incorrect formatting: each entry in the /etc/hosts file must be an IP address followed by a space or tab, followed by the name, then a linefeed at the end of the line. Try printing the hosts file with cat -vet /etc/hosts to make normally invisible characters visible. Each line should look like one of these:

    127.0.0.1^Iwww.example.com$
    127.0.0.1 www.example.com$
    

    (The "^I" is a tab, and the "$" is the linefeed at the end of the line.) It's also ok if the entry has multiple names listed (also separated by spaces or tabs).

    If you see a "^M" (carriage return) just before the "$", you have DOS/Windows formatted text and you need to remove the carriage return(s).

  • Incorrect testing: Don't use the command-line tools dig, host, and nslookup, since they all test DNS directly and therefore bypass the /etc/hosts file. Browsers sometimes cache things, which can give misleading results. The "right" way to test the system's name resolution system is with the dscacheutil command:

    dscacheutil -q host -a name www.example.com
    

    ...but since that's annoyingly verbose, I tend to just use ping, and see what address it says it's going to test.

like image 129
Gordon Davisson Avatar answered Dec 18 '22 16:12

Gordon Davisson


I came across this thread to try and solve the same issue on macOS Catalina and was not successful. This is because macOS Catalina has another thing going; it will only make changes in the hosts file effective if you change them as the root user (this is not done with the sudo command) !!

By default there is not a root user on your system so here's a link with a step by step guide to do so: https://support.apple.com/en-us/HT204012

then I was able to:

su
nano /etc/hosts

for more information: https://discussions.apple.com/thread/250805304

like image 44
joronimo Avatar answered Dec 18 '22 17:12

joronimo