Using and Editing the Windows hosts File

Techelopment - Nov 4 - - Dev Community

The Windows hosts file is a system file used to map IP addresses to domain names. This file, present in every operating system, allows a user to resolve the IP address of a site or local server without relying on DNS servers (what is DNS?). It is an essential tool for IT professionals, web developers, and anyone who wants to control the behavior of domain name resolution locally. In this article, we will see how to edit and use the hosts file in Windows.

🔗 Do you like Techelopment? Check out the site for all the details!

What the hosts file is

The hosts file is a simple text file located in the C:\Windows\System32\drivers\etc folder. In it, you can manually specify a list of IP addresses and their corresponding domain names, thus overriding the system DNS settings. The structure of this file is very simple and is based on lines of text that specify the IP address and domain separated by spaces or tabs.

Example of a line in the hosts file:

127.0.0.1   localhost
192.168.1.100   test.local
Enter fullscreen mode Exit fullscreen mode

In this example:

  • 127.0.0.1 localhost represents the loopback address, used to indicate the local machine
  • 192.168.1.100 test.local indicates that the domain test.local should be resolved to the IP address 192.168.1.100

Why use the hosts file?

The hosts file is useful for several reasons, including:

  • Testing websites in development: It is useful for developers and testers to temporarily associate a domain name with a test server
  • Blocking unwanted sites: Assigning the IP address 127.0.0.1 to a site (such as an unwanted advertising domain) will block the site
  • Fast name resolution: In network environments, it can be useful to quickly resolve domain names without depending on a DNS server
  • Temporary DNS override: To temporarily bypass the company or ISP DNS

How to edit the hosts file in Windows

Editing the hosts file requires Administrator privileges. Here are the steps to edit it:

1-Open Notepad as Administrator

  • Click the Windows Start button
  • Type "Notepad" or "Notepad" in the search bar
  • Click "Run as Administrator"

Open Notepad as Administrator

2-Open the hosts file

  • Once you have opened Notepad as an administrator, click File > Open
  • Navigate to the path C:\Windows\System32\drivers\etc
  • Select all file types in the drop-down menu
  • Open the file called hosts

3-Add, edit or delete lines

  • Add a new line for each IP address and domain that you want to map (aka mapping)
  • For example, adding the line 127.0.0.1 spamsite.com will block access to the site spamsite.com

4-Save the changes

  • Once you have made the changes, click File > Save
  • Close Notepad

Common problems when managing or editing the hosts file

Here are the most common problems you may encounter:

  • Access right errors: If Notepad is not run as an administrator, you will not be able to save your changes
  • DNS cache: After you edit the hosts file, Windows may have cached your old DNS settings. To fix this, open a command prompt as an administrator and type ipconfig /flushdns to clear the cache
  • Correct syntax: Each entry in the hosts file must have the correct formatting: IP address space/tab domain otherwise it will be ignored
  • Extensions or antivirus software: Some antivirus and security programs may block changes to the hosts file as a protection measure. This can also happen with corporate PCs or those managed by an internal network

What if I want to remove changes without having to rewrite them?

If you want to temporarily remove a change without having to delete it and then rewrite it, you can use comments.

The hosts file allows you to comment out its lines, which means that a mapping has no effect, without removing it. To do this, simply add the # character before the line you want to ignore:

127.0.0.1   localhost
#192.168.1.100   test.local disabled mapping
Enter fullscreen mode Exit fullscreen mode

Follow me #techelopment

Official site: www.techelopment.it
Medium: @techelopment
Dev.to: Techelopment
facebook: Techelopment
instagram: @techelopment
X: techelopment
telegram: @techelopment_channel
youtube: @techelopment
whatsapp: Techelopment

. . . . . . . . . . . . . . . . . . . . . . . . . .