88

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.

  • You probably just need to clear your web browser's cache. – ma11hew28 May 17 '18 at 15:43
126

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

| improve this answer | |
  • For clearing dns cache, see: support.opendns.com/entries/… – Romans-8---31-39 Sep 20 '14 at 21:36
  • 2
    Make sure you obviously use the right order too (IP address, then DNS). – user1442960 Dec 12 '14 at 23:03
  • @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 Sep 9 '17 at 2:53
  • OSX 10.12 here, sudo dscacheutil -flushcache was just enough to clear the cache – oski86 Sep 13 '17 at 13:41
  • On MacOS 10.14.5 dscacheutil -flushcache was enough – zengabor Jun 9 '19 at 10:06
6

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

sudo killall -HUP mDNSResponder

Source

| improve this answer | |
0

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

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

| improve this answer | |
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
| improve this answer | |
-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.

| improve this answer | |

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.