• ARTICLES
SEARCH

How-To Geek

Beginner Geek: How To Edit Your Hosts File

On occasion you will need to edit the hosts file on your machine. Sometimes because of an attack or prank, and others so that you can simply and freely control access to websites and network traffic.

hosts files have been in use since ARPANET. They were used to resolve hosts names before DNS. hosts files would be massive documents used to aide the network name resolution.

Microsoft kept the hosts file alive in Windows networking which is why it varies very little whether used in Windows, OS X or Linux. The syntax stays mostly the same across all platforms. Most hosts files will have several entries for loopback. We can use that for the basic example for the typical syntax.

The first part will be the location to redirect the address to, the second part will be the address that you will want to redirect, and the third part is the comment. They can be separated by a space, but for ease of reading are typically separated by one or two tabs.

127.0.0.1 localhosts #loopback

Now let’s look at accessing the hosts files in the different operating systems…

Windows 8 or 8.1

Unfortunately Windows 8 makes it annoying to open apps as administrator — but it’s not too difficult. Just search for Notepad, then right-click on Notepad in the search results list, and choose to run it as administrator.

Once you’ve done so, open up the following file using the File -> Open feature.

c:\windows\system32\drivers\etc\hosts

Then you can edit as normal.

Windows 7

To access the hosts file in Windows 7 you can use the following command in the Run Line to open notepad and the file.

notepad c:\windows\system32\drivers\etc\hosts

sshot-2010-08-31-[19-41-19]

Once notepad is open you can edit the file. In this example we will block Facebook. To do this just enter in the following after the # mark.

0.0.0.0    www.facebook.com

sshot-2010-08-31-[20-51-49]

Now that you have edited your Hosts file make sure to save it.

sshot-2010-08-31-[20-54-07]

Now notice if we try to access Facebook in IE we can’t get to the page.

sshot-2010-08-31-[20-56-44]

We also were not able to get to it in Google Chrome… (check notes at the end). Also for more info on editing your Hosts file, check out The Geek’s article on how to create a shortcut to quickly edit your Hosts file.

sshot-2010-08-31-[21-04-27]

Ubuntu

In Ubuntu 10.04 and most Linux distro’s you can edit the hosts file directly in the terminal. You can use your favorite editor or even open your favorite GUI text editor. For this example we will use VIM. Like Windows 7, Ubuntu’s hosts file is located in the /etc/ folder, though here it is in the root of the drive. In order to edit the file you will need to open it as root which is why we use sudo here.

Now that it is open we can edit it to redirect Facebook into nothing. You will notice that with Ubuntu there is also a section for IP6. For most needs you will only need to edit it the top section and ignore the IP6.

Now we can save the file and try to go to Facebook.com. Just like in windows we will see that we are now redirected to a site that does not exist.

sshot-2010-08-31-[23-14-30]

Mac OS X (Any version)

In OS X, accessing the hosts file is very similar to Ubuntu. Begin in terminal and use your favorite editor, even is you wish to call a GUI text editor, it is easier to do so from terminal.

The file will look a bit more like Windows, only with a little less explanation. Again we are going to redirect Facebook.

This time it seems that 0.0.0.0 is a loopback and will direct you to the computers Apache test page.

Notes

There are some things to note from this walkthrough that we did notice. When tested it, Chrome did not use the hosts file in any operating system but we were able to block Facebook in Chrome by adding www.facebook.com. Also, make sure to place and extra line after the last entry for the section.

This should get you started in understanding the Hosts file and how it can help protect your computer. You can use it to block sites that you don’t want a PC to be able to access. If you have more suggestions for any of the operating systems we coved, then leave a comment and let us know!

Create a Shortcut to Quickly Edit Your Hosts File in Windows

Chris is a Mac geek who still knows his way around Linux and Windows. He's always looking for a good way to translate geek to english.

  • Published 09/1/13

Comments (19)

  1. kreager

    In what context would one need to edit ons host file?

  2. Tangmeister

    also, Google chrome often accesses folders you shouldn’t be able to…

  3. Bob

    I regularly perform virus removal and have noticed a few times when an infected machine will not connect to Windows update even when all other sites load fine. I have seen this problem continue even after removing all infections (checked clean with several different A/V programs), but I have noticed that somehow MS Security Essentials seems to fix it just by installing it.

    I never knew how or why this works but after reading this it seems like maybe some infections alter the IP address of the Windows update website in the host file and that MSE must check and fix this during the install.

    I can not confirm this at the moment but the next time I come across a machine with this issue I will check the host file first and update this thread.

  4. Zymastorik

    This doesn’t actually block access to these sites. It merely prevents the OS from performing a DNS lookup by prodiving it with (false) information about where (in the example) http://www.facebook.com is located.

    If you input the ip address into your browser it will load the page located there.

    Just thought I would clarify, it’s not a block on the site but a redirect of the DNS query being made to resolve it’s IP address.

  5. Costy

    can you in any way direct programs to use a specific internet source (i.e. my 3g stick for Yahoo Messenger and GTalk and my ethernet for the rest) ?

  6. KSizemore

    It should be noted that with UAC on (Windows Vista and above), you will need to run the command in an elevated (Administrative) command prompt or you will be prevented form saving the changed Hosts file in the default location (you will get the Save As dialog when you attempt to save, and it will try to save the file to My Documents instead)

  7. KSizemore

    Gah…typo: “you will be prevented FROM saving”

  8. Zymastorik

    Interesting. Curious why my previous comment was moderated out? Would be nice to get a bounce back about that.

  9. Syb

    what would be good instead of it displaying the default connection error and diagnose button etc would for it to redirect to a webpage either on the machine itself or a page in a separate folder on your site so it displays a custom message/page of your own choice ..

    anyone know if that can be done? and of course how to do it?

    rgds
    Syb

  10. FilePanda.co.uk

    You would change or turn the host file off if it is re-directing you to a dodgey site. e.g if you are going onto you internet banking and your host file is compromised it could lead you to a fake website that looks the same as your internet banking and if you did not realise you may enter your details. The term for this is Pharming Attack

  11. FilePanda.co.uk

    Another thing you can also disable UAC (User Account Control) by typing MSCONFIG into your Run box and unticking it :note you will have to restart the computer before it takes effect

  12. Dav

    In Ubuntu 10.10 hosts file seems to have been seperated into hosts.allow; host.deny

    could you please guide as to how + where one can copy paste the host file listed in mvps.org site ?

  13. encoderX

    @Dav (above)
    In terminal type: sudo gedit /etc/hosts

  14. sheik

    anyone can help me….
    i wish to block facebook frm ma lap
    which IP address i should give ….?
    in above xample shown 0.0.0.0
    its working….???

  15. sheik

    wr i can save d edited file…..??

  16. Israel

    Just tried it out. It works. But as one of the peers in a comment says: ‘you can access the page if you write the page’s server IP address’.

    I tried to write the server IP address of a page… for example, instead of:

    0.0.0.0 facebook.com

    I wrote:

    69.63.189.16 facebook.com

    And it doesn’t work. The page appears as if I hadn’t modify the host file, whether I write facebook.com or 69.63.189.16.

    Any ideas?

  17. johnnydollar

    I have used the Mvps custom Hosts file in Windows with “Hostsman” and then copy and pasted IT into the Ubuntu /etc/hosts file. Of course you have to use sudo gedit before that. It Works fine, just like Windows. Only trouble, There is no “server” function like HOSTSMAN has to substitute a colored photo or .jpg of your choice to Replace the blank space where the AD would have been. I TRIED in Vain to explain this to my relatives and how easy it is on XP especially and why it’s good. An effective layer of protection. Finally I showed them how after their daughter *who clicks on flashing banners and anything, welcomed several bad things on to the computer. OH! light bulb went off. Thanks for the refresher How to Geek. :0)

  18. avi raj

    thanks for the post . media fire was blocked in our collage and i managed to get all the ips and host name of media fire and posted in host file . now i can acess it as i used to do before blocking .
    but from where i can get all “ips and corresponding hostname for shared”

  19. daniel

    I´m setting up an svn repository on a Network Attached Storage device, connected to a local network at the office. At the same time i would like to check out the repo from home (over wan).

    Now comes the tricky part that i´m still trying to figure out…
    I have some svn:externals references set up in the project, binding it to some other library-projects. Of course at home i had to map these LAN addresses to the external ip of the network to make it work. Now back at the office i would love to make use of the wired ethernet network we have set up, and not to have to pass through the internet to access those external resources.
    I thought of something like editing this hosts file in a manner similar to this:

    # local network address of NAS – server domain name
    192.168.0.2 server.com

    But it doesn´t really work, as the files on the NAS is not directly accessible by its local IP, but rather by shared folders in windows.

    Is there a way to use the hosts file to map a domain to a local shared folder? Or am i on the wrong track?
    Any help appreciated…

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!