Ubuntu – How to put 127.0.0.1 ABOVE in /etc/resolv.conf with ubuntu + networkmanager + DHCP

dhcpnetworkingUbuntu

Actually, on a ubuntu 16.04 server, my /etc/resolv.conf is :

# 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 213.186.33.99
search local

I want to put "nameserver 127.0.0.1" at the top.
I modified my network config file to:

auto lo
iface lo inet loopback

auto ens3
iface ens3 inet dhcp
    dns-nameservers 127.0.0.1      <--- added this

but the nameserver 127.0.0.1 has been set at the bottom :

# 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 213.186.33.99
nameserver 127.0.0.1
search local

I modified /etc/dhcp/dhclient.conf to ask to prepend 127.0.0.1 :

prepend domain-name-servers 127.0.0.1;

But now, the nameserver dynamically found by DHCP has disappeared :

# 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 127.0.0.1
search local

So how can I have "nameserver 127.0.0.1" at the top AND the nameserver dynamically found by DHCP bellow ?

Best Answer

As mentioned in the comment line, the file is generated by resolvconf(8). It is configured with resolvconf.conf(5):

name_servers

Prepend name servers to the dynamically generated list. You should set this to 127.0.0.1 if you use a local name server other than libc.

And the options that affects the processing order:

interface_order

These interfaces will always be processed first. If unset, defaults to the following:- lo lo[0-9]*

dynamic_order

These interfaces will be processed next, unless they have a metric. If unset, defaults to the following:- tap[0-9]* tun[0-9]* vpn vpn[0-9]* ppp[0-9]* ippp[0-9]*