How to Edit Your Hosts Files: Mac, Windows, Linux 2024

Why Trust Techopedia

The hosts file, a fundamental component of computer networks, acts as an internal address book, mapping domain names to specific IP addresses. Originating from the early days of networked computing, it played a pivotal role in directing network traffic before the ubiquity of the Domain Name System (DNS)

Today, while DNS servers largely manage this task, the hosts file remains a big part of most operating systems, offering users the flexibility to customize their network interactions.

What is a Hosts File?

A hosts file is like the address books of your computer. Just as you might look up a friend’s name in your address book to find their phone number, your computer refers to the hosts file to map domain names to IP addresses

Originally, the hosts file was the primary method for networked computers to find each other. Today, the DNS largely takes on this role, but the hosts file is still present and can be useful.

When you enter a web address into your browser, your computer first checks the hosts file to see if there are any specific instructions for where to route that request. If the domain isn’t listed in the hosts file, the computer then consults DNS servers.

Editing the hosts file can let you do things like block access to certain websites or test how a site performs from a specific IP address. 

How to Edit Your Hosts Files?

By editing the hosts file, you can block specific websites, redirect domain requests, or even simulate web developments locally. However, editing the hosts file without the necessary precautions and understanding can disrupt the computer’s network activities.

Prerequisites and Considerations

Editing the hosts file is a sensitive operation that requires careful consideration. First, you’ll need administrative access, as this system file governs your computer’s network connections. If you’re on a personal device, you probably have this, but double-check if you’re on a shared or work computer.

Also, make sure to back up the original hosts file before making any changes to it. Copy it and store it in a different location, such as a USB flash drive or network-attached storage device. This is so that you can restore the file if any issues arise.

Alterations to the hosts file can profoundly impact how your computer interacts with the network. You could block vital sites, redirect network requests, or change how websites are accessed. While these tweaks can be beneficial, they can also unintentionally disrupt your system.

Editing the Hosts File on Windows

You can edit the hosts file on your Windows PC in a relatively straightforward way. You just need a plain text editor such as VSCode or even Notepad, and of course, proper admin rights.

We’ll use Notepad in this example, but the process is the same regardless of the plain text editor that you use.

Steps to Locate the File

1. Search Notepad, right-click the app in the search results, and then select Run as administrator from the menu. If you use a different text editor, be sure to run it as an administrator.

How to Edit the Hosts File on Windows 2. In Notepad, click File > Open, and then navigate to the following file path: C:\Windows\System32\drivers\etc

Make sure to switch the file type from “Text documents (*.txt)” to All files. The hosts file isn’t really a .txt document, so it won’t appear unless you switch the file type.

Edit hosts file on Windows

3. The hosts file will now open In Notepad, and you can make your additions or edits. An entry generally consists of an IP address followed by a space and then the domain name (e.g., 192.0.2.1 www.example.com). 

4. Once you’re done, save the file and then close Notepad.

5. You can clear the DNS cache so that your changes to place immediately. In the Command Prompt, run the ipconfig/flushdns command.

How to Edit Hosts Files on Mac

The hosts file is also a piece of the MacOS architecture. As with Windows, you’ll need proper admin rights to edit the file.

Steps to Locate the File

We’ll need to use a text editor to update the hosts file.

  1. Open the terminal and execute the following command:

       sudo nano /etc/hosts

       You’ll also be prompted to input your password.

Open nano on Mac

2. You can now edit the hosts file. For example, if you wanted to block X (formerly Twitter), you could enter 0.0.0.0  www.x.com.

Edit the host file on Mac

 3. Don’t forget to save the file to reflect the changes.

 4. Clear your DNS cache so that the system uses the updated hosts file immediately. To do this, run the sudo killall -HUPmDNSResponder command in the terminal.

Editing Hosts Files on Linux

Just like other operating systems, Ubuntu (along with most other Linux distributions) has a hosts file that aids in network hostname resolution.

Steps to Locate the File

To edit the hosts file, you’ll need to use a text editor. We’ll use nano, but there are other popular text editors for Linux that you can use, such as Vim.

1. Open the terminal and run this command:

    sudo nano /etc/hosts

    You’ll be prompted to enter your password, too.

Open nano on Linux

2. You can now edit the hosts file. Using the same example as before, if you wanted to block X, you could enter 0.0.0.0 www.x.com.

Edit the host file on Linux

3. Save the file for your changes to take place. 

4. Ubuntu doesn’t cache DNS queries by default. But if you’re using a service that does, like dnsmasq, you can restart it by running the sudo service dnsmasq restart command.

Common Mistakes to Avoid

Common Mistakes to Avoid When Working With the Hosts File

When working with the hosts file, some errors can easily slip through, especially if you’re new to the process.

Here are some common pitfalls you should try to avoid.

  • Forgetting Backups: Always create a backup of your original hosts file before making any edits so that you can quickly restore it if something goes wrong.
  • Typos and Formatting: Make sure that each entry is correctly formatted with an IP address followed by a space and then the domain name. Even a small typo can cause unexpected behavior.
  • Overlooking Permissions: Remember, you need administrative rights to edit the hosts file.
  • Not Flushing the DNS Cache: Flush the DNS cache after editing to apply the changes immediately. Otherwise, your system might still use old, cached data.
  • Using Outdated IPs: IP addresses for websites can change over time. If redirecting a domain, make sure the IP address you’re using is current and accurate.
  • Overcomplicating Entries: Keep the hosts file clean and simple. Avoid adding unnecessary entries and leave notes (using #) for clarity.

Use Cases and Examples

The hosts file has a few practical applications. Here are some common use cases where you may want to edit the hosts file.

  • Blocking Specific Websites: By adding an entry in the hosts file that points a website domain name to the IP address 127.0.0.1 (the local machine), you effectively block that site. So the entry in the host file would look like: 127.0.0.1 www.example.com
  • Redirecting Domains: You can redirect one domain to another by pointing it to a different IP address. For instance, if you want www.oldsite.com to redirect to the IP of www.newsite.com, find the IP of www.newsite.com and add: IP_of_newsite www.oldsite.com
  • Local Development Testing: Web developers often need to test sites locally before publishing online. By pointing a domain to 127.0.0.1, they can load the local version of a website.

Additional Tools and Software

When managing the hosts file, sometimes the basic text editors might not be enough, especially when dealing with bulk entries or frequent changes. There are some specialized tools and software to streamline the process, but always be sure what you’re using is legit.

Third-Party Editors

There are third-party editors, such as HostsMan for Windows, that offer enhanced features for editing the hosts file, making it easier to manage, organize, and implement changes. These editors often come with user-friendly interfaces and functionalities like sorting, filtering, and easy toggling between entries.

Automation Scripts

If you’re regularly updating your hosts files or maintaining consistency across multiple machines, automation scripts are the way to go. With scripts, you can automatically update, backup, or modify entries based on predefined criteria or schedules. Tools like Ansible or simple bash scripts can be used for these automation tasks.

The Bottom Line

The hosts file serves as an internal directory, linking domain names to specific IP addresses. While today’s DNS servers majorly handle domain to IP mappings, the hosts file still offers a level of customization, allowing you to block or redirect specific websites and test web developments locally. 

Editing this file requires caution. Users must have administrative access, always backup the original file, and completely understand the potential implications of any changes. Editing can be done on Windows, Mac, or Linux systems, each with its own set of steps.

FAQs

What is the hosts file, and why would I need to edit it?

Do I need administrative rights to edit the hosts file?

Can editing the hosts file harm my computer or network?

How can I revert the changes if something goes wrong after editing the hosts file?

Related How To’s

Related Terms

Marshall Gunnell
IT & Cybersecurity Expert
Marshall Gunnell
IT & Cybersecurity Expert

Marshall, a Mississippi native, is a dedicated expert in IT and cybersecurity with over a decade of experience. Along Techopedia, his bylines can be found on Business Insider, PCWorld, VGKAMI, How-To Geek, and Zapier. His articles have reached a massive readership of over 100 million people. Marshall previously served as the Chief Marketing Officer (CMO) and technical staff writer at StorageReview, providing comprehensive news coverage and detailed product reviews on storage arrays, hard drives, SSDs, and more. He also developed sales strategies based on regional and global market research to identify and create new project initiatives.  Currently, Marshall resides in…