4

There is no less than 3 different ways to add DNS servers posted all over the internet. I have been reading netplan documentation and am I still not clear on how to add dns servers to a laptop. I am in a country with censorship/surveillance and I am using a VPN that is leaking DNS information.

say what you will, at least adding nameservers to resolv.conf was simple. Here is the best example of configuring DNS with a wifi connection that I have found. https://netplan.io/examples

network:
  version: 2
  renderer: networkd
  wifis:
    wlp2s0b1:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.0.21/24]
      gateway4: 192.168.0.1
      nameservers:
        addresses: [192.168.0.1, 8.8.8.8]
      access-points:
        "network_ssid_name":
          password: "**********"

This examples makes it seem that I need to configure every access point I go to, I'm really hoping that's not true. Can one of you smart people please post a realistic example for home/small business user.

Are we really expecting desktop users to write .yaml files now? I just want to put nameservers in a file and be done with it.

Thank you.

2

The only way that I've found to have the changes be persistent across all networks and interfaces (that are using dhcp that is) is to add the following line to /etc/dhcp/dhclient.conf:

supersede domain-name-servers 8.8.8.8,8.8.4.4 # replace with wanted DNS server

1

The default netplan renderer on laptops is NetworkManager, not networkd. You would therefore normally configure your DNS preferences through nm-applet rather than by editing netplan yaml.

However, it's quite unusual to want to specify an override DNS server for all configured wifi connections; the common case is to use the DNS information provided by the DHCP server and override it only for exceptional cases. So there is no optimized way to override the DNS servers for all available wifi connections.

To point at a different DNS server than the ones automatically configured via your network connections, you can rm the /etc/resolv.conf symlink and replace it with a real file containing the contents you want.

  • 1
    Thank you, but can you please expand your answer? I want this thread to be a clear and concise updated way of adding DNS servers to Ubuntu for all to see. Clear instructions on how to add DNS servers to Ubuntu and I would like it to be clear to people who are not system engineers and stress the importance on clarity here. Please understand the implications, Some people live in a countries with censorship. Using Network Manager doesn't seem to do anything, the dns servers just get deleted and editing the symlink seems to only send me down more rabbit holes with different answers. – Ph055a Aug 22 '18 at 2:29
  • Bad suggestion. It is bad idea to delete /etc/resolv.conf symlink. Just use the below answer. – Gannet Jan 7 at 23:13
  • I've been reading docs all over the net, too and everybody mentions using the "applet" #()U*(@#E()! Remember that there are people out there who don't use the GUI for every little thing. I have YET to find someone address the proper way to add dns nameservers properly using CLI without editing /etc/resolv.conf which boldly states DO NOT DO THAT. It would REALLY be nice if people would stop suggesting only GUI helps given the fact that Linux is not windows. It has two distinct environments that should be addressed contextually accurately. – Jim May 10 at 22:26
1

I have YET to find someone address the proper way to add dns nameservers properly using CLI without editing /etc/resolv.conf which boldly states DO NOT DO THAT.

Ok. I think I figured it out.

First of all /etc/resolv.conf is just symlink to ../run/systemd/resolve/stub-resolv.conf. And comment that states to not edit it is about ../run/systemd/resolve/stub-resolv.conf, not the /etc/resolv.conf.

Same comment also suggest to replace /etc/resolv.conf symlink with static file. So I suppose the answer is:

sudo rm /etc/resolv.conf #remove the file (symlink)

echo "nameserver 1.1.1.1" | sudo tee -a /etc/resolv.conf #creates new, static file and puts dns server adress in it. You can repeat this line with different attributes to add spare adresses I guess.

echo "options edns0" | sudo tee -a /etc/resolv.conf #adds this line at the end of said file. Don't know what it does but it exist in my original file so I put this here to in case it is needed.

0

Another way (avoiding to touch the /etc/netplan/ files) would be to edit the file /etc/systemd/resolved.conf if such a file exists in your machine. For example, initially it should be

[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

and you can make it like so (first issue the commands:

systemctl status systemd-resolved.service

systemd-resolve --status

to see how things are before you do your changes)

[Resolve]
DNS=192.168.10.100 192.168.10.105
FallbackDNS=8.8.8.8 9.9.9.9
Domains=example.com
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

and then do the:

sudo systemctl restart systemd-resolved.service

and check your results with

systemd-resolve --status

Useful information can be found in the (ugly and not human friendly, as usual with linux man pages, all man pages are absolutely horrible)

man 5 resolved.conf
man 5 resolv.conf

Also be sure that only one service regarding resolve is active, you can have a look by

systemctl | grep resolv

and you should see the systemd-resolved.service only . If you see also the older-style resolvconf then you better stop+disable it, or even remove the package and its config files with

sudo apt-get purge resolvconf

before doing any of the above proposed edits

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.