What is a Hosts File?
It is possible to setup IP addresses for a domain on your local system by editing file that can overrule the authoritative DNS. It can permit you to check, what a domain will display if it is pointed to a different server.
To block certain hostnames (like advertising/malicious hosts), the hosts file will be edited or used for web development purposes, i.e. to redirect domains to local addresses.
Windows
Use the below command in the Run Line to open the file with notepad, to access the hosts file in Windows XP, Vista, 7, 8x, 10.
STEP 1: Press Windows + R keys to Run Line.
STEP 2: Copy the below command and paste into the Run Line.
notepad c:\windows\system32\drivers\etc\hosts
STEP 3: It is possible to edit the hosts file, when notepad is open.
For instance, in this example, we will block Facebook. For this, just enter below the line after the # mark in the hosts file.
0.0.0.0 www.facebook.com
STEP 4: After editing your Hosts file, save it.
STEP 5: Now browse Facebook url in IE browser and see that you can’t get to the page.
STEP 6: You shouldn’t be able to access it in Google Chrome too.
Ubuntu
Changes can be made in the hosts file directly via the terminal on In Ubuntu 10.04 and most Linux distros. This is possible using your favorite editor or even opening your favorite GUI text editor. Ubuntu’s hosts file is placed in the /etc/ folder similar to Windows 7x, but here it is the root of the drive. For editing the file, it should be opened as root and so, we use sudo here.
STEP 1: For this, we will use VIM.
STEP 2: With Ubuntu, there is similarly a section for IP6. Most of the time, you will need to just edit its top section and ignore the IP6.
STEP 3: Then save the file and try to open Facebook.com. Similar to that in Windows you will see that you can’t reach the page.
Mac OS X (Any version)
STEP 1: Editing the hosts file in Mac OS X is similar to Ubuntu. Start in terminal and use your favorite editor or favorite GUI text editor, it is easier to do this from terminal.
STEP 2: The file will appear similar to that in Windows, just with a little less explanation. Again you will be redirecting Facebook.
STEP 3: Here’s it seems like 0.0.0.0 is a loopback and will redirect you to the PCs Apache test page.
In this way, you can update hosts file entry on Windows, Ubuntu and Mac.