Oct 10, 2023 3 min read

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

Edit Your Hosts File on Linux, Windows, and macOS with our step-by-step tutorial. Domain names map to IP addresses in the hosts file.

Edit Your Hosts File on Linux, Windows, and macOS
Table of Contents

Introduction

Before we begin talking about how to edit your Hosts File on Linux, Windows, and macOS, let's briefly understand – What is Host File ?

Domain names map to IP addresses in the hosts file. It works as a local DNS resolver, allowing the computer to bypass the internet's DNS lookup process. By modifying the host file, users can control the IP address associated with specific domains.

This can be useful for testing websites on different server configurations or for blocking access to certain websites. Understanding how the host file functions is essential for troubleshooting network issues and implementing website restrictions.

In this tutorial, you will edit your Hosts File on Linux, Windows, and macOS. We will also address a few FAQs on how to edit your Hosts File on Linux, Windows, and macOS.

Advantages of Host File

  1. Enhanced web development: Easily test websites by redirecting domain names to different IP addresses.
  2. Improved security: Block access to malicious or unwanted websites by redirecting them to localhost.
  3. Faster website access: Avoid DNS lookup delays by directly mapping frequently accessed websites to their IP addresses.
  4. Network troubleshooting: Resolve network issues by bypassing DNS servers and directly accessing specific IP addresses.
  5. Web content filtering: Restrict access to certain websites by redirecting them to a different IP or blocking their IP address in the host file.

Hosts File Format

The format of entries in the hosts file is as follows:

IPAddress DomainName [DomainAliases]

At least one space or tab should be used to separate the IP address and domain names. Lines beginning with # are comments and should be disregarded.

Simply open the hosts file in your text editor to add a new entry. A sample hosts file is shown below:

# Static table lookup for hostnames.
# See hosts(5) for details.

127.0.1.1 linuxize.desktop linuxize
127.0.0.1 localhost

Except in circumstances when DNS entries are cached by apps, updates to the hosts file take effect instantly.

Simply open the file and delete the lines you inserted to undo the modifications.

Modify Hosts File on Linux

The complete location of the file on Linux is /etc/hosts.

The following instructions apply to any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, and Linux Mint:

  1. Open the hosts file in your terminal window using your chosen text editor:
sudo nano /etc/hosts

Enter your sudo password when prompted.

2. Add your new entries by scrolling down to the bottom of the file.

3. Save the modifications.

Modify Hosts File on Windows

The complete path to the file on Windows is c:\Windows\System32\Drivers\etc\hosts. The steps below apply to both Windows 10 and Windows 8.

  1. In the search field, press the Windows key and type Notepad.
  2. Run as administrator by right-clicking on the Notepad icon.
  3. Click File, then Open in Notepad. Paste c:\Windows\System32\drivers\etc\hosts into the File name field.
  4. Add your new entries by scrolling down to the bottom of the file.
  5. Click File > Save to save your changes.

Modify Hosts File on macOS

The complete location of the file on macOS is /etc/hosts. All macOS versions are supported by the steps below.

  1. Open the hosts file in your terminal window using your chosen text editor:
sudo nano /etc/hosts

Enter your administrative password when prompted.

2. Add your new entries by scrolling down to the bottom of the file.

3. Save the modifications and clear the DNS cache.

dscacheutil -flushcache

FAQs to Edit Your Hosts File on Linux, Windows, and macOS

Can I add multiple entries in the hosts file?

 Yes, you can add multiple entries by following the format: IP_address domain.com. Each entry should be on a new line.

Do changes in the hosts file take immediate effect? 

Yes, changes take effect immediately. However, sometimes you may need to clear your DNS cache or restart applications for the changes to be recognized.

How do I revert changes in the hosts file? 

To revert changes, you can either remove the entries added to the hosts file or restore the original backup copy, if available.

Can I use the hosts file to block ads or unwanted websites? 

Absolutely! By mapping ad domains or unwanted websites to the loopback address (127.0.0.1), you can effectively block them from loading.

What if my computer doesn't allow me to edit the hosts file?

If you encounter permission issues, ensure you are using administrative privileges. On some systems, you may need to modify file permissions to edit the hosts file.

Will editing the hosts file affect other devices on my network? 

No, the changes made in the hosts file are local to the computer only. Other devices on your network will not be impacted.

Do changes to the hosts file impact network performance? 

No, changes in the hosts file do not impact network performance. They only affect the IP address resolution for specific domains on the local machine.

Conclusion

You can change the IP address of a domain name using the hosts file.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.