Thursday, February 6, 2025

How To Safely Edit Hosts File In Linux: A Beginners Guide

https://ostechnix.com/edit-hosts-file-in-linux

How To Safely Edit Hosts File In Linux: A Beginners Guide

Have you ever wanted to test a website locally, block annoying ads, or create shortcuts for devices on your network? The Linux hosts file is a powerful tool that can help you do all this and more! Located at /etc/hosts, this simple text file lets you map hostnames to specific IP addresses, giving you control over how your system resolves domain names. In this guide, we will learn why and how to safely edit the hosts file in Linux, along with real-world examples.

What is the Hosts File?

The /etc/hosts file is a local text file used by the operating system to map hostnames to IP addresses before querying a DNS (Domain Name System) server. It provides a way to override DNS resolution for specific domain names.

Why Edit the Hosts File?

  1. Local Development: Developers use it to point domain names to local servers (e.g., 127.0.0.1 example.com).
  2. Block Websites: You can redirect unwanted domains to 0.0.0.0 or 127.0.0.1 (loopback address) to prevent access.
  3. Network Troubleshooting: You can bypass DNS to test connectivity to a specific server.
  4. Custom Domain Mapping: You can assign friendly names to IP addresses in a private network.
  5. Speeding Up Access to Websites: The hosts file is checked before the internet’s DNS system. If a website is in your hosts file, your computer doesn’t have to look it up online, making it load faster.

Precautions When Editing /etc/hosts File

  • Do not remove existing system entries like 127.0.0.1 localhost.
  • Ensure there are no duplicate entries for the same hostname.
  • If a hostname is defined in /etc/hosts, it will override DNS resolution.

I’ll break down each precaution in simple terms so you can understand why they matter.

1. Do Not Remove Existing System Entries Like 127.0.0.1 localhost

Your system relies on 127.0.0.1 localhost for internal processes. Removing or modifying this line can cause software or system services to break.

Example of a default /etc/hosts entry:

127.0.0.1   localhost
::1         localhost

Reason:

Many programs, including servers and networking tools, assume localhost always maps to 127.0.0.1. If this is missing, some software may fail to work properly.

2. Ensure There Are No Duplicate Entries for the Same Hostname

If you add the same hostname multiple times with different IP addresses, your system might get confused.

Example of a bad /etc/hosts file:

127.0.0.1   mywebsite.local
192.168.1.100 mywebsite.local

Reason:

The system will use the first entry it finds, and the second one will be ignored. This can cause unexpected behavior when trying to reach mywebsite.local.

3. If a Hostname Is Defined in /etc/hosts, It Will Override DNS Resolution

The /etc/hosts file is checked before the system queries external DNS servers. If a domain is listed in /etc/hosts, the system will use the IP address from this file, even if a different address is available in public DNS.

Example:

127.0.0.1   example.com
  • Normally, example.com resolves to an IP address from a DNS server.
  • With this entry, your computer will always resolve example.com to 127.0.0.1, regardless of the real IP address.

Reason:

If you accidentally override an important hostname, you might block access to legitimate websites or services.

Key Takeaways:

  • Always backup the file before editing.
  • Never remove or change system default entries.
  • Avoid duplicate entries to prevent confusion.
  • Understand that /etc/hosts overrides DNS, which can affect website access.

How to Edit the Hosts File in Linux

1. Backup the Hosts File

Backing up the /etc/hosts file before making changes is a good practice. If something goes wrong, you can easily restore the original file.

Let us make a backup of /etc/hosts file using command:

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

This creates a copy named hosts.bak in the same directory.

2. Open the Hosts File

Since /etc/hosts is a system file, editing it requires root privileges.

Use a text editor like nano or vim:

sudo nano /etc/hosts

or

sudo vim /etc/hosts

3. Understand the hosts File Format

Each entry in /etc/hosts follows this format:

<IP Address> <Hostname> [Alias]

Example:

127.0.0.1   localhost
192.168.1.100  myserver.local myserver
  • 127.0.0.1 is mapped to localhost (default).
  • 192.168.1.100 is assigned to myserver.local, with myserver as an alias.

4. Adding a Custom Domain

To map a domain to a local server:

127.0.0.1   mywebsite.local

Now, when you access mywebsite.local, it will resolve to 127.0.0.1 (localhost).

5. Blocking a Website

To block a website (e.g., example.com):

0.0.0.0 www.example.com

or,

127.0.0.1 www.example.com

This prevents access to www.example.com by redirecting it to a non-routable address.

6. Save and Exit

  • In Nano, press CTRL + X, then Y, and hit Enter.
  • In Vim, press ESC, type :wq, and hit Enter.

7. Flush DNS Cache (if needed)

Some Linux distributions cache DNS lookups. To apply changes immediately, clear the DNS cache:

sudo systemctl restart systemd-resolved

or for nscd:

sudo systemctl restart nscd

Related Read: How To Clear Or Flush DNS Cache In Linux

8. Verify Changes

Test the changes using command:

ping mywebsite.local

or

getent hosts mywebsite.local

9. Restore from Backup (if needed)

If something breaks, you can restore the original file:

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

10. Verify the File Integrity

After restoring, check if the file has the correct entries:

cat /etc/hosts

or

getent hosts localhost

Conclusion

In this step-by-step tutorial, we explained how to safely edit hosts file in Linux. By editing the hosts file, you gain fine-grained control over hostname resolution, which can be useful for development, testing, and network management.

 

No comments:

Post a Comment