How do I refresh the hosts file on OS X?

79

30

I'm trying to develop using subdomains on OSX but don't want to reboot the computer to refresh the hosts file. Normally on linux I'd do /etc/init.d/networking restart but I can't figure out how to do this on OSX.

chrism2671

Posted 2011-10-14T09:02:59.710

Reputation: 1 139

You probably just need to clear your web browser's cache. – ma11hew28 – 2018-05-17T15:43:33.070

Answers

111

You don't need to. Simply sudo vim /etc/hosts, change what you need to change and go on. Your changes will be applied instantly.

If ping yourdomain.com still gives you the wrong IP, try clearing your DNS cache:

OSX 10.4 and below: lookupd -flushcache
OSX 10.5 + 10.6: dscacheutil -flushcache
OSX 10.7 + 10.8: sudo killall -HUP mDNSResponder
OSX 10.9 and above: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
OSX 10.12 and above: sudo dscacheutil -flushcache

vzwick

Posted 2011-10-14T09:02:59.710

Reputation: 1 227

For clearing dns cache, see: https://support.opendns.com/entries/26336865-Clearing-the-DNS-Cache-on-Computers-Servers-and-Web-Browsers

– Romans-8---31-39 – 2014-09-20T21:36:12.660

2Make sure you obviously use the right order too (IP address, then DNS). – user1442960 – 2014-12-12T23:03:28.967

@user1442960 That sounds so simple, but I had a brain fart and added host entries that were incorrect. As vzwick said, the changes were applied instantly as soon as I corrected them. – Nate Barbettini – 2017-09-09T02:53:44.773

OSX 10.12 here, sudo dscacheutil -flushcache was just enough to clear the cache – oski86 – 2017-09-13T13:41:28.250

On MacOS 10.14.5 dscacheutil -flushcache was enough – zengabor – 2019-06-09T10:06:38.750

4

For El Capitan, go to the Terminal app and type:

sudo killall -HUP mDNSResponder

Source

Supra888

Posted 2011-10-14T09:02:59.710

Reputation: 41

0

For Sierra: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

from https://www.indabaa.com/how-to-reset-flush-dns-cache-in-macos-sierra/

seizethecarp

Posted 2011-10-14T09:02:59.710

Reputation: 517

0

I understand from https://serverfault.com/questions/478534/how-is-dns-lookup-configured-for-osx-mountain-lion that /etc/hosts and /etc/resolv.conf are really no longer used on OS X Sierra 10.12.* (and probably earlier). Indeed when I tried to add hosts to /etc/hosts and reset the cache with the above instructions, the hosts were not recognized, via the host command. However, ping does recognize them.

The networksetup command replaces the "old" way of doing things, or use the Network GUI. For example, to specify dns servers to use:

sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4

Kevin Buchs

Posted 2011-10-14T09:02:59.710

Reputation: 101

-1

You will need to enter a command to open the Nano text editor. You will need your administrator password, as well. type sudo nano /etc/hosts and then hit return. Enter your administrator password and then hit return.

Luke Dawn

Posted 2011-10-14T09:02:59.710

Reputation: 1