How to edit the hosts file in Windows, Linux, and macOS step by step

Last update: 23/05/2025
Author Isaac

hosts file

Talking about the hosts file means delving into one of the oldest and most practical resources available on any computer, whether it runs Windows , Linux , or macOS. Although its use has been relegated to specific tasks due to advancements in the DNS system, it remains a fundamental tool for advanced users, developers, and system administrators . Over the years, its function has evolved, but its usefulness remains intact for those who need to force domain name resolution to specific IP addresses, perform tests before DNS changes, or simply block and redirect websites locally and easily.

If you've ever wondered how to point a domain to a specific IP address without relying on public DNS servers, or if you need to test your website's functionality before changing a domain's DNS settings, you'll be surprised how easy it is to modify this small file. In this article, I'll explain in detail, with a completely up-to-date approach, everything you need to know about the hosts file, how to edit it on any operating system, and in what situations it can get you out of a tight spot.

What is the hosts file and what is it used for?

The hosts file is one of those system files that often goes unnoticed but performs very powerful functions. It's a plain text file present in virtually every modern operating system , whose function is to manually associate domain names with IP addresses, acting as a local address book. When your browser tries to resolve a domain, the first thing your operating system does is consult this file before resorting to an external DNS server.

The first common use case for hosts is to force the resolution of one or more domains to a specific IP address . In other words, you can tell your computer to point to the IP address 123.123.123.123 even if the global DNS says otherwise. This small local trick only affects your computer, but it's sufficient for tasks such as:

  • Perform web development tests in a local environment before publishing any changes to the internet.
  • Check website migration between servers before DNS propagation.
  • Redirect domains temporarily without touching the official configuration.
  • Restrict, allow, or block access to certain websites directly on your computer without the need for extra software.

By modifying this file, the resolution will always be internal and prioritized for that domain , so as long as that entry exists, any access from your browser or applications will use the IP you specify, completely ignoring public DNS information.

  The story behind Windows Solitaire: from intern to myth

Main uses and precautions of the hosts file

There are numerous and varied situations where modifying the hosts file can be useful. For example, if you want to access your website from your own computer before DNS propagation is complete , or if you urgently need to check the functionality of a new website after a server migration.

  • Previewing sites before delegating DNS: Ideal for avoiding surprises after migrations or changes in hosting providers.
  • Selective subdomain redirection during development processes.
  • Domain blocking unwanted or malicious, associating them with a non-existent IP or localhost (127.0.0.1).
  • Simulating connections in test environments without having to alter the production environment.

However, there are potential risks if the hosts file is modified inadvertently or maliciously. Certain viruses or malware can manipulate this file to redirect you to fraudulent sites or prevent access to legitimate pages. For this reason, it is crucial to know how to review its contents and restore its original state in case of unusual behavior.

When modifying the file, pay attention:

  • Avoid syntax errors: Each entry must be on a single line with the IP followed by the domain, separated by spaces or tabs.
  • Do not delete lines from the system (for example, those on localhost).
  • Ensures read-only permissions after editing it to prevent unauthorized modifications.

Where is the hosts file located on each operating system?

The location of the hosts file varies depending on the operating system , although its function is identical. Below are the common paths where you can find it.

  • Windows XP, Vista, 7, 8, 10 and 11: C: \ Windows \ System32 \ drivers \ etc \ hosts
  • Windows NT/2000: C:\WINNT\System32\drivers\etc\hosts
  • Windows 95/98/Me: C:\Windows\hosts
  • Linux: / Etc / hosts
  • macOS: /private/etc/hosts

By default, the hosts file is protected and does not have a .txt extension . If you try to open it without administrator privileges or without selecting "all files" when opening it from the editor, you may not see it listed.

How to edit the hosts file in Windows

Modifying the hosts file in Windows is easy if you understand a few basic concepts. It's important to run Notepad with administrator privileges , as the file is protected by the system.

  • Click on the menu Home and search for “Notepad”.
  • Right click on the icon and select Run as administrator. (In Windows 8 and later, use search apps if you don't see it directly).
  • In Notepad, navigate to File> Open and put the following route: C: \ Windows \ System32 \ drivers \ etc.
  • Choose "All the files” (not just .txt files), select the file hosts and click open.
  • Add new lines or edit existing ones as needed: first the IP address, then the domain, separated by a space or tab. For example:
    192.168.1.100 www.mydomain.com
  • Save the file normally. If you don't have permissions, check that you've opened Notepad as an administrator.
  OneDrive reports an error with the cloud file provider

Always remember to save a backup copy before modifying the file in case you need to restore it.

Edit the hosts file on macOS

On macOS systems , the procedure for editing the hosts file is almost identical to that of Linux, since both are Unix -based and depend on the use of the Terminal.

  • Open the application Bus Terminal from “Applications > Utilities” or from the Launchpad.
  • Enter the following command and press Enter:
    sudo nano / private / etc / hosts file
  • Enter your administrator password if requested.
  • Edit the file by adding the IP and domain you want, for example:
    10.0.0.1 test.mydomain.com
  • Save changes by clicking Control + OR and nano salt with Control+X.

If after saving the changes the system does not reflect the new resolution, it may be necessary to clear the DNS cache by running:
dscacheutil -flushcache

How to edit the hosts file in Linux

In Linux , the hosts file is always located in "/etc/hosts" and you only need administrator privileges to modify it. This procedure is valid for distributions such as Ubuntu, Debian, Fedora, CentOS, and others.

  • Open Terminal (Ctrl+Alt+T or search for “Terminal” in the applications menu).
  • Run the following command:
    sudo nano / etc / hosts
  • Enter your password if prompted.
  • Edit the file with the usual format, for example:
    123.45.67.89 miwebtest.com www.miwebtest.com
  • Save with Ctrl + A and go out with Ctrl + X if you use nano, or use the text editor of your choice (vim, gedit, etc.).

In some cases, it may be a good idea to restart your browser or clear your system's DNS cache to see the effect of your changes.

Practical examples of host entry and recommendations

The hosts file is organized into individual lines, each containing an IP address and one or more associated domain names. It's essential to leave at least one space or tab between the IP address and the domain name . You can also add comments by preceding them with the # symbol.

Examples of typical entries:

  • 127.0.0.1 localhost
  • 192.168.1.50 tests.mydomain.com
  • 64.49.219.194 www.nuevodominio.com
  • # Redirect www and non-www
    123.45.67.89 mydomain.com
    123.45.67.89 www.mydomain.com

General recommendations:

  • Keep the file protected after editing it to prevent malicious software from modifying it.
  • Make a backup before making changes.
  • Avoid syntax errors and check that there are no unnecessary spaces or misspelled records.

How to protect and restore the hosts file if it has been altered

Some viruses or malware can modify the hosts file to redirect traffic to fraudulent sites or block legitimate access. If you notice strange behavior while browsing or have problems accessing certain websites, review the contents of the hosts file to make sure there are no suspicious entries.

  • Open the file in the editor for your operating system and delete any lines you didn't intentionally add.
  • Restore the original lines if you have a backup or check the internet for your system's default entries.
  • You can strengthen the security of the file by making it read-only.
    In Windows: Right-click the file, select “Properties” and select “Read-only.”
    On Linux/macOS: Use the command chmod 444 /etc/hosts or equivalent.
  • Run a full scan with your antivirus to make sure your computer is clean.
  SteamOS 3.7.15: All the new features and why they matter

Common troubleshooting and post-editing tips

Making changes to the hosts file is usually not a problem, but if editing it doesn't make the changes effective, try the following:

  • Close and reopen the browser in which you are testing access.
  • Sometimes it is necessary flush the DNS cache of the system:
  • On Windows: run the command ipconfig / flushdns from a console CMD.
  • On macOS: As stated before, run dscacheutil -flushcache in Terminal.
  • On Linux: Restarting the network service or the computer itself is usually sufficient.

If you need to discard any modifications, remove the added lines or restore the original file. Leave only the system entries (for example, localhost on 127.0.0.1).

The hosts file is a very useful tool that you should master whether you're an advanced user, a web developer, or simply interested in having more control over your computer's browsing. Mastering how it works can save you trouble, give you a boost of security, and make your work easier when you need flexibility in domain name resolution. Acting cautiously, documenting changes, and backing up the file before any intervention are recommended practices to get the most out of it. With this knowledge, you can take full advantage of one of your operating system's oldest and most versatile features.

Isaac

Isaac

Passionate writer about the world of bytes and technology in general. I love sharing my knowledge through writing, and that's what I'll do on this blog, show you all the most interesting things about gadgets, software, hardware, tech trends, and more. My goal is to help you navigate the digital world in a simple and entertaining way.