Hosts File

Managing /etc/hosts for local DNS lookups, hostname mapping, and network configuration

/etc/hosts File Management

Topic Overview

The /etc/hosts file is a local DNS lookup table that maps hostnames to IP addresses. It allows system administrators to override DNS resolution locally, providing faster lookups and network control without external DNS queries.

Key Concepts

  • Static DNS: Local hostname-to-IP mapping
  • DNS Override: Takes precedence over external DNS
  • Loopback: 127.0.0.1 maps to localhost
  • FQDN: Fully Qualified Domain Names supported
  • IPv4/IPv6: Both address types supported

File Format

1
IP_address hostname [alias1] [alias2] ...
  • One entry per line
  • Whitespace separates fields
  • Comments start with #

File Location

  • Linux/Unix: /etc/hosts
  • Windows: C:\Windows\System32\drivers\etc\hosts
  • Permissions: Root/admin access required

Practical Examples

Example 1: View current hosts file

1
2
3
4
5
cat /etc/hosts
# Output:
127.0.0.1   localhost
127.0.1.1   mycomputer
::1         localhost ip6-localhost

Standard default entries for local system

Example 2: Add custom mapping

1
2
3
4
sudo nano /etc/hosts

# Add line:
192.168.1.100   webserver web.local

Maps webserver and web.local to 192.168.1.100

Example 3: Block unwanted sites

1
2
3
4
5
sudo nano /etc/hosts

# Add blocking entries:
0.0.0.0   ads.example.com
0.0.0.0   tracker.site.com

Redirects unwanted domains to nowhere

Example 4: Development environment

1
2
3
4
5
6
sudo nano /etc/hosts

# Development mappings:
127.0.0.1   dev.mysite.com
127.0.0.1   api.mysite.com
127.0.0.1   admin.mysite.com

Local development server mappings

Use Cases

  • Local Development: Test sites locally
  • Network Troubleshooting: Override DNS temporarily
  • Ad Blocking: Redirect unwanted domains
  • Internal Networks: Map internal server names
  • DNS Bypass: Quick hostname resolution

ping hostname - Test hostname resolution nslookup hostname - Check DNS resolution dig hostname - Detailed DNS lookup host hostname - Simple hostname lookup getent hosts hostname - System resolver lookup

File Management Commands

Backup hosts file

1
sudo cp /etc/hosts /etc/hosts.backup

Edit safely

1
2
3
sudo nano /etc/hosts
# or
sudo vim /etc/hosts

Validate entries

1
2
getent hosts webserver
ping -c1 webserver

Tips & Troubleshooting

Common Issues:

  • No effect: Check file permissions and syntax
  • Typos: Validate IP addresses and hostnames
  • Cache: Some apps cache DNS, restart if needed
  • Order matters: First match wins

Best Practices:

  • Always backup before editing
  • Use comments to document entries
  • Keep entries organized by purpose
  • Test changes with ping or getent

Security Notes:

  • Protect from unauthorized modification
  • Monitor for malicious entries
  • Regular audits recommended
  • Consider file integrity monitoring

Performance:

  • Large hosts files slow resolution
  • Keep entries relevant and current
  • Remove unused mappings regularly