Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
25.10.2024

The Hosts File: Where It Is Located and How to Edit It

The hosts file is a plain text file that maps domain names to IP addresses. It acts as a local DNS resolver on your computer, allowing you to override DNS settings and define how specific domain names are resolved. This can be useful for testing websites, blocking unwanted sites, or troubleshooting network issues.

In this article, we’ll explain where the hosts file is located on different operating systems (Windows, macOS, and Linux) and provide step-by-step instructions on how to edit it.


What is the Hosts File?

The hosts file is a simple text file used by operating systems to map hostnames (domain names) to IP addresses. When you try to visit a website, your computer checks the hosts file first to see if there are any custom mappings for that domain. If an entry exists, the computer will use the IP address specified in the hosts file instead of querying a DNS server.

Common Uses of the Hosts File:

  • Website testing: Redirect a domain to a local server or staging environment for testing.
  • Block access to websites: Map unwanted domains (e.g., ad servers) to a non-existent IP address like 127.0.0.1.
  • Bypass DNS issues: Manually set an IP address for a domain when DNS resolution is problematic.

Where is the Hosts File Located?

The location of the hosts file depends on the operating system you are using.

1. Windows

In Windows, the hosts file is located in the following directory:

C:\Windows\System32\drivers\etc\hosts

2. macOS and Linux

On both macOS and Linux, the hosts file is located at:

/etc/hosts

How to Edit the Hosts File

Editing the hosts file is straightforward, but it requires administrator or root permissions because it is a system file. Below are the steps to edit the hosts file on Windows, macOS, and Linux.


Editing the Hosts File on Windows

Step 1: Open Notepad as Administrator

  • Press Windows Key + S and type Notepad in the search bar.
  • Right-click on Notepad and select Run as administrator.

Step 2: Open the Hosts File

  • In Notepad, click on File > Open.
  • Navigate to the following folder: C:\Windows\System32\drivers\etc\
  • In the file type dropdown (next to the File name field), select All Files.
  • Select the hosts file and click Open.

Step 3: Edit the Hosts File

  • To add a new domain mapping, enter the IP address followed by a space and the domain name. For example:
    127.0.0.1 example.com 192.168.1.1 testsite.com
  • This will redirect example.com to your local machine (127.0.0.1) and testsite.com to the IP address 192.168.1.1.

Step 4: Save the File

  • Once you’ve made your changes, click File > Save to save the hosts file. You may need to confirm administrator permissions.

Step 5: Clear DNS Cache

  • After editing the hosts file, it’s a good idea to clear the DNS cache to ensure that the changes take effect immediately. To do this, open Command Prompt and type:
    ipconfig /flushdns
  • Press Enter to clear the cache.

Editing the Hosts File on macOS

Step 1: Open Terminal

  • Press Command + Space to open Spotlight and type Terminal, then press Enter.

Step 2: Open the Hosts File in a Text Editor

  • In the Terminal, enter the following command to open the hosts file using nano (a text editor):
    sudo nano /etc/hosts
  • You will be prompted to enter your administrator password.

Step 3: Edit the Hosts File

  • Once the hosts file is open in nano, add new entries at the bottom. For example:
    127.0.0.1 example.com 192.168.1.1 testsite.com

Step 4: Save the File

  • To save the file in nano, press Control + O (the letter O), then press Enter to confirm.
  • Exit the editor by pressing Control + X.

Step 5: Flush DNS Cache

  • To make sure your changes take effect, flush the DNS cache by running the following command in Terminal:
    sudo killall -HUP mDNSResponder
  • Press Enter.

Editing the Hosts File on Linux

Step 1: Open the Terminal

  • Launch the Terminal application.

Step 2: Open the Hosts File in a Text Editor

  • Use a text editor such as nano to open the hosts file:
    sudo nano /etc/hosts
  • You will be asked to enter your root password.

Step 3: Edit the Hosts File

  • Add your new domain mappings at the bottom of the file. For example:
    127.0.0.1 example.com 192.168.1.1 testsite.com

Step 4: Save the File

  • Save your changes by pressing Control + O and then press Enter.
  • Exit nano by pressing Control + X.

Step 5: Flush DNS Cache

  • Depending on your Linux distribution, you may need to flush the DNS cache for the changes to take effect. On Ubuntu, you can run:
    sudo systemd-resolve –flush-caches

Best Practices When Editing the Hosts File

  1. Create a Backup: Before editing the hosts file, make a backup of the original file. This way, if something goes wrong, you can restore the original version.
    • Example: sudo cp /etc/hosts /etc/hosts.backup
  2. Use Comments: You can add comments in the hosts file by starting a line with #. This is useful for documenting changes or explaining why certain entries are there.
    • Example:
      # Local testing environment for example.com 127.0.0.1 example.com
  3. Be Careful When Blocking Websites: Blocking websites by redirecting them to 127.0.0.1 can be useful, but be cautious when doing so. Blocking legitimate services or websites could disrupt important functions or services on your computer.

Conclusion

The hosts file is a powerful tool for managing domain-to-IP mappings on your local computer. Whether you need to test websites locally, block unwanted sites, or troubleshoot network problems, knowing how to locate and edit the hosts file is essential. Always be careful when modifying this system file and ensure that you make a backup before making changes.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills