Mapping Hostnames with Ports in /etc/hosts

The /etc/hosts file is a simple text file used to map hostnames to IP addresses locally on a system. It provides hostname resolution by bypassing DNS servers, allowing administrators to define custom IP-to-hostname mappings. Each line represents a single mapping with the IP address followed by one or more hostnames separated by spaces.

Understanding the Hosts File Structure

The hosts file is located at /etc/hosts on Unix-like systems and C:\Windows\System32\drivers\etc\hosts on Windows. It follows a simple format where each line contains an IP address followed by hostnames:

192.168.1.100 myserver.local
127.0.0.1 localhost
::1 localhost

When an application attempts to resolve a hostname, the system checks the hosts file first before querying DNS servers. This makes it useful for local development, testing, and basic hostname management.

Common Misconception About Ports in Hosts File

Important: The /etc/hosts file cannot directly map hostnames with specific ports. Entries like 192.168.1.100 example.com:8080 do not work as expected because the hosts file only resolves hostnames to IP addresses, not ports.

If you add this line to your hosts file:

192.168.1.100 example.com:8080

The system treats example.com:8080 as the literal hostname, not as example.com on port 8080. This means you would need to type http://example.com:8080 in your browser to match this entry, which defeats the purpose.

Proper Hostname Mapping Examples

Here are correct ways to use the hosts file for local development:

# Map multiple hostnames to same IP
192.168.1.100 dev.example.com staging.example.com

# Block websites by redirecting to localhost
127.0.0.1 unwanted-site.com

# Map internal services
10.0.0.50 database.internal
10.0.0.51 api.internal

Alternative Solutions for Port-Specific Routing

Using Reverse Proxies

A reverse proxy is the proper solution for routing requests based on hostnames to different backend services running on specific ports. Popular options include:

  • Nginx Can route api.example.com to localhost:8080

  • Apache HTTP Server With mod_proxy for request forwarding

  • HAProxy Load balancer with hostname-based routing

Example Nginx Configuration

server {
    listen 80;
    server_name api.example.com;
    
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
    }
}

With this setup, you add 127.0.0.1 api.example.com to your hosts file, and Nginx handles routing to port 8080.

Comparison of Approaches

Method Supports Ports Complexity Use Case
Hosts File Only No Simple Basic hostname mapping
Reverse Proxy Yes Moderate Production routing, load balancing
Application-Level Proxy Yes High Complex routing logic

Best Practices

  • Keep it simple Use hosts file for basic hostname-to-IP mapping only

  • Document entries Add comments using # to explain mappings

  • Use reverse proxies For port-specific routing requirements

  • Remember scope Hosts file changes are local to the specific machine

Conclusion

The /etc/hosts file is designed for simple hostname-to-IP address mapping and cannot directly handle port specifications. For routing hostnames to specific ports, use reverse proxies like Nginx or Apache, which provide the flexibility to map hostnames to backend services running on different ports while maintaining clean URLs.

Updated on: 2026-03-17T09:01:38+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements