Understanding Ubuntu Hosts File

The “hosts” file in Ubuntu is a plain text file that associates hostnames to IP addresses. It is utilized to resolve hostnames to IP addresses locally before requesting the information from a DNS server. This can be useful for troubleshooting or creating a custom local development environment. 

This tutorial will provide detailed information regarding the Ubuntu hosts file. 

Working of Ubuntu Hosts file 

When a hostname is entered into a web browser or used in a command line tool, the operating system first checks the hosts’ file for a matching entry. If it finds one, it uses the associated IP address instead of requesting the information from a DNS server. This allows for customizing the resolution of hostnames and can be useful for several different purposes.

How to Access the Ubuntu Hosts File?

To access the hosts’ file, specify the “/etc/hosts” file with the text editor as “nano” via “sudo” privileges as below:

$ sudo nano /etc/hosts

The highlighted line maps the hostname “localhost” and “johnson-vm” to the IP addresses “127.0.0.1” and “127.0.1.1“, respectively.

How to Edit Ubuntu Hosts File?

Users can edit the hosts’ file to perform various functions such as “ping”. In our case, specify the localhost “roger” and map to the IP address “192.168.157.135”:

$ 192.168.157.135  roger

After performing the modification, save and exit the editor.

Verify Mapping Using ping Command

To check the mapping of an edited localhost with the IP address, execute the “ping” command with the “roger” as below:

$ ping roger

The output shows that localhost “roger” has been mapped to the specified IP address.

Conclusion

Ubuntu offers the “hosts” file to map the hostnames to IP addresses. Ubuntu’s default location of the hosts file is “/etc/hosts”. It provides a convenient way to test or troubleshoot issues with DNS resolution quickly. This article has briefly explained the working of the “hosts” file and how to access and edit it in Ubuntu.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×