118

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
  • 1,579

5 Answers5

160

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
OSX 10.14.5 and above: dscacheutil -flushcache

vzwick
  • 1,716
  • 1
  • 13
  • 11
10

For macOS El Capitan (10.11), go to the Terminal app and type:

sudo killall -HUP mDNSResponder

Source

Cœur
  • 479
Supra888
  • 109
1

For macOS Sierra (10.12):

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

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

Cœur
  • 479
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
-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.