How to Edit Hosts File in macOS

This quick tutorial will help you understand what the file contains and how you can edit it in the macOS system.

How to Edit Hosts File in macOS

The host file, also known as the hosts file, is a critical component of the local Domain Name System (DNS) resolution mechanism found in various operating systems, such as Windows, Linux, and macOS. It serves as a static table lookup, mapping hostnames to their corresponding IP addresses within the local network environment.

Because it is a local DNS system, it takes precedence over other DNS systems, making it a good choice for unrecognized domains.

How to Edit the Host File

You will find the macOS host file stored in the /etc/hosts ..

Hence, you will require sudo permissions to modify the contents of the file

The syntax for the entries in the host file is:

IP_address canonical_hostname [aliases...]

The IP address specified in the host file can be either an IPv4 or IPv6 address as long as it resolves to the specified domain.

Comments in host File

The host file also supports comments that the system ignores. They begin with an octothorpe (#).

For example, the system will ignore the following entry.

# comment is ignored by the system    localhost
::1       localhost  

Rules for naming hostnames

Now, there are some rules to naming hostnames in the host file, allowing the system to resolve to the specified IP address.

The rules include:

  • Hostnames should not start with a hyphen or a special character except a wildcard character such as an asterisk.
  • The specified hostname should only contain alphanumeric characters a minus sign (-) and/or period (.)
  • The hostname should only begin and end with alphanumeric characters.

Example Use

Suppose we gave a have a local web server running on port 8000, and we want the domain to resolve that locally hosted website.

Since the domain is not a valid domain, we cannot use the DNS server to resovle it. This is where teh host file comes into play.

sudo vim /etc/hosts

In the host file, add the entry as shown:

Save and close the file.

Finally, open the browser and navigate to the address


If the settings are correct, the address above will load the locally hosted website running on port 8000.

Blocking Websites

Another common use case of the hosts file is blocking a website by redirecting the traffic to an invalid address. For example, suppose we wish to block the site We can use the entry as shown:

In the above example, we set the IP address of the local machine to

To confirm the changes, open the browser and navigate to

In this case, the website fails to load as there is no webserver running on localhost which is where the domain redirects to.

You can also test the address by running a simple ping command:

ping -c 3 


PING localhost ( 56 data bytes                                   
64 bytes from icmp_seq=0 ttl=64 time=0.124 ms                    
64 bytes from icmp_seq=1 ttl=64 time=0.192 ms                    
64 bytes from icmp_seq=2 ttl=64 time=0.205 ms                    
--- localhost ping statistics ---                                           
3 packets transmitted, 3 packets received, 0.0% packet loss                 
round-trip min/avg/max/stddev = 0.124/0.174/0.205/0.036 ms 

In some instances, making changes to the hosts file will not take effect immediately due to DNS caching. On macOS, you can flush DNS cache by running the commmand:

sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder


In this tutorial, we covered how to edit the hosts file in macOS and use it as local DNS or a Firewall.

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.