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:

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.
