Photo by Random Institute / Unsplash

About your local DNS /etc/hosts file

architecture Nov 12, 2021

Hosts file is a simple text file that functions like a table lookup in plain text format for the network settings inside your OS. Your computer looks up to this file before looking at internet (DNS servers).

TL;DR -  hosts - static table lookup for hostnames

For instance; in early days when internet was still in infancy if you had entered twitter.com then your network could only resolve or understand it if the hosts file in your computer already had a record of its IP address mapped to the domain twitter.com. So the network admins used to literally update huge chunks of such records, make a central database of it and other connected devices would download them as hosts files. Today most of the internet looks up to 8.8.8.8 and 1.1.1.1 which shrank hosts files from hundreds to around 10 lines of records.

All popular OS like Windows, MacOS and Linux have a text file within their system folders called Hosts. It is a text file that maps IP numbers to hostnames and alias.

Things you can do with /etc/hosts file:

  • Your network looks up to this file over anything at all every time. It supersedes all the DNS lookups, server configs or manual overrides. Hence if you set 127.0.0.1   google.com then your network will loopback it as unreachable.
  • deny/ block the access of specific websites on a DNS level
  • create your own custom web links to map specific domain naming
  • map/ override an IP address to any custom hostnames/  specific aliases
  • specify custom name aliases for network locations within your computer.

Things to know about /etc/hosts file:

  • You can open to edit hosts files with any basic to advanced text editor in any GUI or even terminals. Use Vi/m, Nano, Notepad, TextEdit, Sublime, Atom or even Webstorm.
  • The format to write entries is: IPv4/IPv6 hostname [aliases...] # comment. Each one separated by tabs or spaces.
  • Empty lines are allowed.  #  comments starts with hash to the end of line.
  • After canonical hostname each of its aliases supersedes the first priority with the order in which its listed in the same line. For eg:
192.168.12.34 main.example.com blog.example.com abc xyz
one of the entry in hosts file
$ ping xyz
PING abc.example.com (192.168.12.34): 56 data bytes
64 bytes from 192.168.12.34: icmp_seq=0 ttl=113 time=50.554 ms
a ping to later alias xyz retrieves results from previous alias abc
  • Hosts file does not work with Ports/ Port numbers. For eg: localhost:8000 will not work. Hence ports have no meaning in hosts files.
  • However you can use reverse proxy methods to achieve port manipulation. Usually we implement them in config files of web servers like Nginx or Apache. A basic example of a server block code to reverse proxy localhost:8000 to / apex domain in both Nginx and Apache is shown below:

In Nginx

server {
    listen 80;
    server_name 123.45.678.901;
    
    location / {
        proxy_pass http://localhost:8000;
        include /etc/nginx/proxy_params;
        proxy_redirect off;
    }
}
/etc/nginx/sites-enabled/example.com

In Apache

<VirtualHost *:*>
    ProxyPreserveHost On

    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/

    ServerName localhost
</VirtualHost>
/etc/apache2/sites-enabled/000-default.conf

Your computer networks needs IP addresses that starts from 127. You should not change them because they are reserved as loopback address.

Each device connected with every other creates a network. Every device talks with every other using a public IP address that's unique within that connected network. Now every one of these devices has 127.0.0.1 by default as localhost (a loopback address) so they choose another one as a public IP address to talk in the network. Think of 127.0.0.1 aka. localhost as your nickname that only your family knows and uses to call you inside your home (localhost) whereas your legal name is how you are known and used in the outside world (internet).

Usage - IPv4/IPv6 hostname [aliases...] # comment

Some examples of how hosts file looks by default in different OS:

  • Default hosts in macOS
##
# 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
255.255.255.255	broadcasthost
::1             localhost
macOS Big Sur
  • Default hosts in Ubuntu
127.0.0.1  localhost
127.0.1.1  ubuntu-focal # here you should have your hostname

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Ubuntu 20.x Desktop
  • Default hosts in Ubuntu Server
127.0.0.1       localhost.localdomain   big # the second place is your server name
::1             localhost6.localdomain6 localhost6

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
Ubuntu 20.x Server Edition

Usage

For very basic lesson, lets assume you are running a local web server that resolves a web page or any project at 127.0.0.1 or with a port 127.0.0.1:xxxx. Now add an alias yourname.com at end of the line shown like below and your localhost will also start resolving at yourname.com or yourname.com:xxxx. Change it like any other domain naming system and it will work perfectly in addition to localhost and 127.0.0.1.

127.0.0.1	localhost	example.com
# The following lines are desirable for IPv4 capable hosts
127.0.0.1       localhost

# 127.0.1.1 is often used for the FQDN of the machine
127.0.1.1       thishost.mydomain.org  thishost
192.168.1.10    foo.mydomain.org       foo
192.168.1.13    bar.mydomain.org       bar
146.82.138.7    master.debian.org      master
209.237.226.90  www.opensource.org

# The following lines are desirable for IPv6 capable hosts
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
I edit and update this article timely. Thank you.

Tags

Sparsh

Software Engineer