How to Edit the Hosts File on Windows, Linux, and macOS

How to Edit the Hosts File on Windows, Linux, and macOS

The hosts file is a text file present on all operating systems that allows you to associate domain names with IP addresses locally, without relying on DNS servers. Your computer checks this file before making any DNS query, so the entries you add here take priority.

What is the hosts file used for?

  • Preview a website before changing DNS: If you are migrating your site to a new server, you can point the domain to the new IP only on your computer to verify that everything works correctly before making the public change.
  • Development and testing: Developers use the hosts file to point domains to local or test servers.
  • Block websites: You can redirect a domain to 127.0.0.1 to prevent access to certain sites.

What format does a hosts file entry have?

Each line in the hosts file follows this format:

  1. IP_ADDRESS domain-name.com

For example, to point the domain mydomain.com to the IP 185.50.100.200:

  1. 185.50.100.200 mydomain.com www.mydomain.com


Edit the Hosts File on Windows

The hosts file on Windows is located at:

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

To edit it:

  1. Search for Notepad in the Start menu.
  2. Right-click on it and select "Run as administrator".
  3. Go to File → Open.
  4. Navigate to the path C:\Windows\System32\drivers\etc.
  5. In the file type filter, change to "All Files (*.*)" and select the hosts file.
  6. Add the lines you need at the end of the file using the format indicated above.
  7. Save the changes with Ctrl + S.

 

Edit the Hosts File on Linux

The hosts file on Linux is located at:

  1. /etc/hosts

To edit it:

  1. Open a terminal with the shortcut Ctrl + Alt + T.
  2. Run the following command:
  1. sudo nano /etc/hosts
  1. Enter your password if the system requests it.
  2. Add the necessary lines at the end of the file.
  3. Save the changes with Ctrl + O and press Enter.
  4. Exit the editor with Ctrl + X.

 

Edit the Hosts File on macOS

The hosts file on macOS is located at the same path as on Linux:

  1. /etc/hosts

To edit it:

  1. Open Terminal from Applications → Utilities → Terminal.
  2. Run the following command:
  1. sudo nano /etc/hosts
  1. Enter your password if the system requests it.
  2. Add the necessary lines at the end of the file.
  3. Save the changes with Ctrl + O and press Enter.
  4. Exit the editor with Ctrl + X.

 

Flush the DNS Cache After Making Changes

After saving the changes to the hosts file, it is recommended to flush the DNS cache so that the changes take effect immediately:

Windows (open Command Prompt as administrator):

  1. ipconfig /flushdns

macOS:

  1. sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Linux:

  1. sudo systemd-resolve --flush-caches

 

Alert
Remember to remove the lines you added to the hosts file once you have finished your testing. If you leave them, your computer will continue pointing those domains to the IPs you configured, even if the public DNS already points to a different server.

 

The hosts file is a very useful tool for developers and system administrators. It allows you to test migrations, preview websites on new servers, and perform network diagnostics without affecting the public configuration of your domains.

If you have any questions, we are available to help you through our chat system.


    • Related Articles

    • How to Change DNS Servers on Windows and macOS – Step-by-Step Guide

      DNS (Domain Name System) servers are responsible for translating website names (like www.webzi.mx) into IP addresses that computers can understand. Your internet provider assigns DNS servers automatically, but you can change them manually to get ...
    • How to fix WordPress memory limit errors

      Sometimes when installing or using themes and plugins in WordPress, you might run into errors related to the memory limit assigned to PHP. Some common messages include: Allowed memory size of xxx bytes exhausted... Fatal error: Out of memory... These ...
    • What Are Inodes and Why Do They Matter in Your Hosting?

      An inode is an identifier for each file or folder in your hosting account. It doesn't matter if the file is large or small: every file, image, email, or folder counts as an inode. If you reach the inode limit of your plan, you won't be able to upload ...
    • How to Connect Your Domain to Shopify

      If you have an online store on Shopify and want to use your own domain (for example yourdomain.com), you need to configure two DNS records in cPanel so your domain points to Shopify's servers. ℹ️ Note: This guide applies when your domain is ...
    • What Is WP Accelerate and How to Configure It?

      WP Accelerate is a plugin designed for WordPress that includes a set of optimizations designed to improve the performance of WordPress sites hosted in environments with cPanel and CloudLinux. It integrates various techniques and tools that speed up ...