Posted on

editing the host file

When you search for a site with your browser, type a name in the search bar and find the site you are looking for. When you do this you are actually querying a server on the internet telling it “I want to go to www.mydomain.com”; this server contains a list of all internet addresses with the “real” site address which, in machine language, is a sequence of 4 numbers: XXX.XXX.XXX.XXX. With this information, NAME – NUMBER relationship, the server reaches the site you are looking for and sends it back to you: you see it on your browser. Long procedure to explain but which is done in an instant.

Host file

It may happen that you need to define this NAME-NUMBER relationship yourself, practically cheating the external server. When does this situation happen? It happens to systems engineers when, in an internal computer network, not having a server machine available to store the NAME-NUMBER relationship, they have to tell their users: “go to the site http://192.168.0.254”. Perhaps the user is not very happy with having to remember a number. It would be much better to tell him go to http://yourdomain.com.

This need occurs above all to those who develop websites .. The web designer must develop a new version of the site yourdomain.com. If the web design searches for the site, our external server sends it back to the old site because this is still active for the internet world with, for example, number 192.168.0.254. The new site instead is hosted on a new machine, for example 192.168.100.1, but the internet does not know this information. So how do we tell the browser that when it loads your domain.com it doesn’t have to go to the site where all the surfers go, at the moment, but to the new site?

System engineers or web designers have to cheat the browser by telling it: “when I want to go to yourdomain.com do not query the external server but follow my instructions”. To do this, they act on the host file of their computer. The host file is an empty file that must be compiled in this way. Returning to our example

192.168.100.1        yourdomain.com

192.168.100.1        www.yourdomain.com

With these indications the browser does not query the external server but does everything by itself because it knows the NUMBER; and then loads the site from a machine (192.168.100.1) which is not the one that all the other surfers see (192.168.0.254).

Editing the hosts file on Apple Mac

Editing the hosts file on Apple Mac

Open a terminal. To be able to open a terminal, open Spotlight (CMD + spacebar). Type “terminal” and click the first result.

Once the terminal is open, type the following command:

sudo nano /etc/hosts

press “Enter”

Once opened, we will see a screen very similar to this:

Within our hosts file we will have to enter two basic information in order to reach our site / resource.

Particularly:

  • SERVER_IP_ADDRESS: represents the public IP address of the server or resource, the NUMBER (ex: 192.168.100.1)
  • domain.com/www.domain.com: represent the NAME of the site, in a form that can be recognized by humans
  • The NUMBER and NAME must be separated by a keyboard “TAB”., Then type the number, press TAB on the keyboard and type the name.

To save the file, just press CTRL + X, and then, when prompted to save, press Y