How to Edit Your Hosts File

Editing your hosts file can be useful for various tasks such as testing website changes before DNS propagation, blocking access to certain websites, or creating shortcuts to frequently used IP addresses. Here's a step-by-step guide on how to edit your hosts file on different operating systems.

What is the Hosts File?

The hosts file is a plain text file used by your operating system to map hostnames to IP addresses. It's often used to override DNS settings or to block specific websites.

Steps to Edit the Hosts File

On Windows

  1. Open Notepad as Administrator:

    • Press the Windows key, type Notepad, right-click on Notepad, and select Run as administrator.
  2. Open the Hosts File:

    • In Notepad, go to File > Open.
    • Navigate to C:\Windows\System32\drivers\etc
    • Select All Files from the dropdown menu to view the hosts file.
    • Click on hosts and open it.
  3. Edit the File:

    • Add the desired mappings at the end of the file. For example:
      127.0.0.1 example.com
      192.168.1.1 router.local
  4. Save the Changes:

    • Save the file and close Notepad.

On macOS

  1. Open Terminal:

    • Press Command + Space, type Terminal, and press Enter.
  2. Edit the Hosts File:

    • In the Terminal window, type the following command and press Enter:

      sh
      sudo nano /etc/hosts

    • Enter your password when prompted.
  3. Make Changes:

    • Use the arrow keys to navigate to the end of the file.
    • Add the necessary mappings, for example:
       
      127.0.0.1 example.com
      192.168.1.1 router.local
  4. Save and Exit:

    • Press Control + O to save the file, then Control + X to exit.

On Linux

  1. Open Terminal:

    • Access the terminal via your applications menu or by pressing Ctrl + Alt + T.
  2. Edit the Hosts File:

    • In the terminal, type the following command and press Enter:

      sh
      sudo nano /etc/hosts

    • Enter your password if prompted.
  3. Modify the File:

    • Navigate to the end of the file using the arrow keys.
    • Add your desired mappings, such as:
       
      127.0.0.1 example.com
      192.168.1.1 router.local
  4. Save and Exit:

    • Press Ctrl + O to save, then Ctrl + X to exit.

Tips and Considerations

  • Backup the Original File: Before making changes, it's a good idea to create a backup of the original hosts file.
  • Use Correct Syntax: Ensure that each entry follows the correct format: IP_address hostname.
  • Flushing DNS Cache: After editing the hosts file, you might need to flush your DNS cache for changes to take effect.

    • On Windows, open Command Prompt as an administrator and run:

      sh
      ipconfig /flushdns

    • On macOS, use the Terminal:

      sh
      sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

    • On Linux, restart the network service (commands may vary based on the distribution).
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

Disable OpenDNS resolver on Windows Server Operating Systems

An open DNS resolver is a DNS server that responds to recursive DNS queries from any IP address...

Disable OpenDNS resolver on Windows Server Operating Systems

An open DNS resolver is a DNS server that responds to recursive DNS queries from any IP address...

Disable OpenDNS resolver on Windows Server Operating Systems

An open DNS resolver is a DNS server that responds to recursive DNS queries from any IP address...

Enable GUI on Ubuntu Server (18.04/20.04)

In this article, we take a look at how to install GUI on Ubuntu server 18.04 and 20.04. Because...

Enable GUI on Ubuntu Server (18.04/20.04)

In this article, we take a look at how to install GUI on Ubuntu server 18.04 and 20.04. Because...