macOS

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.
Captain Salem 3 min read
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
127.0.0.1    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 staging.geekbits.io to resolve that locally hosted website.

Since the domain staging.geekbits.io 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:

127.0.0.1    staging.geekbits.io

Save and close the file.

Finally, open the browser and navigate to the address

http:/staging.geekbits.io:8000

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 bing.com. We can use the entry as shown:

127.0.0.1 	bing.com

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

To confirm the changes, open the browser and navigate to bing.com.

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

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

ping -c 3 bing.com 

Output

PING localhost (127.0.0.1): 56 data bytes                                   
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.124 ms                    
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.192 ms                    
64 bytes from 127.0.0.1: 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

Conclusion

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

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.