How to Edit Hosts File on Linux

November 29, 2023

Introduction

The hosts file allows users to configure how specific hostnames are resolved. Since the file takes precedence over other methods of hostname resolution, SysAdmins frequently edit it to create application testing environments and control which websites the system can access.

This article shows you how to edit the hosts file on Linux.

How to edit Hosts file on Linux.

Prerequisites

  • Command-line access to the system.
  • Administrative privileges.

What Is Linux Hosts File?

Hosts is a plain-text file that maps hostnames to public and private IP addresses. In Linux, it is located in the root-owned etc directory with other necessary configuration files and executables.

By default, the operating system uses hosts as the primary hostname resolution method and checks the file before communicating with a DNS server. If the system detects a relevant mapping in the file, it skips querying the server and resolves the host accordingly.

Why Would You Edit Linux Hosts File?

The simple structure of the hosts file allows quick testing and network customization. The following are the most common reasons to edit the file:

  • Application testing. Developers of applications with integrated networking features use hosts to test ingress/egress traffic and the communication between components.
  • Testing load balancers. The ability to map multiple IP addresses to a single domain allows developers to test the load-balancing capabilities of their website or app.
  • Overriding DNS. The hosts file provides a quick way to test DNS configuration changes by temporarily overriding DNS entries.
  • Creating server aliases. Replacing server IP addresses with easy-to-remember aliases helps organize the development environment.
  • Blocking websites. The hosts file simplifies parental control and blocking websites on corporate networks. The websites are rendered inaccessible by mapping a public domain to a localhost IP address.

Note: Check out our other OS guides for editing the hosts file on Mac or editing the hosts file on Windows.

How to Edit Linux Hosts File?

The recommended way to modify the contents of the hosts file is by using the terminal and a terminal-based text editor. Follow the procedure below to edit the hosts file in Linux.

Step 1: Backup Old Hosts File

The hosts file can contain critical network configuration parameters. Execute the following command to ensure you have a backup copy of the original file before making any changes:

sudo cp /etc/hosts /etc/hosts.bak

If something goes wrong and you want to restore the previous hosts configuration, type the mv command below:

sudo mv /etc/hosts.bak /etc/hosts

Step 2: Open Hosts File

Open the host file using a text editor. This tutorial uses nano:

sudo nano /etc/hosts

The file may already contain some default system IPv4 and IPv6 mappings. IPv4 mappings are at the top of the file.

The look of the Hosts file in Linux.

Step 3: Edit Hosts File

Place your mappings in the corresponding category. Use the following syntax:

[ip-address] [hostname-or-domain-name]
Adding an IP address and domain pair to the Hosts file in Linux.

To set up aliases for the same IP address, put them in the same line but separate the hostnames with a space:

[ip-address] [hostname-or-domain-name] [alias-1] [alias-2] [...]

When you finish, save the file and exit.

Note: The hosts file maps IP addresses only. Create a reverse proxy server to map hostnames to a specific port.

Conclusion

After reading this article, you should know how to edit the host file safely in Linux. The article also briefly overviews the hosts file and how its editing can help in application development and network administration.

Next, learn more about Linux configuration by reading Linux Config Files - A Comprehensive Guide.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
What Is 127.0.0.1 Localhost?
February 17, 2022

Localhost is essential for hosting web apps locally, as well as testing applications. Learn what localhost is and why it is...
Read more
How to Edit Hosts File on Mac
November 21, 2023

Administrators edit the hosts file to control domain and hostname resolution on the system manually.
Read more
How to set DNS Nameserver on Ubuntu
May 18, 2021

This tutorial will show you how to change DNS nameservers on your Ubuntu machine using GUI or the command line.
Read more
How to Resolve the "Temporary failure in name resolution" Error
November 9, 2023

This tutorial will show you how to troubleshoot the "Temporary failure in name resolution"...
Read more