I have multiple internet connections on a linux machine, I want each internet connection to use a specific DNS nameserver. /etc/resolv.conf is a problem, since it uses a global name server for all open internet connections.

Please note that the connections are all open and being used at the same time.


A way to say to linux the following:

Use DNS Nameserver A for internet connection 1

Use DNS Nameserver B for internet connection 2

Use DNS Nameserver C for internet connection 3

The problem is that /etc/resolv.conf uses the same nameserver for all conenections.

The exact requirement above I have to stick to.

Since the application I'm working on measure's ISP performance and DNS response time.

Using OpenDNS wont do me any good.

As for DHCP, I'm already obtaining DNS automatically from the ISP, but only the last DNS servers are being used in resolve.conf overwriting the previous name servers.

Need help.


In essence you have two issues:

  1. default resolver behaviour
  2. packet routing

The first is relatively easily solved if you develop your own test code. End-user programs almost always end up using libresolv and it's the res_init() function therein that's responsible for reading /etc/resolv.conf and setting the DNS servers for subsequent queries by that program. It's very easy to explicitly reset the DNS servers on a per-program instance within your own code.

The second problem requires network level changes. It's likely that you have three default routes, and no specific routing table entries for each ISP. You need to find out which subnet your three ISPs' name servers are in, and then add a route to that subnet that forces the traffic to exit via the right ISP.

For example, if an ISP is using as a DNS server, then you need:

  # route add -net gw w.x.y.z

where w.x.y.z is the address of the next hop facing that particular ISP.

Note that all of this would be a lot simpler if you didn't try to test three ISPs all at the same time from the same machine!

I've done bulk testing of DNS servers and rejected trying to do it with them all connected at once - it's far, far, easier to do them one at a time.

| improve this answer | |
  • 2
    I would vote this up a dozen times if I could. The person asking the question needs to solve their real problem, not their invented problem. – Matt Simmons Aug 8 '09 at 14:24
  • It's a damn crazy question, for sure. :) – David Pashley Aug 8 '09 at 18:52
  • +1 for noting that it would be easier for three machines. If need be, use one physical machines and two virtual machines. – Kevin M Aug 9 '09 at 6:44

Why don't use dig for performing your test?

It would be so easier:

#using local resolver (with cache)
dig +noall +stats www.google.com | grep ";; Query time"
;; Query time: 3 msec

#using opendns
dig +noall +stats google.com @ | grep ";; Query time" 
;; Query time: 28 msec

#using my isp dns
dig +noall +stats google.com @  | grep ";; Query time" 
;; Query time: 20 msec
| improve this answer | |

Typically, the routing decision of "which route to use / which ISP to send a connection via" is done based on destination IP address. You don't have a destination IP address until you make a DNS request to map the destination hostname to a destination IP address.

If your config isn't "typical", you need to provide more details.

Until then, you have a chicken/egg situation. You can't decide which DNS server to use until you know which ISP you're going to be using, and (barring something special which you've not yet described here) you don't know what ISP you'll be using until you've performed the DNS lookup and obtained the destination IP address.

You mention that you are working on an application to measure ISP performance and DNS response time. Your least complex bet may be to monitor one ISP at a time.

More details from you may permit more detailed, relevant answers.

| improve this answer | |

Wouldn't it make more sense to use the DHCP from the ISP and automatically obtain the DNS servers that way instead of hard coding them?

| improve this answer | |

As alternative you can even ignore the DNS servers of your ISP and go with OpenDNS (unless your ISP is silly enough to block DNS requests to external DNS servers).

| improve this answer | |

Maybe resolvconf or openresolv is what you are looking for:

| improve this answer | |

I think Benoit gave you the right answer. You should not try to solve this problem within CentOS. If you write an application for measuring the performances of the internet connection then this application should choose the DNS server (and not CentOS itself).

If you use dig for the performance test or do it directly within your application is up to you. But either way you can specify directly what DNS server you want to take.

Another open question is: How do you do the traffic shaping and routing? Are those solely static?

| improve this answer | |
  • half right - the app needs to pick the DNS server (see my answer) but the O/S has to pick the right network interface to reach that server. – Alnitak Aug 8 '09 at 13:54
  • That's why I asked about the routing and traffic shaping. But, yes, your answer is a lot clearer. (It looks to me that the person who asked the question did not think about it yet. That's why I did not got into details about it.) – Raffael Luthiger Aug 8 '09 at 22:41

You can try putting DNS lines like the following in your /etc/sysconfig/network-scripts/ifcfg-eth{0,1,2} files. I'm not certain this will actually achieve what you want however.

# Networking Interface
| improve this answer | |

Here is what I would do.

1) Set up each ISP on a separate NIC. This allows for easier routing.

2) Setup statics routes that force queries to isp A's dns servers to use isp A's interface/gateway. Repeat for the other isps (B's dns through interface b, etc).

3) Use dig the test the dns servers. The routes you added should make sure that the queries go out the correct interface to the correct isp.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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