How To Block or Access a Website by Editing Hosts file in Ubuntu

Now a days I am in the process to convert my Blogger blog to self host WordPress platform. If you are familiar with blog platform then you might be know that WordPress is much better than Google Blogspot Platform and in the process of conversion I need to change my hosts file to test my new site so that only I can see that my site is working well or not.Now you might want to know something about the Hosts file. As per Wikipedia

The Hosts file is a computer file used in an operating system to map hostnames to IP addresses. The hosts file is a plain-text file and is conventionally named hosts.

The hosts file is one of several system facilities to assist in addressing network nodes in a computer network. It is a common part in an operating system’s Internet Protocol (IP) implementation, and serves the function of translating human-friendly hostnames into numeric protocol addresses, called IP addresses, that identify and locate a host in an IP network.

In some operating systems, the hosts file content is used preferentially over other methods, such as the Domain Name System (DNS), but many systems implement name service switches (e.g., nsswitch.conf) to provide customization. Unlike the DNS, the hosts file is under the direct control of the local computer’s administrator.

In broad sense whenever you type a web address in your browser URL first it looks into DNS to translates to the IP4 or IP6 addresses to access the site and Hosts files works in the same way as DNS works but the only difference is that you find Hosts file in your own computer. DNS translate human friendly hostnames into IP addresses and this process will consume some time and if the DNS  are down then you are not able to access the site but if you use Hosts file for mapping hostnames to IP addresses then you can access the page smoothly and it is also accelerate the access speed of the website. 

In modern systems, even though the host table has been superseded by DNS, it is still widely used for bootstrapping, NIS, isolated nodes.

The Hosts file contains one entry per line consisting of an IP address in the first text field followed by hostnames, and an optional list of aliases for the hostname . Each field is separated by white space (blanks or tabulation characters). Comment lines may be included; they are indicated by a hash character (#) in the first position of such lines. Anything following a hash sign (#) is regarded as a comment and is ignored. Entirely blank lines in the file are ignored.

The Hosts file normally named “hosts” without any extension. The location of Hosts file in Linux OS (like Ubuntu) is /etc/hosts . For example, a typical hosts file may look like the following screenshot

Hosts View How To Block or Access a Website by Editing Hosts file in Ubuntu

Now I discuss the proper method how you edit the Hosts file in Ubuntu. 

1. First open Terminal. If you don’t know how to open Terminal then visit this page. For Ubuntu 11.10 Keyboard shortcut is Ctrl + Alt + T 

2. Now enter the following command and hit Enter button. Then it ask for password.

                                sudo  nano /etc/hosts

Hosts file in Ubuntu Command How To Block or Access a Website by Editing Hosts file in Ubuntu

3. Now enter your administrator password and hit Enter button to display your Hosts file in Terminal Window.

4. Now for example, if you want to block or  or any other website then just add as an IP address to the related website (hostname). Follow screenshot show you how to block

Hosts file in Ubuntu edit How To Block or Access a Website by Editing Hosts file in Ubuntu

If you want to access a website then in the same way add the IP address of that website.

5. Now Press Ctrl+O button to overwrite the existing host file. And finally hit Enter button for permanently save changes in your Hosts file.

Hosts file in Ubuntu save How To Block or Access a Website by Editing Hosts file in Ubuntu

If you faced any problem just post comment in this thread and I’ll try my best to help you and solved your problem.

If you like my post then Subscribe my blog RSS feed or signup for free Email Newsletter.

About Swarup
+Swarup is a student and part-time blogger from India. He loves to blog about software, gadgets, mobiles and anything related to Technology. He also love Linux, Windows, Test Softwares, Reading Books & Blogs, Watch Movie, Play Games. If you like This post, you can follow TechJagat on Twitter , like us on Facebook and circle us on Google+.

4 thoughts on “How To Block or Access a Website by Editing Hosts file in Ubuntu

  1. Thankful
    segment about the way of To Block or
    Access a Website by Editing Hosts file in Ubuntu. Provided proper method is
    really good from all side and that’s why I’m impressed. Thanks mate.

  2. @swarup, i’m unable to open and some other websites in ubuntu [in any version], so by this method can i access those sites too ????

    • Hi Aamir

      I don’t know why u unable to open and some other websites in ubuntu but here I am telling you the process how u add those site in hosts file -

      First visit this page –
      Now convert your website address to IP address.
      Now open Hosts file in Ubuntu using terminal
      Now IP address to the related website address twice, here use WWW for first one and second one is non WWW, for example use below format

      Now saved the hosts file. Thats all.

Leave a Reply