일. 8월 3rd, 2025

What Is the Hosts File?

The /etc/hosts file is a simple text file that maps hostnames (like example.com) to IP addresses on your Linux system. It predates DNS (Domain Name System) and allows you to override DNS lookups locally. Think of it as your computer’s personal address book!


Key Use Cases

  1. Block Websites
    Redirect unwanted domains (e.g., social media) to 127.0.0.1 (your local machine).
  2. Test Websites Locally
    Point a domain to your development server’s IP before DNS updates.
  3. Access Servers with Unresolved Domains
    Map IPs to custom hostnames (e.g., 192.168.1.100 my-server.local).

Where to Find the Hosts File

Location: /etc/hosts


How to Edit the Hosts File

Step 1: Open a Terminal

Use Ctrl+Alt+T or search for “Terminal”.

Step 2: Edit with Superuser Privileges

Use a command-line text editor (like nano):

sudo nano /etc/hosts

You’ll need your admin password.

Step 3: Add Your Entries

Syntax:

IP_address   hostname_or_domain   [optional_aliases]  

Examples:

  • Block a website:
    127.0.0.1   facebook.com  
  • Map a domain to a local server:
    192.168.1.20   staging.myapp.com  
  • Create a shortcut for a local device:
    192.168.1.5   printer.local  

Step 4: Save & Exit

  • In Nano: Press Ctrl+OEnter to save, then Ctrl+X to exit.

Testing Your Changes

  1. Flush DNS Cache (if needed):
    sudo systemd-resolve --flush-caches  # For systemd-based systems
    # Or
    sudo service nscd restart           # Older systems
  2. Verify with ping:
    ping example.com

    If blocked, you’ll see replies from 127.0.0.1.


Important Notes

  1. Security:
    • The hosts file only affects your machine.
    • It’s not a substitute for firewalls or DNS security tools.
  2. Syntax Rules:
    • One entry per line.
    • Comments start with #.
    • Use 127.0.0.1 for IPv4 or ::1 for IPv6.
  3. Permission Issues:
    • Always use sudo to edit—it’s a system-protected file.

Troubleshooting

  • Changes not working?
    • Check for typos in IPs/hostnames.
    • Clear your browser cache or use private browsing.
    • Restart networking: sudo systemctl restart NetworkManager.

Why Use Hosts Over DNS?

  • Instant local overrides without DNS configs.
  • No internet required for custom mappings.
  • Perfect for development/testing.

Final Thoughts

The hosts file is a lightweight, powerful tool for managing network connections on Linux. Use it wisely to control how your system resolves domains—whether for productivity, testing, or local networking!

> Pro Tip: Back up /etc/hosts before editing:
> bash > sudo cp /etc/hosts /etc/hosts.bak >

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다