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
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
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 Facebook.com or Google.com or any other website then just add 0.0.0.0 as an IP address to the related website (hostname). Follow screenshot show you how to block Facebook.com
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.
If you faced any problem just post comment in this thread and I’ll try my best to help you and solved your problem.