Local DNS /etc/hosts file
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:
- 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 proxylocalhost:8000
to/
apex domain in both Nginx and Apache is shown below:
In Nginx
In Apache
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
- Default hosts in Ubuntu
- Default hosts in Ubuntu Server
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.