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. localhosts #loopback

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

Windows 8 or 8.1 or 10

Unfortunately Windows 8 or 10 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. If you’re using Windows 10 this will be on the Start Menu.

If you’re using Windows 10, it’ll look more like this:


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



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


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.


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


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


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.



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 Just like in windows we will see that we are now redirected to a site that does not exist.


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 is a loopback and will direct you to the computers Apache test page.


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 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 02/1/15

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) 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?



    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


    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 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
    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:

    I wrote:

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

    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

    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…

More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!