16/03/2018
The /etc/hosts file is a simple, yet powerful, text file that plays a significant role in how your computer resolves hostnames to IP addresses. In essence, it acts as a local DNS lookup table, allowing you to manually map domain names to specific IP addresses. While modern operating systems rely heavily on dynamic DNS services, understanding and potentially modifying this file can be crucial for troubleshooting network issues, setting up local development environments, or even enhancing security. This guide will delve into the intricacies of the /etc/hosts file, its functions, and the implications of altering it.

What Exactly Does the /etc/hosts File Do?
At its core, the /etc/hosts file provides a static mapping between hostnames and IP addresses. When your computer needs to access a resource on a network, it first consults this file before querying external DNS servers. This local lookup is incredibly fast and bypasses the standard DNS resolution process. This behaviour can be leveraged in several ways:
- Local Development: Developers often use /etc/hosts to point domain names (e.g.,
myproject.local) to their local machine (127.0.0.1) when building and testing web applications. This allows them to simulate a live server environment on their own computer. - Blocking Websites: You can effectively block access to specific websites by mapping their domain names to an invalid IP address or the loopback address (
127.0.0.1). This is a simple, albeit manual, method for content filtering. - Overriding DNS: In certain scenarios, you might want to force your system to use a specific IP address for a domain, overriding the IP address provided by your DNS server. This could be for testing purposes or to access a server that isn't yet publicly accessible via DNS.
The Interaction with systemd-resolved
In many modern Linux distributions, the systemd-resolved.service is responsible for managing DNS resolution. This service can interact with and, in some cases, overwrite the /etc/hosts file. systemd-resolved synthesizes DNS resource records (RRs) for several key scenarios:
- Local Hostname Resolution: It resolves your system's configured hostname to all its locally configured IP addresses. If no specific IPs are set, it defaults to
127.0.0.2for IPv4 and::1for IPv6. - Localhost Aliases: It ensures that hostnames like
localhostandlocalhost.localdomain, along with any hostname ending in.localhostor.localhost.localdomain, are resolved to the loopback addresses127.0.0.1and::1respectively. - Gateway Resolution: The hostname
_gatewayis resolved to all current default routing gateway addresses. This provides a stable hostname for your network gateway, regardless of IP address changes.
The mappings defined within your /etc/hosts file are generally respected by systemd-resolved for address lookups. However, it's important to note that these local mappings will not affect lookups for other record types, such as Mail Exchanger (MX) records.
Configuring systemd-resolved and /etc/hosts
If you find that your /etc/hosts file isn't behaving as expected, particularly in systems managed by systemd-resolved, there are a few configuration options:
1. Enabling ReadEtcHosts
According to the documentation for /etc/systemd/resolved.conf, you can explicitly instruct systemd-resolved to read the /etc/hosts file. This is achieved by adding the directive ReadEtcHosts=yes to the configuration file. This ensures that your custom mappings are prioritised.
2. Disabling systemd-resolved
For users who prefer to manage DNS resolution entirely through traditional methods or other services, disabling systemd-resolved is an option. This is a more advanced step and requires careful consideration. Resources like "How to disable systemd-resolved in Ubuntu?" can provide detailed instructions.
3. Using Local DNS Servers (e.g., dnsmasq)
On systems like Ubuntu, services like dnsmasq are often used for local DNS caching and forwarding. You can configure dnsmasq to resolve specific domains through your local DNS server or to integrate with the /etc/hosts file in a more granular way.
Potential Causes of Issues
Beyond systemd-resolved, other system services can influence how /etc/hosts is interpreted or managed:
- NetworkManager: In some distributions, NetworkManager can also play a role in network configuration and DNS resolution. Issues with NetworkManager could indirectly affect how your /etc/hosts file is used.
- cloud-init: For cloud-based instances (e.g., Ubuntu servers deployed in the cloud), the cloud-init service is responsible for initial system configuration. Bug reports on platforms like Launchpad have indicated that cloud-init can sometimes cause unexpected behaviour related to DNS and host file management.
What Happens if I Change /etc/hosts?
Modifying the /etc/hosts file can have immediate and significant effects on your network connectivity. Here's a breakdown of what can happen:
Successful Changes:
- Website Access: You can direct traffic to specific IP addresses, for example, to access a test server or a mirrored website.
- Blocking: As mentioned, you can prevent access to certain websites by mapping them to
127.0.0.1. - Local Development: Setting up custom domains for local projects becomes straightforward.
Potential Issues and What to Avoid:
It's crucial to be cautious when editing /etc/hosts, especially if you are connected remotely via SSH. An incorrect entry could sever your connection, leaving you unable to revert the changes without physical access to the machine.
Disclaimer: If you are connected via SSH, it is strongly advised NOT to make changes to your network configuration files, including /etc/hosts, without having a backup plan or alternative access method. A common troubleshooting step that can sometimes resolve network issues, but should be done with extreme caution and only if you understand the implications, involves temporarily disabling and re-enabling your network interface:
# Substitute eth0 with your actual network interface name sudo ifconfig eth0 down sudo ifconfig eth0 upHowever, this command is more about resetting the network interface state and less about directly fixing /etc/hosts issues. The core principle remains: edit with care.

Best Practices for Editing /etc/hosts
To ensure a smooth experience when working with the /etc/hosts file, follow these best practices:
- Backup First: Always create a backup of your /etc/hosts file before making any modifications. You can do this with a simple command like:
sudo cp /etc/hosts /etc/hosts.backup - Use a Reliable Editor: Use a text editor with root privileges (e.g.,
sudo nano /etc/hostsorsudo vim /etc/hosts) to edit the file. - Understand the Format: Each line in /etc/hosts follows a specific format:
IP_Address Hostname [Alias1 Alias2 ...]. Ensure your entries are correctly formatted. - Test Carefully: After making changes, test your network connectivity thoroughly. Use commands like
pingto verify that hostnames resolve to the intended IP addresses. - Keep it Clean: Remove any entries you no longer need to avoid potential conflicts or confusion.
Common Entries in /etc/hosts
Here's a look at some typical entries you might find or want to add:
| IP Address | Hostname | Description |
|---|---|---|
| 127.0.0.1 | localhost | The primary loopback address for the local machine. |
| ::1 | localhost | The IPv6 loopback address. |
| 127.0.0.1 | mydevsite.local | Maps a custom domain to the local machine for development. |
| 127.0.0.1 | blockedsite.com | Blocks access to 'blockedsite.com' by redirecting it to localhost. |
| 192.168.1.100 | myserver | Assigns a static IP to a local network server. |
Frequently Asked Questions (FAQs)
Q1: Can I edit /etc/hosts without root privileges?
No, the /etc/hosts file is a system file and requires root (administrator) privileges to modify. You'll typically use commands like sudo nano /etc/hosts or sudo vim /etc/hosts.
Q2: Will changes to /etc/hosts affect other computers on my network?
No, the /etc/hosts file is specific to the computer on which it is edited. It does not influence how other computers resolve hostnames.
Q3: What happens if I accidentally delete /etc/hosts?
If you delete the /etc/hosts file, your system will likely still function as it can fall back to using DNS servers. However, you will lose the ability to perform local hostname resolutions and overrides. It's advisable to restore the file with its default entries (usually just the localhost mappings) as soon as possible.
Q4: How do I revert changes made to /etc/hosts?
If you made a backup, you can restore it using sudo cp /etc/hosts.backup /etc/hosts. If not, you can manually recreate the default entries for localhost.
Q5: Is it safe to use /etc/hosts to block websites?
It's a simple method for personal use, but it's not a robust security solution. Advertisers and malicious sites can employ various techniques to bypass these local restrictions. For comprehensive ad-blocking or parental controls, dedicated software or browser extensions are more effective.
Conclusion
The /etc/hosts file is a fundamental component of network name resolution. While its role might seem diminished with the prevalence of dynamic DNS, it remains an invaluable tool for local development, network troubleshooting, and system customization. By understanding its functionality and adhering to best practices, you can effectively leverage the power of the /etc/hosts file while avoiding potential pitfalls, particularly when working remotely.
If you want to read more articles similar to Understanding /etc/hosts: A Guide, you can visit the Automotive category.
