Programmatically set DNS nameserver on Linux

4.7k views Asked by At

I'd like to be able to add IP addresses for DNS nameservers on Linux from my C/C++ program. I'm on a somewhat embedded platform with a read-only /etc/resolv.conf. This means I can't simply add a "nameserver xxx.xxx.xxx.xxx" line to the file without a little trickery. Are there any clean ways to do this from code?

I could try symlinking /etc/resolv.conf to a file in tmpfs, but that seems hackish and it feels like something I should be able to do without writing to a file.

1

There are 1 answers

4
aaz On BEST ANSWER

Have a look at nsaddr_list in resolv.conf from your libc.

This question says:

Although not documented, the common way to set the resolver used is to update _res.nsaddr_list.

In uClibc 0.9.31 specifically there is a comment on line 533 in resolv.c describing how this works.