Adding DNS servers to Ubuntu 18.04

16,308

Solution 1

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

Solution 2

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 too in case it is needed.

Solution 3

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.

Share:
16,308

Related videos on Youtube

Ph055a
Author by

Ph055a

Updated on September 18, 2022

Comments

  • Ph055a
    Ph055a over 1 year

    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.

  • Ph055a
    Ph055a over 5 years
    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.
  • Gannet
    Gannet over 5 years
    Bad suggestion. It is bad idea to delete /etc/resolv.conf symlink. Just use the below answer.
  • Jim
    Jim almost 5 years
    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.