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 archive hosts is to dive into one of the oldest and at the same time most practical resources that we can find on any computer, whether with Windows, Linux or macOS. Although its use has been relegated to specific tasks due to advances in the DNS system, It remains an essential tool for power users, developers, and system administrators.Over the years, its functionality has evolved, but its usefulness remains intact for those who need to force domain name resolution to specific IP addresses, perform testing before DNS changes, or simply block and redirect sites locally and easily.

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

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

The file hosts It is one of those system files that often go unnoticed but allow you to perform very powerful functions. It is a plain text file present in virtually any modern operating system., whose function is to manually associate domain names with IP addresses, acting as a local web address book. When your browser attempts 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 one or more domains to resolve to a specific IPThat is, you can tell your computer that example.com point to the IP address 123.123.123.123 even though 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 priority 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.

  Filters, Boolean Operators, and Search Ranges in Windows: A Complete Guide

Main uses and precautions of the hosts file

The situations in which modifying the hosts file can be useful are numerous and varied. 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 operation of a new website after a server move.

  • 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 inadvertently or maliciously modified. Certain viruses or malware They can manipulate this file to redirect you to fraudulent sites or prevent you from accessing legitimate pages. For this reason, It is key to know how to review its content and restore its original state in case of strange 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?

La The location of the hosts file varies depending on the operating system., although its operation is identical. Below I detail the usual routes to 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

Default, The hosts file is protected and does not have a .txt extension.If you try to open it without administrator permissions 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 have a clear understanding of certain concepts. It is important to run Notepad with administrator privileges., since 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.
  Below are two of the most popular Xbox controller software for your PC.

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

Edit the hosts file on macOS

In systems MacOS, the procedure for editing the hosts file is almost identical to that of Linux, since both are based on Unix and depend on the use of the Bus 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

En LinuxThe hosts file is always located in "/etc/hosts" and only requires administrator permissions to edit it. This procedure works 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 is essential to leave at least one space or tab between the IP and the domain.. You can also add comments by preceding it 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 install Ruby on Windows step by step

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.

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 DCM.
  • 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.

Leave a comment