Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a web sever that connects to an internal database through a VPN. There are 2 IPs (primary and secondary) to the database from the web server. How can I setup my /etc/hosts file so that if the primary IP is not available then the secondary IP will be used?

Would this work for my hosts file?   #primary   #secondary
share|improve this question

The hosts file does not provide such mechanism. If you list two IPs for the same name, only the first one will be used. So, there is no such thing primary and secondary.

Also, the hosts file does not handle URLs. It just handles names like the oncs provided in the question. A URL contains complete path and protocol such as http//host/path/to/resource.

share|improve this answer
Edited title and naming in OPs question to eliminate the confusion about URLs/hosts. – dmourati Jan 30 '13 at 17:56

You can't provide resilience or round robin load balancing via the /etc/hosts file - it is not designed for that purpose.

Instead, your options are ... (in no particular order)

  1. Configure your network properly, so that routes change when a link is dropped
  2. Use DNS round-robin load balancing (not A Good Idea TM) using a managed service (eg. or etc.)
  3. Use a local L3 load balancer for the outbound traffic (HAProxy?) with the back-ends defined as necessary
  4. Build the resilience into your web application itself
share|improve this answer
DNS round-robin load balancing isn't usually resilient. One is selected and others are not tried. – anttiR Sep 19 '12 at 17:09
Another option could be to use netcat or another software to forward the connection to an IP. Then change the forward if one IP is lost. – anttiR Sep 19 '12 at 17:11
@anttiR DNS RR alone has no resilience, but used via a managed DNS service provider it does. I've edited my answers and given some examples to be clearer. – Ben Lessani - Sonassi Sep 19 '12 at 17:19
I doubt it would work great with database. They have the tendency to fetch one IP and stick to it. An internet website on the other hand would work great. – anttiR Sep 19 '12 at 17:30
That would depend on the resolver of the host machine. If the DNS resolver is set to be a non-caching service - or polls the DNS registrars DB directly, then it would work. But like I said, its not a good idea, its just an idea. – Ben Lessani - Sonassi Sep 19 '12 at 17:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.