How to Edit Hosts File in Linux, Windows, or Mac?


Introduction

Have you ever wondered how your computer connects to a website? The answer lies in the Domain Name System (DNS), which translates domain names into IP addresses.

But did you know that you can actually edit this process by modifying your computer's hosts file? The hosts file is a simple text file that maps IP addresses to domain names, allowing you to bypass DNS and specify exactly where your computer should look for a website.

Editing Hosts File in Linux

Where is the hosts file located in Linux and how to access it?

The hosts file in Linux is located at /etc/hosts. To edit this file, you need administrative privileges.

You can access it using a text editor or a command-line interface. The most common text editors used are Nano, Vi, or Gedit, while the terminal application is preferred for its efficiency.

Step-by-step instructions on how to edit the hosts file using a text editor or command line interface

To edit the hosts file using a text editor, open up your terminal application and use the following command −

sudo nano /etc/hosts       

This will open up the hosts file in the Nano editor with administrative privileges.

From here, you can make any necessary changes to the file. To edit the hosts file using a command-line interface, use this command −

sudo echo "IP_address domain.com" >> /etc/hosts        

This adds an entry into your hosts file with an IP address and domain name.

Examples of common use cases for editing the hosts file in Linux

One common use case for editing the hosts file in Linux is to map local network resources. For instance, you may want to assign static IP addresses for devices on your local network through your router and map them via hostname instead of typing out each IP address when accessing them via their web interface or SSH connection. Another example would be blocking ad servers on websites by mapping their hostnames back to localhost (127.0.0.1).

This will effectively block all traffic from that server as it loops back into itself without loading any content. Overall, whether you are accessing resources within your internal network or configuring custom domain names for development/testing purposes, understanding how to manage and modify files such as the hosts file is a critical skill to have.

Editing Hosts File in Windows

Windows has a hosts file located in the same directory as Linux, but with a different path. On Windows, the hosts file is typically located at C:\Windows\System32\drivers\etc\hosts.

Accessing the Hosts File

To access the hosts file in Windows, you must first open your text editor or command prompt with administrative privileges. This is necessary to modify system files such as the hosts file.

To access the hosts file using Notepad −

  • Click on Start and type "Notepad" in the search bar.

  • Right-click on "Notepad" and select "Run as administrator."

  • In Notepad, click on "File" and select "Open."

  • Navigate to C:\Windows\System32\drivers\etc.

  • Select "All Files" from the dropdown menu next to "File name."

  • Select the "hosts" file.

To access the hosts file using Command Prompt −

  • Click on Start and type "Command Prompt" in the search bar.

  • Right-click on "Command Prompt" and select "Run as administrator."

  • In Command Prompt, type−

notepad C:\Windows\System32\drivers\etc\hosts       

Editing the Hosts File

The format of editing a host's entry is similar across all operating systems. Each entry consists of an IP address followed by one or more domain names separated by spaces or tabs. Comments can be inserted using # sign in front of each line.

To edit the hosts file in Windows −

  • Open Notepad or Command Prompt with administrative privileges as instructed above.

  • Navigate to C:\Windows\System32\drivers\etc\hosts.

  • Add or modify entries as desired using the format

  • Save the hosts file.

Common Use Cases for Editing the Hosts File in Windows

The most common use case for editing the hosts file in Windows is for testing website changes before making them live. By modifying the hosts file to point a domain name to a local IP address, you can view changes made to a website on your local machine before pushing those changes live to the public site. Another common use case is redirecting unwanted domains to localhost, which can be useful for blocking ads or malicious websites.

Editing Hosts File in Mac

The Location of the Hosts File in Mac

The hosts file in Mac is located at /private/etc/hosts. It is a plain text file that lists the IP addresses and domain names that resolve to those addresses. To access the hosts file, you will need administrative privileges on your Mac.

Editing Hosts File Using Terminal Application

To edit the hosts file using the Terminal application, open Terminal by going to Applications > Utilities > Terminal or by searching for it in Spotlight. Once in Terminal, type "sudo nano /private/etc/hosts" and press Enter.

This command will open the hosts file in Nano, a built-in text editor. You will need to enter your administrator password to make changes to the file.

Once you have opened the hosts file in Nano, you can add or modify entries by typing them directly into the file. Each entry should be on its own line and follow this format: IP Address Domain Name

For example: 127.0.0.1 localhost

After making any changes, save your edits by pressing

Control+O (that's “O”, not zero) followed by Control+X to exit Nano.

Examples of Common Use Cases for Editing Hosts File in Mac

There are several reasons why you might want to edit your hosts file on a Mac. Here are some common use cases −

  • Blocking access to certain websites or online services

  • Redirecting traffic from one domain name to another - Testing website changes before making them live

  • Creating custom local domains for development purposes Overall, learning how to edit your hosts file can be a valuable skill for anyone who uses a computer regularly

  • especially developers and network administrators who need more control over DNS resolution on their machines!

Advanced Tips and Tricks

Create Aliases for Hostnames

One useful trick for more advanced users is to create aliases for frequently used hostnames in the hosts file. This can save time and make it easier to remember frequently accessed websites or services. To do this, simply add a new line to the hosts file with the IP address followed by a space, then the hostname alias.

For example: "127.0.0.1 localhost myalias". This will allow you to access the same website using both the original hostname and your new alias.

Block Websites with Wildcard Entries

Another advanced technique is to use wildcard entries in the hosts file to block entire domains or subdomains at once. For example, adding "127.0.0.1 *.example.com" will block all subdomains of example.com from being accessed on your computer.

Conclusion

Learning how to edit the hosts file in Linux, Windows, or Mac can be a powerful tool for managing network connections and accessing specific websites or services more easily. While it may seem daunting at first, following our step-by-step instructions and using these tips and tricks can help even beginners become proficient in editing their hosts files confidently and effectively. So don't be afraid to give it a try - you might be surprised at how much control over your network connections you gain!

Updated on: 11-Jul-2023

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements