Hosts file example on Linux

The /etc/hosts file can be found on all Linux systems. This is a plain text system file which can be used to map network names (like hostnames of computers on your local network, or URLs to online websites) to IP addresses. The hosts file has a higher priority than any DNS servers your system is configured to use.

In other words, editing the hosts file allows you to override other DNS settings. If you have some entries in the hosts file, these names can be resolved even without access to a separate DNS server. System Administrators may be interested in editing the hosts file when they want to override external DNS resolution, or to simply tell a computer where it can expect to find (by IP address) another host.

In this tutorial, we will show an example of an /etc/hosts file, so you can see how to properly format yours. This will also give you some ideas of what it can be used for, and how to ensure that your file abides by the required syntax in order to be recognized by your system for name resolution.

In this tutorial you will learn:

  • How to properly format the /etc/hosts file
  • How to view the /etc/hosts file
  • How to test the changes made to /etc/hosts file
Hosts file example on Linux
Hosts file example on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Hosts file example on Linux



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


Your Linux system should already have a few lines inside of the /etc/hosts file. You can view its contents with the cat command.

$ cat /etc/hosts

Here is what the default file looks like on our Ubuntu system:

Default /etc/hosts file
Default /etc/hosts file
NOTE
You should leave the default entries in your /etc/hosts file rather than deleting them, as these settings may be relied upon by various system services that are configured on your computer.

The hosts file expects one entry per line. An example might look like this:

192.168.1.100 example.com

In the example above, this line tells our system to look for example.com at IP address 192.168.1.100. These two values can be separated by a space, multiple spaces, or a tab – the important thing is that they need to be on the same line.

You can also add one more aliases to the same line. For example:

192.168.1.100 example.com example.net
Adding two hostnames/URLs on the same line in the hosts file
Adding two hostnames/URLs on the same line in the hosts file

Note that you can add IPv6 addresses to the /etc/hosts file as well.

How to verify changes to hosts file

We can verify that these changes have taken effect by trying to ping the hostnames or URLs we added to /etc/hosts.

$ ping example.com
$ ping example.net
Testing our hosts resolution via ping command
Testing our hosts resolution via ping command

As you can see in the screenshot above, our system is not resolving example.com and example.net to IP address 192.168.1.100.

How to edit hosts file




You will need root permissions in order to edit your system’s /etc/hosts file. You can use any command line or GUI text editor of your choice. For example…

$ sudo vim /etc/hosts
$ sudo nano /etc/hosts

After you make your changes to the file and save them, you should notice that the changes take effect instantly. Some older systems may require that you restart the system’s networking service in order for the changes to do anything.

Closing Thoughts

In this tutorial, we saw how to view, format, and edit the /etc/hosts file on a Linux system. This file is extremely simple, but has a powerful effect on the networking. Knowing how to format and edit this file will allow users to have full control over where their system seeks out certain hostnames.



Comments and Discussions
Linux Forum