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
  • 34,024
  • 52
  • 172
  • 299

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
  • 34,024
  • 52
  • 172
  • 299
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,515
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

-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
  • 6
  • 19