Adding a website to hosts file and testing it

Description

Testing a website by adding it to the hosts file


Contents

Overview

In many cases you might be transferring a website from another hosting platform to one of the LeaseWeb's web hosting servers. This page provides you instructions on how you can test the website functionality without making any changes to the domain's DNS records. You need to know the new IP address of the web hosting server. You can usually find this from the order confirmation email/details.

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

In some operating systems, the contents of the hosts file is used preferentially to other name resolution methods such as the Domain Name System (DNS), but many systems implement name service switches to provide customization, such as nsswitch.conf for Linux and UNIX. Unlike remote DNS resolvers, the hosts file is under the direct control of the administrator of the local computer.

Editing the hosts file

In Linux

Most of the Linux distributions use the hosts file at /etc/hosts. You will need root/sudo access to edit this file. Use your favorite editor to open the file. By default it looks something like this:

127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback

To add your domain for testing purposes, in the hosts file, you need to specify what domain points to which IP address. In this example, we will use the IP address 192.168.0.1 and the domain example.com. You will need to add the line:

The file should look like this after editing:

127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
192.168.0.1 www.example.com example.com 

Save the file. All requests for the domain www.example.com and example.com will direct to the specified IP (192.168.0.1). 

In Windows

In Windows XP and higher versions, the hosts file is located in %SystemRoot%\system32\drivers\etc\hosts, where %SystemRoot% is the directory where Windows has been installed. By default, this should be C:\Windows\System32\drivers\etc\hosts. You can edit the file with notepad, but in most cases you will have to do this as an admin user. To do this:

  1. Click the Start menu.
  2. Select All Programs.
  3. Select Accessories.
  4. Right-click on Notepad and select Run as administrator.
  5. Browse to the following path: %SystemRoot%\system32\drivers\etc\ (or as by default C:\Windows\System32\drivers\etc\).
  6. You will need to choose that instead of showing Text Documents (*.txt) you will see all files in the directory.
  7. Open the hosts file.

By default, there should be no entries. You can add new entries after the comment lines that starts with #. After editing, the file should look like this:

.
.
.
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

192.168.0.1 www.example.com example.com

In Mac OS X

Mac OS X stores the hosts file at /etc/private/hosts. Easiest way to edit this is from the Terminal. You can do this either by typing "Terminal" into Spotlight or by opening Finder and navigating to Applications > Utilities > Terminal. To open the hosts file for editing, type:

sudo nano /etc/private/hosts

The file itself should look the same as in Linux or any other *nix system. You can add the the new IP for the domain:

127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
192.168.0.1 www.example.com example.com

Testing the hosts file

Pinging the domain name is the easiest way to test if the newly configured IP is working. Tools like dig, nslookup, and host use the DNS data to retrieve information - because of this you don't see the changes in effect.

To test that the domain is pointed to the correct IP in your hosts file, type the following:

The response should come from the newly configured IP:

PING www.example.com (192.168.0.1) 56(84) bytes of data.
64 bytes from www.example.com (192.168.0.1): icmp_req=1 ttl=58 time=1.40 ms
64 bytes from www.example.com (192.168.0.1): icmp_req=2 ttl=58 time=1.31 ms
64 bytes from www.example.com (192.168.0.1): icmp_req=3 ttl=58 time=1.36 ms
64 bytes from www.example.com (192.168.0.1): icmp_req=4 ttl=58 time=1.36 ms

In Linux or Mac OS X, you can use ping from the Shell or Terminal respectively. In Windows, you will need to open the command line: Press CTRL + Windows button on the keyboard. In the pop-up window, type cmd and hit enter.



Get Support

Need Technical Support?

Have a specific challenge with your setup?

Create a Ticket