The /etc/hosts File in Linux: Configuration and Usage

09 Nov 2026, 11:57:57
The /etc/hosts file is one of the oldest and simplest tools in Linux. It allows you to define mappings between IP addresses and domain names locally, bypassing DNS servers. Despite its simplicity, it is often useful when migrating websites, testing changes, troubleshooting network issues, and configuring communication between servers.
In this article, we'll explain how /etc/hosts works, how to edit it correctly, how to verify the results, and what potential issues to watch out for.

What Is the /etc/hosts File?

/etc/hosts is a plain text file containing a list of mappings between IP addresses and hostnames. It is owned by root, and its standard permissions are 644: any user can read the file, but only the superuser can modify it.
Example of typical contents:
127.0.0.1 localhost
127.0.1.1 server.example.com server
192.168.1.100 example.com
::1 localhost ip6-localhost ip6-loopback
When an application tries to access example.com, the operating system first checks its local name resolution sources. If an entry for the domain is found in /etc/hosts, the specified IP address is used, and no request is sent to a DNS server.
This allows you to direct requests to a specific server without changing the domain's public DNS records.

Record Format

Each line in the file follows this format:
IP_address canonical_hostname [alias ...]
Fields are separated by spaces or tabs. Here are a few rules to remember:
  • Multiple hostnames can be assigned to a single IP address on the same line.
  • Everything after the # character is treated as a comment.
  • Empty lines are ignored.
  • Both IPv4 and IPv6 addresses are supported.
  • Wildcard patterns such as *.example.com are not supported: each hostname must be specified explicitly.
  • Port numbers cannot be specified in this file — it only maps hostnames to IP addresses.
  • If the same hostname appears multiple times in the file, the first matching entry is generally used.
Example with comments and multiple hostnames:
# Test server after migration
192.168.1.1 example.com www.example.com

How Name Resolution Works

The order in which Linux checks name resolution sources is determined by the NSS (Name Service Switch) mechanism. Its configuration is stored in the /etc/nsswitch.conf file.
To view the lookup order for hostnames, run:
grep '^hosts:' /etc/nsswitch.confA typical result looks like this:
hosts: files dns
This means the system checks local files (including /etc/hosts) first and then queries DNS. If a hostname is found in /etc/hosts, the lookup generally stops there.
On modern distributions, the line may be more complex, for example:
hosts: files mdns4_minimal [NOTFOUND=return] resolve [!UNAVAIL=return] dns
The principle remains the same: files comes first, so local entries take priority.
Important: The lookup order depends on the NSS configuration and the name resolution services in use. If the files source appears after dns in nsswitch.conf, local entries may not be applied first.
It is also worth remembering that not all programs use the system's name resolution mechanism. Utilities such as dig and nslookup query DNS directly and ignore the /etc/hosts file. This is expected behavior: they are specifically designed for DNS lookups.

How to View the File Contents

To display the entire file:
cat /etc/hostsTo view it page by page:
less /etc/hostsExample of standard contents:
127.0.0.1 localhost
127.0.1.1 server
::1 localhost ip6-localhost ip6-loopback
The 127.0.1.1 entry is commonly found in Debian and Ubuntu. It maps the computer's hostname to a local address. Standard entries usually do not need to be modified.

How to Add an Entry

Superuser privileges are required to modify the file. Before making changes, it is a good idea to create a backup:
sudo cp /etc/hosts /etc/hosts.bakOpen the file in a text editor, such as Nano:
sudo nano /etc/hostsAdd the following lines at the end of the file:
192.168.1.1 example.com
192.168.1.1 www.example.com
Save the changes:
  • Ctrl + O — save the file;
  • Enter — confirm the filename;
  • Ctrl + X — exit the editor.
Now, whenever the system accesses example.com, it will use the IP address 192.168.1.1.
A quick alternative is to add an entry with a single command, without opening an editor:
echo "192.168.1.1    example.com www.example.com" | sudo tee -a /etc/hostsPlease note: The command sudo echo "..." >> /etc/hosts will not work because the redirection is performed by the shell running with regular user privileges, not by the sudo command. This is why tee -a is used instead.

How to Verify That an Entry Works

After saving the changes, check hostname resolution using one of the following methods.
getent is the most reliable option because it uses the same NSS mechanism as most applications:
getent hosts example.comIf the entry is being applied, the command will return the specified address:
192.168.1.1 example.com
ping — the output should display the IP address specified in the file:
ping -c 4 example.comcurl — check the website's HTTP response:
curl -I https://example.comWhen accessing a domain through a local entry, the hostname is preserved in both the TLS connection (SNI) and the Host header. This means that SSL certificate verification and virtual host configuration work correctly.
If you need to test a new server without editing /etc/hosts, use the --resolve option:
curl -I --resolve example.com:443:192.168.1.1 https://example.comThe command sends the request to the specified IP address while preserving the domain name for SSL certificate verification and the Host header. This is convenient when you need to perform a one-time test without modifying system files.

Practical Example: Testing a Website on a New Server

One of the most common use cases is migrating a website to another server.
Suppose example.com is being moved to a new VPS with the IP address 192.168.1.1, but its DNS records still point to the old server.
To make sure everything works before switching DNS:
  1. Open the /etc/hosts file on the computer you will use to test the website.
  2. Add the following entries:
192.168.1.1 example.com
192.168.1.1 www.example.com
  1. Save the file.
  2. Open the website in your browser.
Your browser will now connect to the new server even though the public DNS records have not yet been updated.
This allows you to check:
  • whether the website loads from the new server;
  • whether pages and sections work correctly;
  • whether the web server is configured properly;
  • whether HTTPS works and the correct SSL certificate is installed;
  • whether the CMS, PHP, and database work correctly.
Once testing is complete and DNS has been switched, remember to remove the temporary entries. Otherwise, you may continue accessing the new server even after the DNS address changes, potentially overlooking issues with actual DNS resolution and routing.
Important: Changes to /etc/hosts apply only to the device on which the file was edited. They do not modify public DNS records or affect other users.

Other Use Cases

In addition to testing website migrations, the /etc/hosts file can be used for several other purposes:
  • Local development. You can assign convenient hostnames such as myapp.local to 127.0.0.1 and work with multiple projects on a single machine.
  • Communication between servers. In an internal network, you can assign short hostnames to servers (db1, app1) if no dedicated DNS server is available.
  • Temporary redirection. For example, you can redirect a domain to a test environment or a placeholder server.
  • Troubleshooting. If a website opens by IP address but not by hostname, an entry in hosts helps distinguish a DNS issue from a web server issue.
  • Blocking domains. By mapping an unwanted domain to 127.0.0.1 or 0.0.0.0, you can block access to it locally. For large-scale filtering, specialized tools are more suitable.

How to Modify or Remove an Entry

To change a domain's IP address, open the file again:
sudo nano /etc/hostsFind the required line and replace the IP address.
To remove the redirection completely, delete the line or comment it out by adding the # character:
192.168.1.1 example.com
After saving the file, the system will use normal DNS-based name resolution again.
In most cases, a reboot is not required. However, some applications and services may cache name resolution results, as explained below.

Caching: When Changes Are Not Applied

Changes to the file do not always take effect immediately. This is often caused by caching, which makes a browser or server continue using a previous version of the file.
The systemd-resolved service (used in many modern distributions):
sudo resolvectl flush-cachesThe nscd service, if installed:
sudo nscd -i hostsBrowsers maintain their own DNS caches and may keep connections to the previous server. A complete browser restart or clearing the socket and DNS caches in its internal settings may help. To verify the result, try opening the website in incognito mode or in another browser.
Long-running applications (web servers, proxies, Java applications) sometimes resolve a hostname only once at startup or keep cached results for a specific period. In such cases, you may need to restart or reload the service.

Features and Limitations

When working with the local hosts file, keep the following points in mind:
  • Local scope. Entries apply only to the specific device on which the file is configured.
  • Priority over DNS. With the standard hosts: files dns configuration, local entries are checked before DNS.
  • No automatic updates. If the server's IP address changes, you must update the entry manually. A forgotten entry is a common cause of unexpected issues.
  • Caching. Individual applications and services may cache name resolution results.
  • IPv4 and IPv6. Both protocols are supported. If a domain has an AAAA record in DNS but only an IPv4 address is specified in hosts, the behavior depends on the application and system configuration. Add both entries if necessary.
  • Multiple hostnames. Multiple hostnames can be assigned to one IP address on a single line.
  • No wildcards or ports. Each hostname must be specified separately, and the port is determined by the application.
  • Scalability. For dozens or hundreds of hosts, using a dedicated DNS service is more convenient.
It is not recommended to remove the standard localhost and ::1 entries, as they are used by system services and applications.

The hosts File in Other Operating Systems

The principle is the same across all popular operating systems. Only the file location and the method of flushing the cache differ.
Operating SystemFile LocationDNS Cache Flush Command
Linux/etc/hostssudo resolvectl flush-caches (when using systemd-resolved)
macOS/etc/hostssudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
WindowsC:\Windows\System32\drivers\etc\hostsipconfig /flushdns
On Windows, the file must be edited with administrator privileges, for example, by launching Notepad with elevated permissions.

Security Recommendations

The hosts file takes priority over DNS, making it an attractive target for malware. Modified entries can redirect users to fake websites.
Here are a few recommendations:
  • Periodically review the file contents and look for unfamiliar entries, especially those related to banking services, email providers, and popular websites.
  • Do not grant unnecessary users or processes write permissions to the file.
  • Add comments to your entries indicating the date and reason for their addition, so you can remember their purpose later.
  • Remove temporary entries as soon as you have finished using them.

Conclusion

The /etc/hosts file is a simple and convenient tool for managing local domain name resolution. It allows you to test websites on new servers before changing DNS, temporarily redirect domains to other IP addresses, configure communication between servers, and troubleshoot name resolution issues.
The key points to remember are its local scope, priority over DNS, and the importance of removing temporary entries when they are no longer needed.

VPS in Bulgaria

Browse Configurations

VPS in the Netherlands

Browse Configurations

Windows SSD Storage VPS

Browse Configurations