THE PROBLEM
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.
WHAT I NEED?
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:
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 theres_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 192.168.1.1 as a DNS server, then you need:
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.
Why don't use dig for performing your test?
It would be so easier:
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.
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?
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).
Maybe resolvconf or openresolv is what you are looking for:
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?
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.
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.