Posts Tagged ‘hosts file’

Change Where Your Site’s Hosted (without changing where it’s hosted!)

Thursday, April 14th, 2016

The Domain Name System (DNS)

When you type in the name of a website, there’s a lot that happens in the background that will connect your web browser with the server the site is on, and what does that is DNS.  Every website, every service, practically every device connected to the Internet has a unique address that tells every other device where to connect to it.  That numeric address an IP address like this one: 192.99.3.191 (IP version 4) or 0:0:0:0:0:ffff:c063:3bf (IP version 6).  Problem is, numeric addresses are not easy to remember, so the DNS system translates web addresses like www.register4less.com that you and I are familiar with into the IP address where that website is hosted.

The Hosts File

This week, we’ll cover how you can override the actual IP address a website is on for Mac and Linux computers.  Next week, we’ll cover how to do this on a Windows computer.

On your Mac or Linux computer, there is a hosts file that contains a listing of IP addresses and domain names.  Your computer will look to the hosts file prior to going outside to your ISP for cached (saved) DNS or to the DNS server for the domain, so if you have an entry for a domain in your hosts file, it will supersede the domain’s actual DNS.

There are a number of reasons why you would want to have an entry to override a domain’s actual DNS information.  If you’re testing a new environment as discussed above, this is a very quick and easy way to get your domain to resolve to a new host without complications.  You can also use the hosts file to “blackhole” sites that you do not want accessible like spyware sites, ad networks, pornographic sites, etc.  For those sites, you would specify the IP address as 127.0.0.1.

How to Edit Your Hosts File (Windows)

In Microsoft operating systems, most versions will have the HOSTS file is located in the following location:

C:\Windows\System32\Drivers\etc

As with Mac and Linux, the hosts file on Windows is a text file.  To edit the file, open the File Manager, navigate to this directory.  Locate the file, right-click on it, and choose to open the file with Notepad. You will see a file similar to this in Notepad:

# Copyright (c) 1993-2009 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:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
# localhost name resolution is handle within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost

Enter in the the IP address of the new server, along with the domain name and www. plus the domain name.

# Override the actual IP address where the domain's DNS resolves
198.27.68.184 lakesideninos.org www.lakesideninos.org

Write the file and exit Notepad.

How to Edit Your Hosts File (Mac)

The hosts file is a text file that is in a directory that you would not normally access, /etc.  The best way to edit and save the hosts file is to use a text editor on your computer’s console/terminal.  One your mac (if you’re a Linux user, we are assuming you know already how to access the console and use the vi editor), do the following:

  1. Open Finder and click on Applications
  2. Double click the Utilities folder
  3. Double click on Terminal
  4. type sudo vi /etc/hosts and hit the enter key
  5. You will be prompted to enter in your account password for your computer.  Type that in and hit enter.

You will see a file that looks something like this:

# 
# Host Database 
#           
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
# 
127.0.0.1       localhost

In our example we are going to use the domain name lakesideninos.org and the IP address 198.27.68.184, the IP address for our ahs2.R4L.com server.

Adding an entry to your hosts file

  1. Hold Shift and G down together to place the cursor at the bottom of the file.
  2. Type o to go to insert mode on the next line
  3. Type in the IP address followed by a space, then the name of the website, and then the name of the site again with www. in front of it and hit enter. In our example, I would type in the following:
# Override the actual IP address where the domain's DNS resolves
198.27.68.184 lakesideninos.org www.lakesideninos.org

# Block out unwanted websites
127.0.0.1 badsitename.com www.badsitename.com
  1. Hit the Esc key to exit insert mode.
  2. Type :wq and hit enter to save and exit the editor.
  3. type exit and hit enter to close Terminal.

Undoing the Change

Once you’ve tested the site and want the real DNS to apply, you will want to make the changes for the domain to point to the new host (assuming that’s why you are overriding the real DNS resolution), you will want to delete the entries from the hosts file.

  1. Follow the instructions above to open the terminal and edit the file with vi.
  2. Use the arrow keys to move the cursor to the first line where the domain and its IP address appear.
  3. Hit the d key twice to delete the line.
  4. Do the same with the entry for www. plus the domain.
  5. Type in :wq to write the hosts file and quit the editor.
  6. Hold Control d to close Terminal.