What Is the /etc/hosts File in Linux?

The /etc/hosts file is a plain text file used in matching a fully qualified domain name (FQDN) with the server IP hosting a specific domain.

/etc/hosts File

image_pdfimage_print

Before DNS became a network standard, the /etc/hosts file was used to resolve an IP address to a fully qualified domain name (FQDN). This file can be used to manually link an FQDN to an IP address, such as a test server or internal network workstation, without editing DNS server entries. Care should be taken when editing this file. Browsers use it prior to DNS server lookups, and any mistakes can cause issues connecting to a domain or cause users to connect to the wrong server if it isn’t formatted correctly.

What Is the /etc/hosts File in Linux?

The /etc/hosts file is a plain text file used in matching an FQDN with the server IP hosting a specific domain. It’s useful if a DNS server is not available when a user wants to access a domain from their browser. When the DNS server cannot be reached, Linux uses the /etc/hosts file to resolve the domain name. 

Note that occasionally the /etc/hosts file is misspelled as /ect/hosts in documentation. This misspelling can be confusing for new developers or operating system users, but know that the correct spelling is /etc/hosts.

Why Would Someone Modify the /etc/hosts File in Linux?

Suppose that you have a production server available to the public internet, but you need to test a new upcoming version of your website on a publicly available server. The test server is also available on the internet, but you can’t change DNS settings to point the current domain to the new server IP before you test the new server. Using the /etc/hosts file, you can point your local development workstation only to the new server IP address and associate it with the production domain name without interrupting service for everyone else.

System administrators must format the /etc/hosts file properly for it to function on a Linux workstation. Any errors in the /etc/hosts file will leave the user unable to access certain domains, so any changes to Linux machines on an environment should be tested prior to deployment.

What Is the Correct /etc/hosts Format?

The /etc/hosts file stores plain text content, but it requires a specific format. The format is:

Notice that a tab is used in this template. A tab is preferred for ease of reading, but a single whitespace character will also work. Every entry should be on its own line, so a carriage return indicates the end of a host entry.

Also, host entries should have comments added to the file explaining their usage in the interest of clarity for administrators and workstation users. Comments are preceded by a hash character. The following is the template for DNS entries with comments:

Comments don’t require any standard format provided the hash character precedes the comment entry. You can have multiple comments, but every line must start with a hash character.

Introducing the Pure Storage Platform for the new era of data services

An Example of the /etc/hosts File in Linux

You might already have entries in your /etc/hosts file, but you can add more using a simple text editor. You can also remove entries, but remember that the DNS server must be available to reach the domain from a web browser.

The following is an example of a Linux /etc/hosts file:

185.233.11.3 production-db.mydomain.com

The above example points the mydomain.com domain to 185.233.11.2 and then points two subdomains to two other IP addresses. When users type mydomain.com into their browser with the above /etc/hosts file stored on their local machine, the browser uses 185.233.11.2 to query the remote server for a web response. The IP address can be any server or workstation on the network, but it must be accessible from the user’s local machine.

Differences in /etc/hosts File in Mac and Windows vs. Linux

A host file is a standard plain text document used in any operating system. The format is globally accepted, so you’ll find that the hosts file stored on a Mac, Windows, and Linux workstation or server are formatted the same. The only difference in hosts files across disparate operating systems is the text editor you use to make changes to them.

Tips and Tricks for Modifying a /etc/hosts File

Before you edit a /etc/hosts file, especially on a critical system like a production server, always make a backup of it before making changes. Backups protect from accidental mistakes on entries or when the file corrupts after saving. You can recover your /etc/hosts file easily if any changes render the local machine unable to access domains.

The text editor that you use depends on the operating system. A hosts file is formatted the same across operating systems, but administrators usually use text editors native to the operating system when the file must be edited. You could use an alternative text editor, but normally the text editor used is:

  • Notepad for Windows
  • Vim for Linux
  • Terminal for Mac

Wildcard entries aren’t possible in a /etc/hosts file. You might have a wildcard SSL/TLS certificate for a server and must test it, but you cannot add a wildcard for a subdomain in the /etc/hosts file. You must manually add each entry, or you can use a third-party application to handle wildcard entries. 

When you edit a file, make sure you leave comments on new, revised, or removed entries. It’s custom to comment out an entry and test the new /etc/hosts file prior to completely deleting it. By commenting it out first, you can quickly reverse the changes should removing an entry cause any serious issues.

Conclusion

Editing the /etc/hosts file is common, especially for network administrators or developers testing a new server or application hosted on a new server. Take care to follow the format and don’t forget comments so that entries are easily understood by other administrators. Back up the file before editing it so that you can recover if the file gets corrupted after changes.

Written By: