Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Host file override not working in Mavericks

I updated the host file using the following command:


sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts


Then ran the following (http://support.apple.com/kb/HT5343) to clear DNS cache:


sudo killall -HUP mDNSResponder


This doesn't seem to be working. A simple Dig before and after this command is enough to confirm that changes to DNS entry in host file are not being taken.


There is another possibility where Dig is not honoring the hosts file. Is there a way to force the DNS lookup against the host file first on Mavericks?


Thanks.

MacBook Pro, OS X Mavericks (10.9.2), host file

Posted on Apr 21, 2014 11:39 AM

Reply
Question marked as Best reply

Posted on Apr 21, 2014 3:54 PM

The hosts file is cached. Rebooting OS X will reload it. You can also attempt to force a cache reload with the following command:

sudo killall -HUP mDNSResponder


Unfortunately, using TextEdit can corrupt the file — I'd guess that this is at the core of the reload problem here. The file needs to be plain ASCII text, and TextEdit usually generates non-ASCII files. Use the TextWrangler tool (free) if really you want a GUI tool, or use the nano editor (or vim or emacs, if you're more familiar with those) at the command line:


sudo nano /etc/hosts


If you need to recover the hosts file, here are the default contents:


##

# Host Database

#

# localhost is used to configure the loopback interface

# when the system is booting. Do not change this entry.

##

127.0.0.1 localhost

255.255.255.255 broadcasthost

::1 localhost

fe80::1%lo0 localhost

9 replies
Question marked as Best reply

Apr 21, 2014 3:54 PM in response to anajat

The hosts file is cached. Rebooting OS X will reload it. You can also attempt to force a cache reload with the following command:

sudo killall -HUP mDNSResponder


Unfortunately, using TextEdit can corrupt the file — I'd guess that this is at the core of the reload problem here. The file needs to be plain ASCII text, and TextEdit usually generates non-ASCII files. Use the TextWrangler tool (free) if really you want a GUI tool, or use the nano editor (or vim or emacs, if you're more familiar with those) at the command line:


sudo nano /etc/hosts


If you need to recover the hosts file, here are the default contents:


##

# Host Database

#

# localhost is used to configure the loopback interface

# when the system is booting. Do not change this entry.

##

127.0.0.1 localhost

255.255.255.255 broadcasthost

::1 localhost

fe80::1%lo0 localhost

Apr 23, 2014 2:53 PM in response to MrHoffman

@MrHoffman - Thanks. That was good tip around the using the correct text editor.


Though I discovered that the host file was not corrupt, I restored it nonetheless, added my changes, ran the command sudo killall -HUP mDNSResponder, restarted the machine and still no luck.


@etresoft - Thanks. I have tried dig/ping/traceroute to check the IP. Changes to the host file are not taking effect.

Apr 24, 2014 11:54 AM in response to etresoft

sir, you read it wrong. the command works as intended. but hosts file is not being used. a tip from another website says to add extra code to resolv.conf, but that gets overwritten every reboot. please advise.



/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 localhost

255.255.255.255 broadcasthost

::1 localhost

fe80::1%lo0 localhost


127.0.0.1 apple.com

after a reboot:


$ nslookup apple.com

Server: 10.30.56.234

Address: 10.30.56.234#53


Non-authoritative answer:

Name: apple.com

Address: 17.149.160.49

Name: apple.com

Address: 17.178.96.59

Name: apple.com

Address: 17.172.224.47


$ dig apple.com


; <<>> DiG 9.8.3-P1 <<>> apple.com

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3746

;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0


;; QUESTION SECTION:

;apple.com. IN A


;; ANSWER SECTION:

apple.com. 1436 IN A 17.178.96.59

apple.com. 1436 IN A 17.172.224.47

apple.com. 1436 IN A 17.149.160.49


;; Query time: 8 msec

;; SERVER: 10.30.56.234#53(10.30.56.234)

;; WHEN: Thu Apr 24 13:50:45 2014

;; MSG SIZE rcvd: 75


$ ping apple.com

PING apple.com (17.172.224.47): 56 data bytes

Request timeout for icmp_seq 0

Request timeout for icmp_seq 1

Request timeout for icmp_seq 2

^C

--- apple.com ping statistics ---

4 packets transmitted, 0 packets received, 100.0% packet loss


#(NOTE: This is normal for me, my firewall blocks icmps, but still resolves wrong)


$ host -v apple.com

Trying "apple.com"

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56674

;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0



;; QUESTION SECTION:

;apple.com. IN A



;; ANSWER SECTION:

apple.com. 1305 IN A 17.149.160.49

apple.com. 1305 IN A 17.178.96.59

apple.com. 1305 IN A 17.172.224.47



Received 75 bytes from 10.30.56.234#53 in 0 ms

Trying "apple.com"

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 16733

;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0



;; QUESTION SECTION:

;apple.com. IN AAAA



;; AUTHORITY SECTION:

apple.com. 85699 IN SOA gridmaster-ib.apple.com. hostmaster.apple.com. 2010088892 1800 900 2016000 86500



Received 88 bytes from 10.20.0.50#53 in 0 ms

Trying "apple.com"

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48782

;; flags: qr rd ra; QUERY: 1, ANSWER: 11, AUTHORITY: 0, ADDITIONAL: 11



;; QUESTION SECTION:

;apple.com. IN MX



;; ANSWER SECTION:

apple.com. 3599 IN MX 100 mail-in100.apple.com.

apple.com. 3599 IN MX 10 mail-in2.apple.com.

apple.com. 3599 IN MX 20 mail-in25.apple.com.

apple.com. 3599 IN MX 20 mail-in24.apple.com.

apple.com. 3599 IN MX 20 mail-in23.apple.com.

apple.com. 3599 IN MX 20 mail-in22.apple.com.

apple.com. 3599 IN MX 20 mail-in21.apple.com.

apple.com. 3599 IN MX 10 mail-in7.apple.com.

apple.com. 3599 IN MX 10 mail-in6.apple.com.

apple.com. 3599 IN MX 10 mail-in5.apple.com.

apple.com. 3599 IN MX 10 mail-in4.apple.com.



;; ADDITIONAL SECTION:

mail-in100.apple.com. 8 IN A 17.151.62.23

mail-in2.apple.com. 8 IN A 17.151.62.17

mail-in25.apple.com. 8 IN A 17.171.2.25

mail-in24.apple.com. 8 IN A 17.171.2.24

mail-in23.apple.com. 8 IN A 17.171.2.23

mail-in22.apple.com. 8 IN A 17.171.2.22

mail-in21.apple.com. 8 IN A 17.171.2.21

mail-in7.apple.com. 8 IN A 17.151.62.22

mail-in6.apple.com. 8 IN A 17.151.62.21

mail-in5.apple.com. 8 IN A 17.151.62.19

mail-in4.apple.com. 8 IN A 17.151.62.18



Received 485 bytes from 10.30.56.234#53 in 151 ms


it does not read the hosts file at all.

Apr 24, 2014 12:59 PM in response to yangeryanger

Most of that is irrelevent. Those are host lookup tools. The talk to DNS servers. They ignore the hosts file by design.


The only one that doesn't look right is ping. I think whatever you have done to resolv.conf has screwed up your system. I can redirect apple.com in my hosts file and it works fine:


/Users/jdaniel $ ping apple.com

PING apple.com (127.0.0.1): 56 data bytes

64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.066 ms

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.131 ms

64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.141 ms

64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.134 ms

64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.162 ms


This works even after a reboot.

Apr 25, 2014 11:55 AM in response to etresoft

Simplified it a bit. Just used vim to edit the hosts file. Worked like a charm. Didn't need to reload cache either via sudo killall -HUP mDNSResponder. As soon as the updated file was saved, I could see the change taking effect.


Editing the file:


blr-mpd9q:~ anajat$ sudo vi /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 localhost

255.255.255.255 broadcasthost

::1 localhost

fe80::1%lo0 localhost

127.0.0.2 www.apple.com


Ping works:


blr-mpd9q:~ anajat$ ping www.apple.com

PING www.apple.com (127.0.0.2): 56 data bytes

Request timeout for icmp_seq 0

Request timeout for icmp_seq 1

Request timeout for icmp_seq 2

^C

--- www.apple.com ping statistics ---

4 packets transmitted, 0 packets received, 100.0% packet loss


Traceroute works too:


blr-mpd9q:~ anajat$ traceroute www.apple.com

traceroute to www.apple.com (127.0.0.2), 64 hops max, 52 byte packets

^C


Removed the old entry, saved it and www.apple.com resolved to a public ip as expected:


blr-mpd9q:~ anajat$ sudo vi /etc/hosts

blr-mpd9q:~ anajat$ ping www.apple.com

PING e3191.dscc.akamaiedge.net (173.222.29.15): 56 data bytes

64 bytes from 173.222.29.15: icmp_seq=0 ttl=58 time=2.463 ms

64 bytes from 173.222.29.15: icmp_seq=1 ttl=58 time=2.369 ms

64 bytes from 173.222.29.15: icmp_seq=2 ttl=58 time=2.786 ms

^C

--- e3191.dscc.akamaiedge.net ping statistics ---

3 packets transmitted, 3 packets received, 0.0% packet loss

round-trip min/avg/max/stddev = 2.369/2.539/2.786/0.179 ms

-----------------------------



Thank you all for the quick help!

Sep 23, 2014 10:45 PM in response to anajat

I was hoping the solutions discussed here would work for me, but they don't seem to. In my case, I have a laptop I take from home to outside the home, and a web server at home. So when I'm at home, I need an entry in /etc/hosts that overrides the DNS-returned IP address of my home router (which port-forwards to the web server), and I need that override to be removed when I'm away from home. I have a LaunchAgent on my laptop that watches for my Location changes and switches the /etc/hosts files for me (I almost never reboot my laptop). This worked well in 10.6, but then I upgraded to 10.9 a few months ago and nothing seems to work to get ping to see my new hosts file entry.


Interestingly, Chrome does recognize the entry in /etc/hosts. But ping/dig/host from the command line do not.


Yes, I've tried: dscacheutil -flushcache ; sudo killall -HUP mDNSResponder


Any other suggestions from anyone?

Host file override not working in Mavericks

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.