Edit Etc/Hosts Linux

In various instances, you will need to edit the host file in the system. That can be to either use it as a firewall, i.e., control some network traffic, add a domain name, or test out its functionality.

The host file is a local file or local DNS system that contains a static table lookup for hostnames and IP addresses. It is available in all operating systems, including Windows, Linux, and macOS.

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

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

How to Edit the Host File

You will find the Linux host file stored in the /etc directory. That means you will require sudo privileges or root user to modify its contents.

The general 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.

# The following line is a comment and be 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 Case 1

Allow me to show you an example edit of the host file. In my example, I have a local website running on port 8000, and I want to use the domain development.local.

Since the domain development.local is not a valid domain, I cannot rely on DNS to resolve it. Hence, I can edit the host file as:

Finally, I can add the entry shown below: development.local *.local

Save and close the file.

Finally, open the browser and navigate to the address


If the hostname is correct, the website hosted on the specified port will load.

Example Use Case 2

The next use case is to block a website by redirecting the traffic to an invalid IP address. For example, to block google.com, add the IP address to localhost as:

NOTE: In the above example, we used the Hex IP notation to represent localhost.

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

As you can see, the address does not resolve to the right address despite my internet connection working correctly.

A simple ping also shows that the address resolves to localhost as shown below:

NOTE: Modifications to the /etc/hosts files work instantly, excluding instances where applications cache the file.


In this tutorial, we covered how to edit the hosts file in Linux and use it as local DNS or a Firewall. Editing the host file comes in handy, especially in cases where the DNS server is down.