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).
For instance; in the early days when the 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 the computer already had a record of its IP address mapped to that domain. So you see the network admins used to literally update huge chunks of such records, make a central database of it and other devices would download them as
hosts files. Today most of the internet looks up to 22.214.171.124 and 126.96.36.199 which shrank
hosts files from hundreds to 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
- 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.comthen 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
- You can open it to edit
hostsfiles with any basic to an 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 is 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 it is listed in the same line. For eg:
- The 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 proxymethods 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
/apex domain in both Nginx and Apache is shown below:
Your computer networks need IP addresses that starts from
127. You should not change them because they are reserved as loopback addresses.
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
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).
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
For a very basic lesson, let's 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:xxxx. Change it like any other domain naming system and it will work perfectly in addition to
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 188.8.131.52 master.debian.org master 184.108.40.206 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.