HOSTS File Editor

by Dmitry Kirsanov 4. September 2019 10:57

HOSTS file is a text file in your Windows system that contains name resolution pairs to quickly resolve domain and LAN computer names, or ensure that resolution fails and host in question would become unreachable. We use it to speed up access to intranet hosts, block advertisement and telemetry websites, and in software development – to substitute real world hosts with local copies.

I have to edit HOSTS file often and on different computers. Supply IP addresses for local servers, block connection to Facebook, disable adverts in some apps, that kind of stuff. Well edited HOSTS file makes Windows work faster and helps avoiding problems with connectivity.

One problem, though – it’s over 60Kb large in my case, and adding / removing / temporarily disabling records - takes time. Not much, but more than I’d like to spend on that task. Therefore I felt the need for a command line editor – a single-command application, that would allow to perform the full range of operations on that file and make whole editing process a fraction of a second. And here it is.

Synopsis

It’s a command line application, meaning you can use it from command line / batch files / scripts / remote access, and it will work even on systems with no GUI. You can also ship this application together with the list of hosts, so it could import the list and perform a batch operation.

Syntax

hosts.exe [host name] [ip] [/parameter]

Command line parameters:

/add - Add item to HOSTS file, if it's not there yet, but don't overwrite existing value.
/remove - Remove this host record from HOSTS file.
/disable - Disable HOSTS file (rename to hosts.removed).
/enable - Restores the backup created with /disable command.
/null - Use 0.0.0.0 as an IP address, preventing name resolution.
/real - Resolve actual address and put it into the file, to speed up future name resolution. 
/ipv6 – Prefer IPV6 address. When used with /null, will produce IPV6 null address.
/comment - Comment out string with this host, if it's in the file.
/uncomment - Uncomment commented host record, if it's in the file.
/import:[file name] - import portion of hosts file into your hosts file. Duplicates will be removed.
/ilc:[In-line comment] - In-line comment to add to this record, only for one host commands, not batch imports.
/partial - Used with /remove. Remove all hosts with this string in their names.
/bak:[file name] - Used with /remove. Write removed lines to separate file. File must have TXT extension, otherwise it's added.
/baka - Do not create automatic HOSTS file backup. (Backup Absent).

/v - Verbose mode. Print informational messages.
/q - Quiet mode. All messages will be printed to Event Log.
/log - Prepare Event Log. This should be normally used just once, using Run As Administrator.

Examples:
hosts myserver.local 127.0.0.1
hosts badserver.com /null /ilc:"Bad server"
hosts /import:facebook.txt /null
hosts /import:facebook.txt /import:linkedin.txt /import:trackers.txt /null
hosts lan_file_server.local /real

Scenarios

1. Block Facebook access from local machine

Let’s say, we have a list of all sites accessed by Facebook, and we want to import it into the HOSTS file, so that all requests would fail. No more spying scripts on pages, no more delays in page load. Once we have the said text file (see below under Downloads), we need to execute this command:

hosts /import:facebook.txt /null

This will read each domain name from the text file and put the null IP address (0.0.0.0) for it. In fact, said text file already contains the IP addresses, so you could just copy-paste it into your HOSTS file, but it could contain just the host names, and the IP would be substituted with null one.

Many people used to replace IP address with 127.0.0.1. Important difference between 0.0.0.0 and 127.0.0.1 is that first one is “non-existent”, and second is “local”. When application receives non-existent IP address, it doesn’t attempt to connect to the host, the connection fails immediately. But if your IP address is 127.0.0.1, the connection is attempted at your local address, and it will take time. So, if you want to speed up the process, use zeroes.

You can also use multiple import files in one command.

2. Resolve local host for quicker access

You may have a local host with static IP address, which takes time to resolve. Even milliseconds are quite irritating when we are talking about local NAS (Network Area Storage, or simply put – network hard drive). This command would resolve the name to it’s actual IP address and store it for future in HOSTS file:

hosts MyFileServer /real

3. Temporarily disable HOSTS file

If you have some items blocked by your HOSTS but would like to temporarily disable the HOSTS file, this is the command:

hosts /disable

After you finish, you can run the enabling command:

hosts /enable

4. Disable / enable particular entries

Let’s imagine, that you have entry that needs to be disabled, temporarily or not.

hosts telemetry.microsoft.com /comment

This will find entry for telemetry.microsoft.com and comment it out, meaning that website will become available until you uncomment it again with command:

hosts telemetry.microsoft.com /uncomment

A few notes

By default, the hosts application creates a backup of your HOSTS file every time you make a change. It tells you where it saves the backup after each execution. If you’d like to disable it, add “/baka” (stands for Backup Absent) parameter.

The “/bak:[filename]” parameter is different kind of backup. It writes a log of what items were removed from HOSTS file, so you could copy-paste them back later. It’s a failsafe mechanism that may be turned on with command line parameter. Note, that resulting file has TXT file extension, it’s a failsafe measure too.

Application requires administrative privilege, in order to access HOSTS file for writing. However, actions may be blocked by antivirus, such as Kaspersky. In that case you can mark this app as trusted in Kaspersky - go to Settings, Threats and Exclusions, Specify trusted applications, add path to hosts.exe, and then mark the "Do not monitor application activity" checkbox.

Downloads

The main application file (See HostsFileEditor.zip)

Additional text files for import (given for reference):

Facebook, Skype advertisement, Microsoft telemetry

HostsImportFiles.rar (7.26 kb)

Disclaimer:

As of 04/09/2019, this application is new, and so bugs are possible, if not expected. In either case it shouldn’t ruin your HOSTS file, but I would suggest to use my backup utility before initial use of this tool, or at least keep a backup copy somewhere.

If you find a bug or a problem, don’t hesitate to let me know (at least in comments to this post), and I’ll fix it asap. The updated version will be posted on this page.

blog comments powered by Disqus