Many Linux administrators wonder if simple round-robin DNS can be achieved through /etc/hosts entries like:
192.168.4.10 shaakunthala.local
192.168.4.20 shaakunthala.local
While this configuration is technically valid, it doesn't provide true round-robin functionality. The /etc/hosts file has fundamental limitations:
- Entries are processed sequentially from top to bottom
- The system will always use the first matching IP address
- No native load-balancing mechanism exists
The Linux resolver library (glibc) implements a first-match strategy for /etc/hosts. When an application requests resolution for shaakunthala.local:
- The resolver scans /etc/hosts line by line
- Upon finding the first matching hostname (192.168.4.10), it returns that IP
- Subsequent identical hostname entries are ignored
For developers needing simple load distribution on a Linux system, consider these approaches:
1. DNSMasq Local Round-Robin
Configure dnsmasq as a local caching DNS server with round-robin support:
# /etc/dnsmasq.conf
address=/shaakunthala.local/192.168.4.10
address=/shaakunthala.local/192.168.4.20
rr-hostname=shaakunthala.local
2. Systemd-Resolved with Multiple A Records
Modern Linux systems using systemd-resolved can be configured with:
# /etc/systemd/resolved.conf
[Resolve]
DNS=192.168.4.10 192.168.4.20
Domains=~shaakunthala.local
3. Application-Level Solutions
For Python applications, you could implement a simple resolver:
import random
hosts = ["192.168.4.10", "192.168.4.20"]
def resolve_host():
return random.choice(hosts)
For production environments requiring true round-robin DNS:
- Configure BIND or PowerDNS with multiple A records
- Use cloud DNS services like Route53 or Cloudflare
- Consider dedicated load balancers (HAProxy, Nginx) for more control
No, you cannot implement true round-robin DNS functionality solely through the /etc/hosts
file in Linux. The hosts file doesn't support DNS load balancing features, and most systems will simply use the first matching entry they find.
The hosts file is a simple text-based DNS resolver that takes precedence over actual DNS queries. When you define multiple IP addresses for the same hostname:
192.168.4.10 shaakunthala.local
192.168.4.20 shaakunthala.local
Most systems will:
- Use the first matching entry (192.168.4.10 in this case)
- Ignore subsequent entries with the same hostname
- Not perform any rotation or load balancing
For actual round-robin functionality, consider these approaches:
1. Local DNS Server (Recommended)
Setup a local DNS server like dnsmasq or BIND:
# dnsmasq configuration example
address=/shaakunthala.local/192.168.4.10
address=/shaakunthala.local/192.168.4.20
rr-set-order=random
2. Application-Level Load Balancing
Implement rotation in your application code:
# Python example
import random
servers = ['192.168.4.10', '192.168.4.20']
selected_server = random.choice(servers)
3. Using nginx as Reverse Proxy
# nginx configuration
upstream backend {
server 192.168.4.10;
server 192.168.4.20;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
To verify how your system handles multiple entries:
ping shaakunthala.local
dig shaakunthala.local
nslookup shaakunthala.local
You'll typically see consistent results pointing to the first IP, not alternating between them.
Understanding these limitations helps prevent:
- False assumptions about load balancing
- Configuration errors in production environments
- Unexpected single points of failure