In classic Unix/Linux systems, we typically see hostname resolution configured in /etc/hosts like this:
127.0.0.1 localhost myhostname
However, modern Debian-based distributions (including Ubuntu/Xubuntu) implement a different approach:
127.0.0.1 localhost
127.0.1.1 myhostname
The separation serves several technical purposes:
- Name Resolution Order: DNS lookups prioritize 127.0.0.1 for 'localhost' while using 127.0.1.1 for the actual hostname
- Systemd Compatibility: Maintains compatibility with systemd's hostnamed service
- Network Configuration: Helps distinguish between purely local services and those bound to the hostname
While both configurations ultimately resolve to loopback addresses, the separation has real consequences:
# Test with curl to see the difference
curl http://localhost # Uses 127.0.0.1
curl http://$(hostname) # Uses 127.0.1.1
This becomes important when:
- Configuring web servers with virtual hosts
- Setting up container networking
- Developing applications that bind to specific interfaces
You might want to modify this setup in these scenarios:
# For systems without permanent network connectivity
127.0.0.1 localhost
127.0.0.1 myhostname
# For multi-homed systems
127.0.0.1 localhost
192.168.1.100 myhostname
If you experience resolution problems, check:
- The output of
hostname -f
matches your /etc/hosts entry - No conflicting entries exist in /etc/hosts
- The hostname resolves correctly with
getent hosts $(hostname)
Remember that systemd-based systems may also check /etc/hostname and use systemd-resolved
for additional resolution.
In most Unix-like systems, the default /etc/hosts
file typically contains:
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
This maps both IPv4 (127.0.0.1) and IPv6 (::1) loopback addresses to 'localhost'. Many administrators manually add their hostname to this line:
127.0.0.1 localhost myhostname
Debian-based systems (including Ubuntu/Xubuntu) use a different convention:
127.0.0.1 localhost
127.0.1.1 myhostname
This separation serves several technical purposes:
The primary reasons for this design choice are:
- Network Interface Binding: Some services bind specifically to 127.0.0.1. Using a separate address prevents potential conflicts.
- DNS Fallback: When the system has no other network configuration, this ensures hostname resolution still works.
- Historical Compatibility: Early Debian systems used this approach for systems without permanent IP addresses.
In most cases, both configurations will work similarly. However, consider these scenarios:
# Test hostname resolution
ping $(hostname) # Should return 127.0.1.1 in Debian systems
For applications that specifically check IP addresses:
# Python example checking localhost
import socket
print(socket.gethostbyname('localhost')) # Returns 127.0.0.1
print(socket.gethostbyname(socket.gethostname())) # Returns 127.0.1.1
You might want to change this if:
- Running services that validate reverse DNS lookups
- Working with containers that expect different resolution
- Debugging network-related issues
To merge the entries (traditional style):
# Edit /etc/hosts
127.0.0.1 localhost myhostname
::1 localhost ip6-localhost ip6-loopback
Modern Linux systems using systemd may override these settings. Check:
systemd-resolve --status
# Or for older systems:
cat /etc/nsswitch.conf
Remember to restart networking after changes:
sudo systemctl restart systemd-networkd