New Users: Quick hosts file
Quick hosts file
During web-site design and development, the hosts file is your friend. You will be amply rewarded for making the effort to understand its capabilities. Before I dive in, a little background knowledge is useful. (Or go straight to real site names to see its true power).
Resolving telephone numbers
A mobile telephone users checks to see if he/she has the name-number pair stored locally. If not, it will resort to looking in a telephone directory (paper version) should this fail resort to calling an operator for a persons telephone number.
Resolving internet address
Resolving an internet address is similar, a computer first checks in its local address book this is a file named hosts. If it cannot find a match it looks in another larger local address book (this may or may not exists such as POSIDAS a personal DNS server). Should this search fail, a last resort is to put out a world wide request on the Internet DNS (domain name service), This also has a hierarchy with your ISP being the first destination in the resolving process.
The hosts file may seem insignificant however it is very powerful. As the first port-of-call in the resolution process, you can return an IP address of your own choice. Use it to speed up external site display, block undesirable external sites, but more importantly harness its power for redirection, easing web page development.
At startup the file is loaded into memory (cache). All it contains are mappings of host names to IP addresses. It overrides any DNS lookups. It is located in this system folder: C:\Windows\System32\drivers\etc. When a browser attempts resolution it follows a defined lookup sequence.
During URL (internet address) IP (numeric) address resolution this sequence is followed:
- The client checks to see if the name queried is its own.
- The client then searches a local Hosts file, a list of IP address and names stored on the local computer.
- The client then checks for a personal DNS server
- If these fail it makes an external DNS request
This process is transparent to the user.
All hosts files contain one very important name-address pair: localhost 127.0.0.1 When you try to connect using either localhost or 127.0.0.1 you are immediately looped back to your own machine. Any address within the range 127.*.*.* may be used for loop back, however stick with convention and use 127.0.0.1
The hosts file is located in the systems folder. The path may be different depending on your setup, these paths are for typical default installations.
|c:\windows\hosts||— Windows 95/98/me|
|c:\winnt\system32\drivers\etc\hosts||— Windows NT/2000/XP Pro|
|c:\windows\system32\drivers\etc\hosts||— Windows XP|
|c:\windows\system32\drivers\etc||— Windows Vista|
|c:\windows\system32\drivers\etc||— Windows 7|
Create a hosts file
After checking the above location and or performing a search, if you cannot find a file, create one.
The default format looks like this. Save it as hosts (no file extension) into the appropriate path as above:
# Copyright (c) 1993-1999 Microsoft Corp. # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # For example: # # 126.96.36.199 rhino.acme.com # source server # 188.8.131.52 x.acme.com # x client host 127.0.0.1 localhost # loop back address
Make sure the file contains the following line: 127.0.0.1 localhost # loop back address
Note: You may use a different loop back address as mentioned above.
Edit Vista or Win 7 hosts file
The "hosts" file is very powerful. It allows any address typed into to a browser to be redirected to any IP address. To prevent this file from being hijacked, Vista and Win 7 impose restrictions on who can edit this file. As a consequence of this, security makes it difficult for legitimate users to modify it.
To successfully modify the hosts file, you need to run notepad.exe as an administrator, otherwise the OS will not let you save the edits. It complains that you don't have permission.
Use the following method to edit the "hosts" file:
- Browse to Start -> All Programs -> Accessories
- Right click "Notepad" and select "Run as administrator"
- Click "Continue" on the UAC prompt
- Click File -> Open
- Browse to "C:\Windows\System32\Drivers\etc"
- Change the file filter drop down box from "Text Documents (*.txt)" to "All Files (*.*)"
- Select "hosts" and click "Open"
- Add the following lines:
(Add any additional lines as required)
- Close Notepad, when prompted click save.
Note: The Uniform Server requires two entries in the “hosts” file (see step 8). The first line is required; it’s the standard loop back address. The second line prevents an error message being displayed in the Vhost section of Apanel.
With the lines added to “hosts” file, check Apanel, which is accessible by typing the following into your browser: '''http://localhost/apanel''' or '''http://localhost/a'''
Known Vista issue
If http://localhost/apanel fails with error message localhost/apanel has no data, try http://127.0.0.1/apanel/ as an alternative. If this works you most likely have entries in the "hosts" file that are specific to IPv6. Look for entries similar to these:
:. localhost or :. 127.0.0.1
Comment them out by adding a # (hash) at the beginning of each line for example:
# :. localhost or # :. 127.0.0.1
The "hosts" file should contain only these lines for IP 127.0.0.1
- 127.0.0.1 localhost
- 127.0.0.1 localhost:80
Restart the server; if it's already running, type http://localhost/apanel or http://localhost/a into your browser and Apanel will display.
Edit XP hosts file
There are no restrictions on who can edit XP’s "hosts" file, hence you can use any text editor of your choice.
It’s worth periodically checking the "hosts" file for any rogue entries, especially if your anti-virus or other protection programs have detected a problem.
Speeding up page loads
Sites you visit regularly are good candidates to put in your hosts file. This can speed up your access to them. A site address may be the result of clicking a page link or one typed in at the browser address bar. Before your computer can access the page it needs to resolve the address into a numeric form.
If this address is not available locally, your browser sends a DNS request to your service provider. The resolution may take several seconds, a long time compared to a few milliseconds for a local resolution. To speed this process up, all you need to do is put the name-address pair in your hosts file.
The downside of this is that if the IP address changes, or is dynamically switched in a pool of addresses, you may be unable to connect. Maintenance of the hosts files was the primary reason for the development of DNS.
You already know the URL (name), (for example, www.uniformserver.com). Like a computer, you need to resolve this to an IP address.
- You can use Ping
- Open a command prompt start > run > type in cmd
- At the prompt type: ping www.uniformserver.com
After a short time, ping will resolve the name to an IP address; it looks similar to this:
Pinging uniformserver.com [184.108.40.206] with 32 bytes of data: Reply from 220.127.116.11: bytes=32 time=259ms TTL=54 Reply from 18.104.22.168: bytes=32 time=239ms TTL=54 Reply from 22.214.171.124: bytes=32 time=235ms TTL=54 Reply from 126.96.36.199: bytes=32 time=199ms TTL=54 Ping statistics for 188.8.131.52: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 199ms, Maximum = 259ms, Average = 233ms
With the name resolved, all you need to do is add the following to the end of your hosts file:
- 184.108.40.206 www.uniformserver.com # Uniserver main site
Repeat the process for each site you would like to speed up.
Another use for the hosts file is to block undesirable sites and advertising.
The hosts file makes it easy to block complete sites or to block ads on your favorite sites.
Use something like this: 127.0.0.1 domain name of site
This is similar to the above. All you need to know is the banner URL
- Right click on the ad banner, select "Properties". This will show the address of the ad banner.
- Make sure it is not the address of the main site. You only need the ad banner address.
With that information, just add it to your hosts file: 127.0.0.1 address for ad banner
You are redirecting these requests to your machine. The pages or images will obviously not be found (shame that) and your browser will just display an empty space where a image is supposed to be or moan that a page cannot be found.
If you pursue this route, be warned that your hosts file will quickly grow. Search the Net for hosts examples and you appreciate what I mean. I am more interested in using the hosts file to make life a little easier for web development.
Real Site Names
All the above tips although worthy of note pale into insignificance compared to this one. If you are developing a site to be hosted on another server or come to that your own server typing localhost or 127.0.0.1 into your browser address bar is not very informative a more desirable solution would be to type in your real Internet address. Using the host file allows you to do just this.
Just in case you missed the significance of the above suppose you install a third party piece of software requiring your real host name and MySQL server host name. You can simulate these locally using the hosts file. Type your web-site name into your browser address bar it instantly loops back and picks up your development server, it does not rush off and pick up the site from the Internet.
For example suppose my real site is www.johndoe.com
Add the following to the hosts file: 127.0.0.1 www.johndoe.com
That’s all its takes simple and very effective.
Your browser now thinks it’s looking at a real site displaying the real site address in its address bar. Click on a link within your site and you will be greeted with its full path again displayed in the address bar for example:
A final piece of magic
One final piece of hosts magic. if you have set up the Apache server for Name based Virtual Hosting, you can add all those site names to the hosts file. This will then loop them back to your development server which in turn will re-direct to the appropriate site. In this way while developing more than one site, you can view them using their real names in your browser.
The Uniform Server makes the above very easy to set-up. check out this section of the admin panel.
If you have changed the Apache listening port to, say 81, then use hosts to redirect to this port. Change the line shown above to look like this:
- 127.0.0.1:81 www.johndoe.com
Alternative to the hosts file
There is an alternative to the hosts file and that is to use PAC. If you need to run Uniform Server on a memory stick, take a look at Virtual Hosting & PAC; it may be of use.