/etc/hosts – hosts file in Ubuntu distribution

/etc/hosts – is a hosts file in Ubuntu distribution. The file contains a list of IP addresses and associated hostnames. Each line in hosts file will correspond to one IP address entry followed by associated canonical hostname.

So, every entry would resemble –

IP_addresses canonical_hostname

Here, IP addresses and canonical hostnames should be separated by at least one blank space. And, if we put # at the beginning of every entry then that particular line would be ignored.

One of the key benefits in keeping important entries in hosts file – it is quite useful in case our DNS servers are not reachable for some reason. In addition to, almost all applications check /etc/hosts file before asking for relevant entries from a DNS server. Therefore, we can utilize this property to our advantage as and when required.

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

/etc/hosts – hosts file in Ubuntu

A normal user can only read the contents of a /etc/hosts file. We need to have administrative account i.e. root privileges to make necessary changes, if required.

To view the contents of /etc/hosts file using cat command-line utility, issue the following in terminal –

cat /etc/hosts

It would return with the following –

127.0.0.1 localhost
127.0.1.1 techpiezo-pc
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

here,

127.0.0.1 is for our localhost (needed for IPv4 capable hosts),

127.0.1.1 is a fully qualified domain name (FQDN) of the machine we are working on, and

::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

it is also for our localhost (needed for IPv6 capable hosts).

Now, to edit the /etc/hosts file – issue the following in terminal –

sudo nano /etc/hosts

replace nano with a text-editor of your own choice.

Append the file and provide the IPv4 address for associated canonical hostname. For instance,

XXX.XXX.XXX.XXX <canonical_hostname>

Similarly, replace IPv4 addresses with IPv6 addresses to get desired outcome.

In conclusion, we have discussed /etc/hosts – hosts file in Ubuntu distribution.