Mac OS | Edit the hosts File

How to edit the macOS hosts file with administrator privileges and flush the DNS cache

Overview

The hosts file maps host names to IP addresses before DNS is used. It is mainly edited during development or when you need to communicate with a host name for a specific reason.

Open an Editor from the Terminal

Editing the hosts file requires administrator privileges, so be sure to use the sudo command.

$ sudo vi /etc/hosts

Or:

$ sudo vi /private/etc/hosts

Edit the Contents

  1 ##
  2 # Host Database
  3 #
  4 # localhost is used to configure the loopback interface
  5 # when the system is booting.  Do not change this entry.
  6 ##
  7 127.0.0.1   localhost
  8 255.255.255.255 broadcasthost
  9 ::1             localhost
 10 fe80::1%lo0 localhost

Enter the IP address and separate it with a tab.

127.0.0.1    www.devkuma.com

Refresh the DNS Cache

After editing the hosts file, reboot or run dscacheutil -flushcache to apply the change immediately.

$ dscacheutil -flushcache