LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > arniekat
User Name
Password

Notices


Rate this Entry

Slackware-13.1-Hacks-Hosts File

Posted 02-16-2011 at 02:34 PM by arniekat

Hosts File

Note-The IP Addresses and Hostnames are made up for this example.

The Hosts file contains a list of IP Addresses mapped to Host Names. It is loaded into memory when your start your computer. Look at the file /etc/HOSTNAME

# cat /etc/HOSTNAME
Strawberry.linux.net

It will have the Host Name of your computer, which you provided when you installed Slackware. But how is it mapped to an IP address? By looking at the /etc/hosts file to see if there is an entry and if not, then a Domain Name Server (DNS) is queried, which is provided by your ISP. The point being that the Hosts file will override a DNS query. To explain this, let's use the Host Name of Strawberry with a Static IP of 192.168.0.20. Look at the file /etc/hosts

# cat /etc/hosts

# hosts This file describes a number of hostname-to-address
# mappings for the TCP/IP subsystem. It is mostly
# used at boot time, when no name servers are running.
# On small systems, this file can be used instead of a
# "named" name server. Just add the names, addresses
# and any aliases to this file...
#
# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine. It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)

# For loopbacking.
127.0.0.1 localhost
192.168.0.20 Strawberry.linux.net Strawberry

# End of hosts.

The IP Address 192.168.0.20 is mapped to a machine called "Strawberry", which is my computer, so anytime a service, say Samba, is looking for an IP Address of 192.168.0.20 or for a computer named "Strawberry" on the network, it will find my box. The address 127.0.0.1 is called the loopback network interface or localhost and is used to specify the hostname of your computer.

The neat thing about the Hosts file is that you can block ads, third-party cookies, third-party hit counters, web bugs and banners by putting the hostname of known offending websites and the IP as 127.0.0.1. This will block the connection to the website from your browser since the Hosts file makes it connect to the loopback network interface. Let's say you have this entry in your Hosts file:

127.0.0.1 ad.badpagecounter.net

If the website tries to redirect you to ad.badpagecounter.net or has a web bug linked to the "badpagecounter" website, the connection will not work! You are protected. Some ad servers try to open a separate connection on the webpage you are viewing so they can track your movements/habits, etc. The Hosts file will block this.

http://www.mvps.org/winhelp2002/hosts.htm

This website is Windows-centric, but has very good information regarding the Hosts file and its uses. Their hosts file works on Slackware.

The file to download is the hosts.zip. The current version is dated January 13, 2011 and is at
http://www.mvps.org/winhelp2002/hosts.zip

After unzipping, you will have a hosts folder. Inside you will find the file called "HOSTS". It will have some introductory information, a localhost entry, then a bunch of website entries like this:

127.0.0.1 localhost

#start of lines added by WinHelp2002
# [Misc A - Z]
127.0.0.1 fr.a2dfp.net
127.0.0.1 m.fr.a2dfp.net
127.0.0.1 ad.a8.net
127.0.0.1 asy.a8ww.net

Open up this file with your favorite text editor and comment-out the localhost entry since it is already in the Slackware /etc/hosts file. We will then be able to append this file to the /etc/hosts and go on with our work. The HOSTS file will look like this after the revision:

#127.0.0.1 localhost

#start of lines added by WinHelp2002
# [Misc A - Z]
127.0.0.1 fr.a2dfp.net
127.0.0.1 m.fr.a2dfp.net
127.0.0.1 ad.a8.net
127.0.0.1 asy.a8ww.net

So that you don't trash your original Slackware /etc/hosts file, copy it to /etc/hosts.backup, then append the modified "HOSTS" file to the /etc/hosts file.

# cp /etc/hosts /etc/hosts.backup
# cat /path/to/the/Modified_HOSTS/file >> /etc/hosts

To see if it worked, run this command

# cat /etc/hosts
Be sure to hold your breath. There are approx 16,500 lines.

Now, logout and login. To test, it, open up your web browser and go to these websites:

http://doubleclick.net/
http://fastclick.net/

You will either get a blank page or an error message "Unable to connect. Firefox can't establish a connection to the server at <Server_Name>. If you can see these websites, then the hosts file is not working.

You may have to comment-out certain sections of the hosts file. For instance, suppose you use a pharmacy to get your pictures developed. When you enter your information and try to load your pictures, it won't work, because the popup does not appear. Your hosts file is blocking it. To know which website is being blocked, left-click on the problem page and select "View Page Info". Click on the Media Tab. Write down the webpage(s) that are greyed-out. These are the ones either backgrounded or not being loaded.

Open up the file /etc/hosts and look for these website(s). When you find it listed in /etc/hosts, put a comment mark on that line. When you are done looking for the website(s), uncommenting the ones you need, save the file and exit. Here is how the line would look in the above example. I want to allow ad.a8.net to popup in my web browser.

127.0.0.1 fr.a2dfp.net
127.0.0.1 m.fr.a2dfp.net
# 127.0.0.1 ad.a8.net
127.0.0.1 asy.a8ww.net

Now go back to your website and try again. Unfortunately, this process is trial-and-error.

Here is another reference which also explains things in a different way and is worth the read. It is geared more towards Linux.
Linux Hosts - http://hostsfile.mine.nu.nyud.net/
This tutorial is also applicable to the hosts file available at the website listed above.
Posted in Uncategorized
Views 2498 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 08:54 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration