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
- How to view the
- How to test the changes made to
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Hosts file example on Linux
Your Linux system should already have a few lines inside of the
/etc/hostsfile. You can view its contents with the
$ cat /etc/hosts
Here is what the default file looks like on our Ubuntu system:
You should leave the default entries in your
/etc/hostsfile rather than deleting them, as these settings may be relied upon by various system services that are configured on your computer.
hosts file expects one entry per line. An example might look like this:
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
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
$ ping example.com $ ping example.net
As you can see in the screenshot above, our system is not resolving
example.net to IP address
How to edit hosts file
You will need root permissions in order to edit your system’s
/etc/hostsfile. 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.
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.