It’s a system file that defines manual IP resolving for specified domains. If you have a pair of IP and domain name in your hosts file, then your browser won’t rely on DNS resolving for that domain name and will send all requests to given IP.

Hosts file example:

127.0.0.1 www.example.com example.com

This means, that when you try to open any page at www.example.com or example.com, all requests will go to your localhost – 127.0.0.1

Windows hosts file

It’s located on your system drive (in most cases it’s “C:\”), so the full path to that file will be:

c:\windows\system32\drivers\etc\hosts

Mac OS hosts file path

In Mac OS X, the this file is placed in

/private/etc/hosts

but for compatibility, it’s also symlinked to

/etc/hosts

In Linux/Unix systems:

it’s located in

/etc/hosts as well.

Editing hosts file

Any text editor of your choice should be sufficient to edit the hosts file, as it’s a simple plain text file.

The structure of the file is simple:

first goes the IP address, second is the domain – one ore more, they just have to be placed in one line after the IP address, and can be separated by tab, one or more spaces

All these records will work:

127.0.0.1 domain.com
127.0.0.1 www.domain.com 
127.0.0.1 domain.com www.domain.com 
127.0.0.1    domain.com
127.0.0.1 www.domain.com 
1.2.3.4      some-oter-domain.com example.com www.test.local

One thing to remember – if you have more than one occurrence of the same domain linked to different addresses, only the latter one will be applied in your system. So, if you’ll put something like this into your hosts file:

127.0.0.1    domain.com
192.168.1.5    domain.com 

when you’ll try opening http://domain.com, your browser will send a request to 192.168.1.5, as it was the last IP referring to that domain in the hosts file.