# How to edit hosts file



## swejaljsoni (Dec 25, 2017)

Hosts file has File format.
I am opening hosts file as an administrator. After editing it, when I am trying to save it, its saved as hosts file with text format. I am saving this file with "Save as type" All Files.
How can I save this file with File format instead of text format.
I am trying to stop some sites from getting visited. If I save hosts file with text format, will that work ? If no, then how can I save hosts file with File format ?
Please see attached image to check the formats. Somehow I have been able to save one file in File format. I dont remember, how I did that.


----------



## Corday (Mar 3, 2010)

In Hosts Notepad, at the bottom of the file type your IP address followed by something like badsite.com


----------



## jenae (Jun 17, 2008)

Hi, what you need to do is open notepad as admin. Go to search and type:- notepad, right click on notepad and select "run as administrator". From the elevated notepad go to "File", select open, navigate to c:\windows\system32\drivers\etc, (if nothing shows go to file type (bottom right of open box) and select "all files". Double click on hosts file, it opens. Any modifications now made will stay (if saved), it is designed to be edited as a txt file so leave this alone.


----------



## Wizmo (Apr 3, 2011)

*Due to this being an extremely common question, should this not be made a "sticky" thread? :wink:

*


----------



## swejaljsoni (Dec 25, 2017)

I am running notepad and opening file from "c:\windows\system32\drivers\etc" as an Administrator and then making all modifications and save it as "All files".
But still file is saved as text file.
I am confused. Please let me know, what can be wrong ? How to save file as File. If hosts file is saved in text format, will it work (I mean will it pick up changes made)


----------



## jenae (Jun 17, 2008)

Hi, if you followed my instructions (notepad, right click, run as administrator) then yes the file is saved and hosts file is modified. There is no file assoc known as "file", I have no idea where you get this from.


----------



## spunk.funk (May 13, 2010)

Once you save the Hosts files with the changes, you can check to see the changes were made by opening up the host file with Notepad again.


----------



## swejaljsoni (Dec 25, 2017)

Please see the image attached. 
I am opening notepad as an administrator. 
I follow these steps
1) Click on windows icon and Type "notepad" in search
2) right click on notepad
3) click on "run as Administrator"
4) Box opens up asking "do you want to allow this app to make changes to your device". CLick "yes" 
5) Click on "File" and go to "C:/windows/system32/drivers/etc" and click on All files
6) double click on hosts file
7) Add "127.0.0.1 www.roblox.com" at the bottom
8) Click on "Save As" 
9) click on All Files
10) When I check the file saved, its saved as text file instead of the original file type

Something is not going right. PLease check the image attached.
How can I troubleshoot ?
If I want to copy file in this location, how can I do that ? Its not allowing me to copy. I can get Administrator privileges to change text file by right clicking and running as Administrator, but How can I login with Administrator privileges to copy a file in this location ?


----------



## spunk.funk (May 13, 2010)

After editing the Host Files, do _not_ click Save, just close it. Windows will ask if you want to Save it, and click *Yes,* The dialog box closes and the file is saved. Open it again to see the changes.


----------



## SpywareDr (Jun 15, 2013)

Try saving it as:

hosts*.*​
(with a single period after the "s").


----------



## tristar (Aug 12, 2008)

By default the host file is read-only, some security apps add this attrib flag, so you can either remove this *Read Only* flag by right clicking the file and going to properties and then trying to Save it 

OR

make the changes to the host file by navigating to the folder right click the hosts file and *open with Notepad*, File Save As -- Choose My Documents, name the file hosts *without any extension* choose *All files* in the type and Save it. Once done, copy this file and overwrite the one in the etc directory.


----------



## swejaljsoni (Dec 25, 2017)

What happens, if I delete hosts file by mistake ?

Thanks


----------



## Corday (Mar 3, 2010)

Make a hosts.old file and move it to the %WinDir%\System32\Drivers\Etc folder


----------



## SpywareDr (Jun 15, 2013)

Here is the default HOSTS file contents in windows 10: # Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handle within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost​


----------



## spunk.funk (May 13, 2010)

> After editing the Host Files, do _not_ click Save, just close it. Windows will ask if you want to Save it? and click *Yes,*


 This is still the easiest way to do this.


----------



## jenae (Jun 17, 2008)

Hi, spunks right, you are editing the hosts file which is opened in notepad, any changes you make if you close the file we be saved if you click on save when prompted by windows. That's why I said you may have to expand the file type to include "all files" it will only be visible if you do this, it is edited in a txt document (typically notepad) in win 10 once saved, having been opened from "all files" it will not append .txt to the file (it has no extension). If you get caught, you can right click the file and rename, remove the .txt extension..


----------



## tristar (Aug 12, 2008)

The above method won't work if the Read-only flag is set, so need to check and clear that in order for you to save it.


----------



## jenae (Jun 17, 2008)

Hi, hosts is owned by the system that means it can only be read, it's why we open notepad as admin doing this removes the read access denied. It's part of UAC since Vista days.


----------



## tristar (Aug 12, 2008)

Aah... ok, thanks ! Didn't know that..


----------



## swejaljsoni (Dec 25, 2017)

SpywareDr said:


> Try saving it as:
> 
> hosts*.*​
> (with a single period after the "s").


After saving file as hosts with a single period, it saves fine. Why is it like this ? When I save as hosts. , why is it saved with file extension. If I save as hosts, it is saved as text file only. Why is it so ?


----------



## swejaljsoni (Dec 25, 2017)

tristar said:


> The above method won't work if the Read-only flag is set, so need to check and clear that in order for you to save it.


1) How can I remove "Read Only" tag ? Will I be allowed to remove this tag on SYSTEM file ?
2) How can I copy a file in "C:/windows/system32/drivers/etc" location ? When I m trying to copy a file here, I get message, I am not authorized to copy a file in here. How can I get permission to copy file in "../etc" folder ?

Thanks


----------



## swejaljsoni (Dec 25, 2017)

tristar said:


> By default the host file is read-only, some security apps add this attrib flag, so you can either remove this *Read Only* flag by right clicking the file and going to properties and then trying to Save it
> 
> OR
> 
> make the changes to the host file by navigating to the folder right click the hosts file and *open with Notepad*, File Save As -- Choose My Documents, name the file hosts *without any extension* choose *All files* in the type and Save it. Once done, copy this file and overwrite the one in the etc directory.


I am not allowed to copy a file in "../etc" folder location. How can I get this permission ?

Thanks


----------



## swejaljsoni (Dec 25, 2017)

jenae said:


> Hi, spunks right, you are editing the hosts file which is opened in notepad, any changes you make if you close the file we be saved if you click on save when prompted by windows. That's why I said you may have to expand the file type to include "all files" it will only be visible if you do this, it is edited in a txt document (typically notepad) in win 10 once saved, having been opened from "all files" it will not append .txt to the file (it has no extension). If you get caught, you can right click the file and rename, remove the .txt extension..


I am saving this file as "All FIles" But still its saving as text file only. When I save this file as "hosts." (with . in file name), then it saves with file extension.
Why is that ?


----------



## spunk.funk (May 13, 2010)

> After editing the Host Files, do _not_ click Save, just close it. Windows will ask if you want to Save it, and click *Yes,* The dialog box closes and the file is saved. Open it again to see the changes.


This is *STILL* the easiest way to do this, forget everything else. 
The* /etc* file is a System file, you cannot copy any files to it.


----------



## SpywareDr (Jun 15, 2013)

The "." indicates that you want the filename to end right there, with no further "txt" (or whatever) added after the "."


----------



## swejaljsoni (Dec 25, 2017)

spunk.funk said:


> This is *STILL* the easiest way to do this, forget everything else.
> The* /etc* file is a System file, you cannot copy any files to it.


So you mean to say "/etc" is System file. But etc is a folder. So when you call it a file, are you referring to some file OR is that a typo ?
Is not every file and folder under windows folder System file and System folder?


tristar said:


> By default the host file is read-only, some security apps add this attrib flag, so you can either remove this *Read Only* flag by right clicking the file and going to properties and then trying to Save it
> 
> OR
> 
> make the changes to the host file by navigating to the folder right click the hosts file and *open with Notepad*, File Save As -- Choose My Documents, name the file hosts *without any extension* choose *All files* in the type and Save it. Once done, copy this file and overwrite the one in the etc directory.


This option of copying file from "My Documents" folder to the "/etc" folder will not work. Because etc is a system folder and it will not allow any copying in this folder. Is that correct ? I just tried copying files from other folder to "etc" folder, it asks me that I will have to provide Administrator per,mission. When I click on "continue", it allows me to copy file in this location.
What has changed from last time to now. Before it was not allowing me at all and now it is allowing me.


----------



## swejaljsoni (Dec 25, 2017)

Anyways, I have added two sites to hosts file as follows
127.0.0.1 www.roblox.com # kids video game site
127.0.0.1 www.xhamster.com #adult site.

After modifying, I restarted computer. 
But both sites can be connected. Why is that ?
How can I stop sites from loading ?


----------



## swejaljsoni (Dec 25, 2017)

swejaljsoni said:


> Anyways, I have added two sites to hosts file as follows
> 127.0.0.1 www.roblox.com # kids video game site
> 127.0.0.1 www.xhamster.com #adult site.
> 
> ...


One more thing, I noticed. Before restarting computer, when I try to connect to roblox.com, I can not connect. But I can connect to www.xhamster.com before or after restarting computer after modification of hosts file.

I just noted one more thing on checking hosts file. After I restart computer, changes that I made to hosts file are removed by itself. I checked hosts file before restarting and changes were there. It did not allow www.roblox.com to load, but www.xhamster.com was able to load. 
After restarting computer, changes from hosts file are removed automatically and both www.roblox.com and www.xhamster.com can be loaded.
Please let me know, how can I keep changes and why one site is able to load even when changes are in effect in hosts file.

Thanks


----------



## SpywareDr (Jun 15, 2013)

The:

127.0.0.1 www.roblox.com # kids video game site​
works fine here in windows 7 Ultimate 64-bit.

In your browser, are you trying to go to "www.roblox.com" or "roblox.com"?

If you want to block access to "roblox.com" as well, you also need to add:

127.0.0.1 roblox.com # kids video game site​


----------



## SpywareDr (Jun 15, 2013)

Since Windows itself will not alter the HOSTS file, either your changes are not being saved or there is something else on your computer undoing the change.

After editing, try adding a "R"ead-only attrib to the HOSTS file:

attrib +r C:\Windows\System32\drivers\etc\HOSTS​
Then to check and see if it worked, use the command:

attrib C:\Windows\System32\drivers\etc\HOSTS​
and you should see both the "A"rchive and "R"ead-only attributes to the left of the drive:\path\filename.

To remove the "R"ead-only attribute so you can edit it again, use the command:

attrib -r C:\Windows\System32\drivers\etc\HOSTS​


----------



## swejaljsoni (Dec 25, 2017)

SpywareDr said:


> Since Windows itself will not alter the HOSTS file, either your changes are not being saved or there is something else on your computer undoing the change.
> 
> After editing, try adding a "R"ead-only attrib to the HOSTS file:
> 
> ...


Even after applying attributes, when I restart computer, changes are removed.
I had to add 4 lines 
127.0.0.1 www.roblox.com 
127.0.0.1 www.xhamster.com 
127.0.0.1 roblox.com 
127.0.0.1 xhamster.com 

I applied attributes as mentioned and then it stopped sites from loading. But with restart, all changes were removed


----------



## SpywareDr (Jun 15, 2013)

Download, install and run Malwarebytes "Free" version is fine.

Then add your HOSTS file additions, add the R attrib to the file, reboot, cross your fingers and see if the changes stuck.


----------



## spunk.funk (May 13, 2010)

well Xhamster.com is a porn video channel, filled with viruses and malware. Why would you want to edit your host file to let this in?
But to each his own. If you don't like my method, try this: [Easily] How to Edit Hosts File in Windows 10 and Windows 8.1


----------



## swejaljsoni (Dec 25, 2017)

spunk.funk said:


> well Xhamster.com is a porn video channel, filled with viruses and malware. Why would you want to edit your host file to let this in?
> But to each his own. If you don't like my method, try this: [Easily] How to Edit Hosts File in Windows 10 and Windows 8.1


I want to edit hosts file not to let any site filled with viruses and malwares. Thats why I am blocking xhamster through hosts file.


----------



## jenae (Jun 17, 2008)

Hi, It's how you are modifying it that is wrong, you don't use 127... use 0.0.0.0 and you need to place it properly, Spunk's link is poor, this one makes it clearer, read it carefully I have modified thousands of hosts files without any of your problems by doing it properly.

https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/


----------



## swejaljsoni (Dec 25, 2017)

After cleaning up system using malware bytes, now hosts file is working and blocks the sites added in the file. Thanks everyone for your support.


jenae said:


> Hi, It's how you are modifying it that is wrong, you don't use 127... use 0.0.0.0 and you need to place it properly, Spunk's link is poor, this one makes it clearer, read it carefully I have modified thousands of hosts files without any of your problems by doing it properly.
> 
> https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/


What happens if you place 0.0.0.0 in place of 127.0.0.1 ? This is out of curiosity.


----------



## SpywareDr (Jun 15, 2013)

127.0.0.1 is the loopback address (also known as localhost).

0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target (a no particular address placeholder).

In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine". If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.

More info: https://superuser.com/questions/949428/whats-the-difference-between-127-0-0-1-and-0-0-0-0
​


----------

