Ubuntu: How to configure DNS servers in /etc/network/interfaces correctly for resolvconf?
Solution 1
It is correct to put dns-* options in either the iface eth0 inet static
stanza or the iface eth0 inet6 static
stanza. The options become active when the logical interface definition to which they belong becomes active.
I just tested this on my own Ubuntu 15.04 machine which I normally configure with DHCP. I disabled NetworkManager and edited /etc/network/interfaces so that it looks like this:
iface eth0 inet static
address 192.168.178.22
netmask 255.255.255.0
dns-nameservers 8.8.8.8
dns-search foo
iface eth0 inet6 static
address fe80::390:f5ff:fef7:96b9/64
dns-nameservers ::2
dns-search bar
I then did
$ sudo ifup eth0
The results:
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:90:f5:f7:96:b9
inet addr:192.168.178.22 Bcast:192.168.178.255 Mask:255.255.255.0
inet6 addr: fe80::390:f5ff:fef7:96b9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2114609 errors:0 dropped:1 overruns:0 frame:0
TX packets:1757693 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2033346950 (2.0 GB) TX bytes:1318685445 (1.3 GB)
Interrupt:20 Memory:f7e00000-f7e20000
$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver ::2
nameserver 8.8.8.8
search bar foo
Note that the addresses, dns-nameservers and dns-search options from both stanzas are now active.
[Updated 30 May 2015]
Solution 2
On my RPi running Ubuntu 16.04.2 LTS, my findings were as follows:
Note: I uninstalled NetworkManager and just left resolvconf to do its stuff. (This is a fixed server so things don't change much network wise.)
IF I had the dns-search and dns-nameserver lines on only ONE interface (the primary, wired one) but not the other (wlan0) then on reboot, about 95% of the time my /etc/resolv.conf file would be empty.
e.g. When my interfaces file looked like this:
# The primary network interface
auto enxb827ebbfedcf
iface enxb827ebbfedcf inet static
address 192.168.1.16
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-search summer.co.uk summer.vpn
dns-nameserver 192.168.1.16
dns-nameserver 192.168.1.1
# end stanza with clear line+
# The loopback network interface
auto lo
iface lo inet loopback
auto wlan0
iface wlan0 inet static
address 192.168.1.17
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
THEN after a reboot my resolv.conf would look like this:
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
However, on one or two reboots I noticed that it DID have what I wanted, e.g.
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.1.16
nameserver 192.168.1.1
search summer.co.uk summer.vpn
Solution When I gave BOTH interfaces (wired and wlan0) the dns-search and dns-nameserver lines, it all worked fine on every reboot (so far anyway)
i.e. the following interfaces file works for me:
# The primary network interface
auto enxb827ebbfedcf
iface enxb827ebbfedcf inet static
address 192.168.1.16
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-search summer.co.uk summer.vpn
dns-nameserver 192.168.1.16
dns-nameserver 192.168.1.1
# end stanza with clear line+
# The loopback network interface
auto lo
iface lo inet loopback
auto wlan0
iface wlan0 inet static
address 192.168.1.17
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-search summer.co.uk summer.vpn
dns-nameserver 192.168.1.16
dns-nameserver 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
and gives me the following correct resolv.conf after every reboot (so far)
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.1.16
nameserver 192.168.1.1
search summer.co.uk summer.vpn
I assume this duplicate config is normal, if somewhat redundant.
Conclusion: If an interface (other than lo0) lacks dns-search and dns-nameserver information, then resolvconf puts nothing in your resolv.conf file. (It's as if ifup did the wlan0 interface second and ifup wiped resolv.conf or such like.)
I'm not sure if this is a bug or not, feels wrong to me though.
Related videos on Youtube
0xC0000022L
human father bibliophile geek & ~nerd misanthropic philanthropist skeptic code necromancer programmer reverse engineer (RCE) / software archaeologist / grayhat hacker moderator on reverseengineering system administrator FLOSS enthusiast Debian, FreeBSD and Ubuntu aficionado
Updated on September 18, 2022Comments
-
0xC0000022L over 1 year
All examples I have seen of configurations (
/etc/network/interfaces
) showed that you configure an interface and then underneath that gave thedns-*
lines such as:auto eth0 iface eth0 inet static ... dns-nameservers 127.0.0.2 dns-search example.com my.example.com
Now I am aware that the DNS protocol is independent of the transport mechanism. Therefore it shouldn't be per-se wrong to add an IPv6 address on the
dns-nameservers
line.However, since I am also configuring the interface for IPv6 anyway, it made sense to add the
dns-nameservers
line there:iface eth0 inet6 static ... dns-nameservers ::2 dns-search example.com my.example.com
... or did it? Because when
resolvconf
creates/etc/resolv.conf
it simply ignores all the IPv6 DNS servers. It also seems to ignoredns-search
anddns-domain
. The only line it seems to honor is thedns-nameservers
line from the IPv4 configuration foreth0
.What is the correct method of configuring DNS servers?
Further circumstantial evidence as to why it should be part of each respective
iface
stanza. Quote fromman 8 resolvconf
:The
ifup(8)
program can be used to configure network interfaces according to settings in/etc/network/interfaces(5)
. To makeifup
push nameserver information toresolvconf
when it configures an interface, adddns-
lines to the relevantiface
stanza in/etc/network/interfaces
. To add nameserver addresses add a line beginning withdns-nameservers
.-
ztan almost 9 yearsIf you still have this problem then please post your whole /etc/network/interfaces file and the output of /usr/share/resolvconf/dump-debug-info which is included in the resolvconf package in Ubuntu 15.04.
-
-
Anthon over 9 yearsWelcome to Unix & Linux! Whilst this may theoretically answer the question, it would be preferable to include more of the essential parts of that guide here, and provide the link for reference.
-
0xC0000022L over 9 years@MateiCezar: could you please elaborate? I'm not sure you even read my question to the end. Package
resolvconf
is installed. I'd like to/etc/network/interfaces
to configure my DNS servers for IPv4 and IPv6 respectively. -
0xC0000022L over 9 yearsFixed your formatting. Again, my question clearly states that this doesn't result in those DNS servers for IPv6 being added to
/etc/resolv.conf
. If this somehow works for you it'd be useful to know the exact system version. -
Matei Cezar over 9 yearslet me get this straight...you want to use IPv6 DNS resolvers from /etc/network/interfaces and somehow adding them in that file doesn't work for you?
-
0xC0000022L over 9 yearsI can add them to
/etc/network/interfaces
fine. What doesn't happen is that they get transferred into/etc/resolv.conf
as they should. And I am trying to figure out what I am doing wrong, yes. -
Matei Cezar over 9 yearsStill didn't get it? Those DNS entries from /etc/network/interfaces gets transfered to /etc/resolv.conf or not? Usullay those namservers entries from interfaces never get transferred or written to /etc/resolv.com, they work on their own from that NIC and are not implemented system-wide. Just the /etc/resolv.conf file DNS are used by the interfaces who lack dnsnameservers entries.
-
Matei Cezar over 9 yearsThe /etc/resolv.conf file is written by a series of DHCP hook scripts and has nothing to do with /etc/network/interfaces file and the interfaces nameserver values that you add there!
-
0xC0000022L over 9 yearsTo the contrary, it has all to do with the entries in
/etc/network/interfaces
. If you read the documentation, you'll notice that. Check out the other "answer": same issue, no solution. The IPv4 DNS entries get transferred intoresolv.conf
, the rest (i.e. IPv6) does not. This is the issue my question is all about. -
0xC0000022L over 9 yearsCould you elaborate on this part, please: they work on their own from that NIC and are not implemented system-wide. Is there any documentation claiming that? Because so far I haven't come across any. If there was documentation supporting that claim, it would be quite some progress.
-
0xC0000022L about 9 yearswhat you mention in your last sentence is exactly the reason why I don't want to use the resolvconf base config and it's exactly what I am trying. Furthermore it is exactly what is failing.
-
bytelinux com about 9 yearsok..I got it! But why you want to implement things in this manner?either way you only need those DNS nameservers to be accessible system-wide,is there any reason to transfer them from network/interfaces to resolv.conf, when you could just write them in both files and you're done!?
-
0xC0000022L about 9 yearsNot sure how many machines you administrate, but for me it's enough to try and make my life easier. Since
/etc/network/interfaces
is supposed to do the job, I am trying to figure out what I am doing wrong. -
Ricardo Alves almost 4 yearsI can confirm this behavior on a fully patched 18.04. I think it's a bug. Just seeing that your answer is already 3 years old, so it's been in this broken state for years. So that makes me sad.