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:
- IP_ADDRESS domain-name.com
For example, to point the domain mydomain.com to the IP 185.50.100.200:
- 185.50.100.200 mydomain.com www.mydomain.com
Edit the Hosts File on Windows
The hosts file on Windows is located at:
- C:\Windows\System32\drivers\etc\hosts
To edit it:
- Search for Notepad in the Start menu.
- Right-click on it and select "Run as administrator".
- Go to File → Open.
- Navigate to the path
C:\Windows\System32\drivers\etc. - In the file type filter, change to "All Files (*.*)" and select the hosts file.
- Add the lines you need at the end of the file using the format indicated above.
- Save the changes with Ctrl + S.
Edit the Hosts File on Linux
The hosts file on Linux is located at:
To edit it:
- Open a terminal with the shortcut Ctrl + Alt + T.
- Run the following command:
- Enter your password if the system requests it.
- Add the necessary lines at the end of the file.
- Save the changes with Ctrl + O and press Enter.
- 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:
To edit it:
- Open Terminal from Applications → Utilities → Terminal.
- Run the following command:
- Enter your password if the system requests it.
- Add the necessary lines at the end of the file.
- Save the changes with Ctrl + O and press Enter.
- 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):
macOS:
- sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Linux:
- sudo systemd-resolve --flush-caches
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.