77

I suspect that the name server provided by my ADSL modem/router is buggy. Whenever I browse to a website for the first time in ubuntu, resolving the domain name takes at least 15 seconds.

To work around that problem, I changed the nameserver configuration in /etc/resolv.conf from 192.168.1.1 (my ADSL modem) to 8.8.8.8 (google's primary DNS). This seems to fix the problem, but unfortunately my changes to /etc/resolv.conf are overwritten by "NetworkManager" at each startup.

What is the proper way to configure the name server IP in ubuntu 10.4?

0

15 Answers 15

66

If you have typical connection setup with the network manager and DHCP, try the following:

  1. Right click on the network manager icon in the panel and choose "Edit connections..."
  2. Select your connection from the wired or wireless tab, choose "Edit"
  3. (Enter your password if the connection is set as "system-wide available")
  4. Choose IPv4 settings tab
  5. Switch method to "Automatic (DHCP) addresses only"
  6. Enter the name server you want in the box "Additional DNS servers" and press "Apply"

That should do the trick.

5
  • 1
    This continues to work in Ubuntu 12.04 and later, even though NetworkManager now uses resolvconf to handle resolv.conf.
    – jdthood
    Dec 7, 2012 at 8:25
  • 5
    If you are wondering where the configuration ends up, have a look at /etc/NetworkManager/system-connections/ (you will need administrative privileges to access those configuration files).
    – Lekensteyn
    Mar 21, 2014 at 10:02
  • 5
    This only changes dns for the current connection, but is there also a way for a Global DNS change?
    – rubo77
    Apr 28, 2015 at 20:43
  • the list is empty for me. what does it use by default?
    – J. Taylor
    Jun 15, 2018 at 9:21
  • 3
    Works exactly the same on terminal interface nmtui btw! Dec 7, 2018 at 1:35
47

The best way to set a nameserver via the CLI, without NetworkManger, with a static address, or independent of the connection is this:

Install the resolvconf package.

Run

sudo nano /etc/resolvconf/resolv.conf.d/head

(ignore the scary warning. /etc/resolv.conf is autogenerated, so the warning is there so it will get put in /etc/resolv.conf when /etc/resolv.conf is generated.) To the end of the file, add

 nameserver <ip_of_nameserver>

Press Ctrl x and answer yes to saving the file. To finish up, regenerate /etc/resolv.conf so the changes are applied right now:

 sudo resolvconf -u
1
  • 1
    For some reason about a month ago my nightly Xubuntu build stopped resolving LAN hostnames. My DHCP server should issue itself (192.168.68.1) as the DNS server, I've also hard-coded it in Network Manager and every place (mentioned above this answer). With the way Xubuntu/NetworkMan/Whatever uses a 127.0.x.x IP these days, it's hard to diagnose the problem. I could resolve external IPs, so it was getting SOMETHING for DNS. Anyway, this was the only answer that solved my prob. Thanks Dec 18, 2016 at 1:41
32

You can change them like this.

First, click the network manager icon in your menubar, and the click the Edit Connections... item.

enter image description here

Now, switch to the "Wired" or "Wireless" tabs, depending on what you use - I use WiFi, so I'll be using that.

Select the name of your network, and click Edit...

Switch to the IPv4 tab, and then change it from automatic DHCP to Automatic (DHCP) addresses only.

Enter in your DNS server IP address.

enter image description here

Click save, and you're done!

3
  • While you can change the DNS server in the network settings on any PC it is best to change them in the actual router that connects to the internet so all connected devices benefit from it. Dec 24, 2011 at 21:29
  • 3
    What if he does not have a router per se? Or he just wants to change the DNS for this computer / user? What if its just this one time? Maybe he actually does not have router access and wants to user another DNS server... I can add reasons to this list all night long. Good tip and answer @jrg. Dec 25, 2011 at 1:12
  • 6
    It is a nice graphical how-to for setting dns on a single machine with network manager. I would only point out you may set more then one, comma separated. 8.8.8.8,8.8.4.4. Setting DNS for a LAN can also be helpful, if the lan is large enough I personally either use the router or dnsmask. On a LAN, it is often easier to maintain a blacklist or proxy at a single location (router/dnsmask/squid) rather then on each client or each user on each client, but that is a little beyond the question asked here.
    – Panther
    Dec 25, 2011 at 6:02
22

Another way to do this is to edit /etc/dhcp3/dhclient.conf you can do this by typing this in to the run dialog (appears by pressing Alt + F2)

gksudo gedit /etc/dhcp3/dhclient.conf

Then find the following line

#prepend domain-name-servers 127.0.0.1;

And change it to

prepend domain-name-servers 8.8.8.8;

This will make dhclient (the DHCP client that NetworkManager uses) prepend this to your dns servers, so resolve.conf will end up looking like this

nameserver 8.8.8.8
nameserver 192.168.1.1

This solution will work everywhere and you will always get the DNS that you have chosen as the primary one.

7
  • 6
    It seems to me that Network Manager writes resolv.conf without consulting dhclient.conf
    – daithib8
    Jun 6, 2011 at 10:52
  • 3
    on ubuntu 11.10 you need to modify /etc/dhcp/dhclient.conf instead of /etc/dhcp3/dhclient.conf Jun 7, 2012 at 12:21
  • 1
    can put in multiple ones? Apr 29, 2014 at 22:50
  • 1
    @PyRulez Yes, separate by comas, semicolon at the end Mar 18, 2015 at 0:30
  • 4
    I tried this in 14.10 but no effect
    – rubo77
    Apr 28, 2015 at 20:01
7

Another solution is to change your router's configuration to use the other DNS server. Just log into its admin and as long as it's not the router causing the problem, all your client machines get better DNS.

6
  • +1 Agreed if this is a problem that isn't likely to be resolved soon best to fix it on the router then it will be fixed for all machines on the network. I find its best to either use Google DNS or OpenDNS. Aug 15, 2010 at 17:22
  • 1
    I've already done that, but the router then still acts as an intermediate name server between clients and the configured name server, and it is still very slow.
    – Wim Coenen
    Aug 15, 2010 at 17:38
  • 1
    Many ISP's lock their routers so you can't change these settings, or have to pay to do so. Aug 15, 2010 at 20:53
  • @Wim If your router can't provide DNS resolution within a reasonable timeframe, say 50ms on a good connection, something is seriously wrong with it. So wrong it may be handling other traffic ineffectively. It might be time for a $30/£20 upgrade.
    – Oli
    Aug 15, 2010 at 22:33
  • 1
    @Oli: The DNS problem only shows up when using the router from a linux box: I had the same problem with debian, but no issues with windows. I'm sure it's just some subtle compatibility issue. The router works fine otherwise.
    – Wim Coenen
    Aug 16, 2010 at 10:14
6

Click on the network indicator at the top right of the screen and pick Edit Connections.... Pick the connection you want to modify (probably from the Wired or Wireless tab and click the Edit... button.

Switch to the IPv4 Settings tab of the connection window and change the method from Automatic (DHCP) to Automatic (DHCP) addresses only. You should now be able to specify a DNS server IP address.

Save the connection and you should be good to go. You may have to reset the connection by picking it in the indicator's menu after changing the settings.

3

You can change IPv4 DNS settings the graphical way as answered here

But if you're wondering how to do the same thing using command line, you have to list files in /etc/NetworkManager/system-connections directory with ls command:

ls /etc/NetworkManager/system-connections

There has to be a file with the name of your connection, mine is called Wired Connection 1.

So we need to edit the file and add DNS servers in it:

Sudo nano "/etc/NetworkManager/system-connections/Wired connection 1"

Add as many DNS servers as you like under the [ipv4] section and seperate them with semicolons ; and also put a ; at the end of the last DNS server like this:

[ipv4]
dns=1.1.1.1;8.8.8.8;9.9.9.9;

You can just delete this line if you don't want to assign any dns.

Also you have to add this line ignore-auto-dns=true to ignore DHCP provided DNS servers (like the DNS server set in router's config):

[ipv4]
dns=1.1.1.1;8.8.8.8;9.9.9.9;
ignore-auto-dns=true

A little context to this answer: I was trying to figure out what file gets edited when I add a DNS server in the GUI's DNS settings, so I was running this command for 16+ hours: sudo grep -rs "1.1.1.1" / which was scanning all files on my Ubuntu virtual machine but it never finished even after running that long, but I knew it's possible that the file could exist in /etc/NetworkManager so I ran the grep again pointing at that directory and found this: /etc/NetworkManager/system-connections/Wired connection 1:dns=1.1.1.1; so I came back here to write an answer only to find out Lekensteyn has already pointed this out in the comments on the accepted answer.

3

First you need to install resolveconf.service:

sudo apt-get update
sudo apt-get install resolvconf

Then you need start the resolvconf service:

sudo systemctl start resolvconf.service

Then you need enable resolvconf.service on boot:

sudo systemctl enable resolvconf.service

The you can check status with command:

sudo systemctl status resolvconf

You will see something like this:

screenshot of terminal showing status

Then you need change configuration in the file /etc/resolv.conf.d/head:

sudo nano /etc/resolvconf/resolv.conf.d/head

You need add your DNS servers like this (Google DNS). You can add:

  1. IPv4 addresses:

    nameserver 8.8.8.8
    nameserver 8.8.4.4
    
  2. or IPv6 addresses:

    nameserver 2001:4860:4860::8888
    nameserver 2001:4860:4860::8844
    

Then you need restart your resolvconf.service:

sudo systemctl restart resolvconf.service

Now you can check your /etc/resolve.conf file. And you will see your new DNS.

sudo nano /etc/resolv.conf
2
  • This already comes in ubuntu right?
    – Zeor137
    Sep 21, 2021 at 23:05
  • Yes, this is for ubuntu.
    – Salinder
    Sep 23, 2021 at 7:20
1

I believe if you change it under the network manager its self the changes with persist.

To use eth0 for example

Network Manager -> Wired -> Auto eth0 -> ipv4 Settings.

Set your DNS server to 8.8.8.8 and apply.

3
  • I had trouble finding anything called "network manager". There is a "System - Administration - Network tools" but that doesn't help. Eventually I found that I needed "System - Preferences - Network Connections".
    – Wim Coenen
    Aug 15, 2010 at 17:54
  • 1
    There's an network manager icon in the panel (the one displaying your network status) that you can right click to edit the connections. Aug 15, 2010 at 20:05
  • This does not work
    – user568021
    May 31, 2015 at 11:10
1

open this file

$ sudo nano /etc/resolv.conf

Remove the IP address created by NetworkManager and add the follow line (I am add the 1.1.1.1 address to here.)

nameserver 1.1.1.1

Save and exit, then run follow command for NetworkManager does not override the file

$ sudo chattr +i /etc/resolv.conf

If you want edit the file type sudo chattr -i /etc/resolv.conf and add your configuration, finally retype chattr +i command.

0

Using the Terminal

You need to edit /etc/resolv.conf file to set Name server IP address that the resolver should query. Up to 3 name server internet IP address can be defined. If there are multiple servers, the resolver library queries them in the order listed.

Firstly type

sudoedit /etc/resolv.conf

Append your Preferred DNS server IP address as follows:

nameserver <preferred-ip1>
nameserver <preferred-ip2>
nameserver <preferred-ip3>

taken from: http://www.cyberciti.biz/faq/ubuntu-linux-configure-dns-nameserver-ip-address/

4
  • 6
    Note that if the name servers are set via DHCP, /etc/resolv.conf will get overwritten. So this approach is only appropriate for machines with manual network configurations. Technically, you could do chattr +i /etc/resolv.conf to prevent the file from being modified (I did so years ago), but the best way for most machines would be jrg's approach in his answer: askubuntu.com/a/90263/13398. Dec 25, 2011 at 13:06
  • @ScottSeverance What it the system didn't have X
    – Amith KK
    Dec 25, 2011 at 13:12
  • In that case, it's probably a server with manually-configured networking, in which case your answer is perfectly appropriate. But most Ubuntu machines run X and can easily handle the other approach. If a machine isn't running X but does have a suitable GUI installed, you could also run the graphical tools over SSH using ForwardX11=yes. Dec 25, 2011 at 13:15
  • 6
    If you are going to try to manually edit the /etc/resolv.conf file, the proper way to do it (so it is not overwritten) is to install the "resolvconf" package and then edit either /etc/resolvconf/resolv.conf.d/head or /etc/resolvconf/resolv.conf.d/tail.
    – Azendale
    Dec 25, 2011 at 14:37
0

For headless servers where there's no X and management is via ssh or whatever, a command-line solution is necessary. If resolv.conf is not overwritten, then that's the correct place to change nameservers.

If resolv.conf does get overwritten then, on 14.04LTS at least, the files to edit are:

  • /etc/resolvconf/resolv.conf.d/head
  • /etc/resolvconf/resolv.conf.d/tail

I strongly suggest putting comment strings into files like these, such that they appear in the generated file (/etc/resolve.conf) and you can find them in the future. I start and end each file with a one-line comment of the form:

  • # ====== begin /etc/resolveconf/resolv.d/tail ======
  • # ====== end /etc/resolveconf/resolv.d/tail ======

and put the relevant directives between them.

0

I found that adding the line dns-nameserver ip to the interfaces file, setting the wired connection ipv4 settings to Automatic (DHCP) settings only and entering the DNS server IP and domain name. Also adding the local DNS IP to the DSL router static DNS section causes NetworkManager to include this local address in the resolv.conf file that it overwrites along with other info retrieved from the router. All of these together seems to make local DNS name resolution persistent - a surprisingly difficult task it seems. My need for local DNS is for mounting a Linux client to a Samba share using mount.cifs on a DHCP served network.

0

If Network-Manager is stopped and /etc/resolvconf/resolv.conf.d/base has been configured.

Maybe resolvconf --enable-updates is useful.

0

Create the setting file (with sudo permissions)

/etc/sysconfig/network-scripts/ifcfg-default-dhcp

with the content:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=no
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME="default setting on boot"
UUID=616e72fa-e208-358c-8567-d0eae408e989
DEVICE=enp0s31f6
ONBOOT=yes
AUTOCONNECT_PRIORITY=-999
DNS1=8.8.8.8
DNS2=8.8.8.4
IPV6_PEERDNS=no

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .