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 the early days when the internet was still in infancy if you had entered 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 and 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 /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 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 it to edit hosts files 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: abc xyz

one of the entries in hosts file

$ ping xyz
PING ( 56 data bytes
64 bytes from icmp_seq=0 ttl=113 time=50.554 ms

a ping to later alias xyz retrieves results from previous alias abc

  • 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 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;


In Apache

<VirtualHost *:*>
    ProxyPreserveHost On

    ProxyPass /
    ProxyPassReverse /

    ServerName localhost


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 by default as localhost (a loopback address) so they choose another one as a public IP address to talk in the network. Think of 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.
##	localhost	broadcasthost
::1             localhost

macOS Big Sur

  • Default hosts in Ubuntu  localhost  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       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


For a very basic lesson, let's assume you are running a local web server that resolves a web page or any project at or with a port Now add an alias at end of the line shown like below and your localhost will also start resolving at or Change it like any other domain naming system and it will work perfectly in addition to localhost and	localhost
       # The following lines are desirable for IPv4 capable hosts       localhost

       # is often used for the FQDN of the machine  thishost       foo       bar      master

       # 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.