5

How do I change my DNS server globally without having to do it manually through network manager for every connection I have.

I want to use OpenDNS, whether its at home, university, wifi hotspot, phone tether...

I use Ubuntu 13.10 but I guess it will be the same on newer versions.

rubo77
  • 32,486
Abdul Hamdy
  • 51
  • 1
  • 4
  • 1
    @Paul: your url changed to https://help.ubuntu.com/lts/serverguide/network-configuration.html#name-resolution – rubo77 Apr 28 '15 at 18:46
  • This is not a duplicate, but it will show how to change dns for one connection: https://askubuntu.com/questions/2321/what-is-the-proper-way-to-change-the-dns-ip – rubo77 Apr 28 '15 at 20:05
  • @rubo77 Thanks. I deleted the comment since it had no other information. – Paul Apr 29 '15 at 23:33

4 Answers4

5

You can add this file so network manager will update resolv.conf each time a connection is established:

echo "echo 'nameserver 85.214.20.141'>/etc/resolv.conf"> /etc/NetworkManager/dispatcher.d/10-FoeBud-dns
chmod +x /etc/NetworkManager/dispatcher.d/10-FoeBud-dns

(I would suggest the dns 85.214.20.141 (FoeBud))

Evaluation:

watch cat /etc/resolv.conf

Then restart networkmanager in another console with

service network-manager restart

... watch changing the resolv.conf file.

Drawback:

The local DNS caching on your machine by dnsmasq will not be available any more.


I also tried:

echo "nameserver 85.214.20.141">/etc/resolv.dnsmasq.conf
echo "resolv-file=/etc/resolv.dnsmasq.conf" > /etc/NetworkManager/dnsmasq.d/FoeBud-dns

which seemed somewhat cleaner or a shorter option would be:

echo "server=85.214.20.141" > /etc/NetworkManager/dnsmasq.d/FoeBud-dns

But both options don't seem to work, because Network-Manager does start his own instance of dnsmasqwhich does not use those standard dnsmasq conf files.

A solution to this would be to detach dnsmasq from NetworkManager and install dnsmasq on its own (see https://superuser.com/a/809716/160420) but I personally don't want to change so much on my default Ubuntu system.

rubo77
  • 32,486
2

You can append a line in /etc/resolvconf/resolv.conf.d/head like

nameserver 8.8.8.8

Don't fear to edit it even though there is the line 'DO NOT EDIT THIS FILE BY HAND'

This file is used by resolvconf (man page) to generate /etc/resolv.conf, which is used by NetworkManager

After that you need to run, once

sudo resolvconf -u
solsTiCe
  • 9,231
  • Will this keep the dnsmasq functionality? Or will it have the same drawback as my solution? – rubo77 Apr 29 '15 at 05:41
  • @rubo77 I've tried this before with my college wifi, and nm-tool would report the dns provided by the access point, not by resolv.conf so I would assume this preserves dnsmasq functionality. In my solution bellow, supersede option basically tells dhcp to replace whatever access point provides with its own dns server. prepend option might be better, as you still receive what dhcp provided AND add additional,custom dns that you specify – Sergiy Kolodyazhnyy Apr 29 '15 at 16:31
  • @rubo77 if you look at the resulting /etc/resolv.conf you end up with 2 lines nameserver the first being the one from head here 8.8.8.8 and the second 127.0.0.1. So first it tries to use google dns, and second locahost/dnsmasq. – solsTiCe Apr 29 '15 at 21:45
  • 1
    This has the same Drawback: The local DNS caching on your machine by dnsmasq will not be available any more, every dns request will be made to the external DNS, so this is much slower. – rubo77 May 05 '15 at 19:24
  • Although this is not the solution, you get the bounty for your hint: "You could use https://dnsleaktest.com to test for your running DNS." ;) – rubo77 May 05 '15 at 20:02
1

METHOD #1: scripting

As you may know , you can change dns for particular connecton using nm-connection-editor (which is what opens when you hit Edit Connections in the network icon). That's the graphical way.

The terminal based way would be to edit each connection in /etc/NetworkManager/system-connections/ folder, by adding lines dns=xxx.xx.xxx.xxx; and ignore-auto-dns=true after [ipv4] and method=auto. Here's a sample file from /etc/Network-Manager/system-connections/:

[connection]
id=My College Connection
uuid=*******-******-****-a155-ca880ccf7ddb
type=802-11-wireless

[802-11-wireless]
ssid=My College Connection
mode=infrastructure
mac-address=00:0B:81:94:D3:08

[ipv6]
method=auto

[ipv4]
method=auto
dns=208.67.220.220;
ignore-auto-dns=true

Now, what if we could automate adding these two lines to every connection configuration file ? With grep and awk it's possible! My script bellow does exactly that.

#!/bin/bash
# Author: Serg Kolo
# Date: May 6, 2015
# Description: this script checks all settings for connections in 
# /etc/NetworkManager/system-connections/ , and if there's no custom
# dns set , this script sets it;
# NOTE: run sudo service network-manager restart after running this script

# set -x

for file in /etc/NetworkManager/system-connections/* ; do
        grep 'dns=208.67.220.220;' "$file"  || ( awk '{print;if ($1=="[ipv4]"){
getline; print "method=auto\ndns=208.67.220.220;\nignore-auto-dns=true"}}' "$fi
le" > .tmpfile && ( cat .tmpfile > "$file") )
done

And here is screenshot of the script at work:

enter image description here

NOTE: for 15.04 this script does not seem to work, but turns out nmcli has added a feature that allows modifying connection settings. The alternative to the script above is the following script:

#!/bin/bash
set -x
for file in /etc/NetworkManager/system-connections/*; do
    file=$(echo $file | cut -d'/' -f5-)
    nmcli connection modify id "$file" +ipv4.dns "" +ipv4.dns 208.67.220.220 +ipv4.ignore-auto-dns yes
done

METHOD #2 My personal favorite is to use /etc/dhcp/dhclient.conf, to uncomment line supersede domain-name-servers line and add your dns servers there , separated by comma. Screenshot bellow is from the web, belongs to debian, however it is equally used in ubuntu. I use this very same trick in both mint 17 and ubuntu 14.04 . Among other things I'd suggest commenting out line dns=dnsmasq in /etc/NetworkManager/NetworkManager.conf but it's not required.

supersede option basically replaces whatever dns you receive from your access point (aka router). For me, nm-tool reports same dsn that I placed on that line for any wifi connection. prepend on the other hand only adds whatever you specify as secondary dns in addition to what router provides.So that may be slightly better option.

enter image description here

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Why should I "commenting out line dns=dnsmasq"??? I want my fast local dns cache (which is dnsmask, isn't it?) and addistionally if my computer still didnt cache an IP I want to ask the FoeBud DNS – rubo77 May 05 '15 at 18:55
  • 1
    I tried your solution, but unfortunately it has the same drawback as my solution: it doesn't use the local dnsmasq any more – rubo77 May 05 '15 at 19:07
  • @rubo77 I've added another solution. Please review – Sergiy Kolodyazhnyy May 06 '15 at 19:39
  • would this keep dnsmasq intact? so the first dig to a new domain takes as long as the external DNS needs and from then on the next dig to that same domain will only tace somwhat 5 milliseconds? – rubo77 May 06 '15 at 19:58
  • @rubo77 well, with the dns that I've added, which is 208.67.220.220 (open dns), it takes me around 220 miliseconds of query time, as reported by dig. On the first try it took about 2-3 seconds to respond but right now, it reports around 220 - 300 mseconds – Sergiy Kolodyazhnyy May 06 '15 at 20:07
  • @rubo77 hold on, my dnsmasq was commented out – Sergiy Kolodyazhnyy May 06 '15 at 20:08
  • @rubo77 yup, I uncommented dns=dnsmasq in my /etc/NetworkManager/NetworkManager.conf and now query time takes 32ms. nm-tool still reports DNS I specified – Sergiy Kolodyazhnyy May 06 '15 at 20:10
  • Added information related to newer release, 15.04 – Sergiy Kolodyazhnyy May 16 '15 at 17:28
-5

You can do that by simply configuring dns in your modem/router itself henceforth all your connections will use that dns by default. Type 192.168.0.1 in your browser page to access your router/modem page. 208.67.222.222 208.67.220.220 are the two opendns servers.

Sapnesh Naik
  • 290
  • 5
  • 18
  • 1
    How would that work at "uni, wifi hotspot, phone tether" ?? – guntbert Oct 06 '14 at 18:38
  • well if you are using broadband(wired connection) and sharing the same broadband over wifi,tether etc. the dns configured at your modem would be the default, as all your internet no matter how you are accessing it(wifi,tether,ethernet port) is ultimately coming/going through the modem. – Sapnesh Naik Oct 06 '14 at 18:44