154

According to this page it appears to be simple. However, /etc/bind does not exist on the default installation of Ubuntu 12.04.3 LTS server.

So, without installing any further software, how can configure DNS and remove dnsmasq on ubuntu server? I am quite familiar with sudo & nano.

Braiam
  • 67,791
  • 32
  • 179
  • 269
Asta-Risky
  • 1,555
  • 2
  • 10
  • 6
  • Also take a look at https://help.ubuntu.com/community/Dnsmasq. bind IS a nameserver. If you need nameserver functionality on your server, you must install a nameserver. It would help if you could describe what it is you are trying to achieve. – hmayag Sep 17 '13 at 21:37
  • 2
    You were reading a manual to configure a DNS Server, not how to configure the DNS entries in the server. The manual you should have read are this – Braiam Sep 17 '13 at 22:55
  • I don't have the rep to post a comment on another answer, but in reply to Nullet's answer on September 17th 2013, this worked for me. I was thinking that there was a problem with my router (a cheap, ISP provided one). I also think it worth mentioning that on the dns-nameservers line, you should use the IP of the public DNS. Don't use the local address of the DNS, which is most likely on your router. I was connecting to the DNS on my router, which for some unknown reason wasn't working with my server. Instead, I found out where the DNS on my router was getting its information from, and set dn – kyul Mar 11 '14 at 18:38
  • 1
    I think there is a better answer here http://unix.stackexchange.com/a/154538/82018 - basically you can override your dhcp settings to "prepend" a server in front of the ones you currently use. This is nice as you don't have to rely solely on the new server, you just give it precedence for lookups. – Steve Midgley Nov 22 '14 at 21:07

5 Answers5

180

Set DNS Servers

You need to configure the /etc/network/interfaces file if you want to change your DNS server via the command line.

It should look something like this:

# The loopback network interface  
auto lo  
iface lo inet loopback

The primary network interface

auto eth0 iface eth0 inet static
address 192.168.X.X netmask 255.255.255.0 gateway 192.168.X.X dns-nameservers X.X.X.X

If you have more than one DNS server, just add a space between each:

dns-nameservers X.X.X.X Y.Y.Y.Y Z.Z.Z.Z

Just replace the Xs, Ys, and Zs with your own IPs of the DNS servers of choice, and when this is done, run this command to update the settings:

sudo ifdown eth0 && sudo ifup eth0

Hope this helps!

Update 2023

ifconfig, ifdown, ifup are deprecated. You can use:

sudo ip link set eth0 down && sudo ip link set eth0 up

Make sure to use these commands in a single line with && to avoid getting stuck out if you're using your system over a network or SSH.

Movahhedi
  • 103
Nullet
  • 3,014
  • Thanks - but how do I disable the dnsmasq as well ? – Asta-Risky Sep 17 '13 at 22:17
  • 1
    Yeah, dnsmasq is not installed on ubuntu server as default, if I remember correctly. – Nullet Sep 17 '13 at 22:47
  • /etc/NetworkManager does not exist on the default install of ubuntu server 12.04, also I ran ps -ef|grep dns which returned no results other than the grep command - Does that mean there is no dnsmasq running ? – Asta-Risky Sep 17 '13 at 22:47
  • OK - thank you for you help .. I have a lot to learn – Asta-Risky Sep 17 '13 at 22:48
  • 5
    tried @Nullet solution, but got this error, longtth@long-ubuntu-14:~$ sudo ifdown eth0 && sudo ifup eth0
    interface eth0 not configured
    RTNETLINK answers: File exists
    Failed to bring up eth0. Can you help me fix it ?
    – Luke Mar 28 '15 at 13:58
  • 2
    Here's my situation: I opened the file indicated in this answer and I had only the first 3 lines in it. I pasted in the rest. I ran the command and I got this: ifdown: interface eth0 not configured RTNETLINK answers: File exists Failed to bring up eth0. That concludes the output I received. If anyone has any thoughts that come to mind feel free to respond. I would like to know: what, generally speaking, am I to replace the X's in address and gateway with? – GoProCameraByGoPro Mar 31 '15 at 10:36
  • No command 'auto' found, did you mean: Command 'uuto' from package 'uucp' (universe) auto: command not found – Dr.jacky Jul 06 '15 at 08:52
  • 2
    @Mr.Hyde You shouldn't type these lines at terminal, you must to write them inside /etc/network/interfaces file. – richardaum Aug 13 '15 at 03:19
  • 1
    Ensure that you are actually using eth0 and not something else like eth1. ifconfig. – briankip Mar 23 '16 at 10:47
  • One thing about dnsmasq: If you use nmap and scan your own Ubuntu 16.04 LTS system it will show it running on port 53. However, that is only on the loopback interface. It is not configured to provide DNS to other systems by default. This is done merely for caching. – SDsolar Jul 24 '17 at 16:25
  • Great answer - just adding that you don't have to use static configuration to configure. You can use DHCP and add the "dns-nameservers" line to get the result – Avision Nov 16 '17 at 14:21
  • if ifdown and ifup does not work use 'ip addr flush dev '. – Indika K Jan 10 '19 at 13:58
  • 2
    This file does not seem to be existent in Ubuntu Server 22.04. – MS Berends May 08 '22 at 12:40
27

Ubuntu 20.04 does not use resolv.conf anymore by default, however it can be installed by:

sudo apt install resolvconf

However if you are on some older Ubuntu version or some fork, you might be using resolvconf (a package that manages the contents of /etc/resolv.conf).

In order to set dns-nameservers which won't be removed after reboot add them in

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

like

nameserver x.x.x.x
nameserver x.x.y.y

And afer that just do

sudo resolvconf -u
  • 1
    that path doesn't exist on my system I only have /etc/resolvconf/update-libc.d/avahi-daemon which loop for another bash script /usr/lib/avahi/avahi-daemon-check-dns.sh all I want is to get ride of default 192.168.1.1 DNS Servers: – Salem F Apr 05 '19 at 16:49
  • @SalemF just go-ahead and create that missing file... – Aleksandar Pavić Apr 08 '19 at 11:46
  • 1
    , it's looks like , ubuntu uses different approach to handle dns setting I was only able change it from the GUI nmwhich work after changingDHCPsetting toaddress only` and type dns address https://i.ibb.co/ysqwL0r/DHCP.png – Salem F Apr 08 '19 at 16:19
  • What is newer version here. I use 20_04, so it should work with it. Another thing: you write base, what about changing head - is head called before base? – Timo May 19 '21 at 07:06
  • 1
    @Timo base is used to be read defaults from and written in head, I think that head is overwritten every time you reboot... – Aleksandar Pavić May 19 '21 at 07:21
  • I do not think so regarding overwriting of the head. – Timo May 19 '21 at 07:48
  • this should be the accepted answer , thank you so much. – ParSa Jul 30 '22 at 13:46
18

I use Ubuntu 20.04 and unfortunately none of the other answers worked for me.

So, I will share the way I fixed the problem below:

First install resolvconf, if it is not already installed

sudo apt update
sudo apt install resolvconf

check that resolvconf service is started and enabled

sudo systemctl status resolvconf.service

If service is not enabled, you can start and enable using the following commands:

sudo systemctl start resolvconf.service
sudo systemctl enable resolvconf.service

Now edit the resolv.conf.d/head configuration file

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

and add your DNS addresses into it (for example, I use Google Public DNS, 8.8.8.8 and 8.8.4.4)

nameserver 8.8.8.8 
nameserver 8.8.4.4

Now force resolvconf to run update scripts when invoked with -u

sudo resolvconf --enable-updates 

Now run updates

sudo resolvconf -u

Now if you check the content of resolv.conf file using the following command

cat /etc/resolv.conf

you must see your DNS configuration. If not, try the following commands and check again

sudo systemctl restart resolvconf.service
sudo systemctl restart systemd-resolved.service

References

Pablo Bianchi
  • 15,657
  • sudo apt update requires you to have a DNS server already. If you don't have a DNS server configured then the update fails because it cannot resolve us.archive.ubuntu.com, etc. – Rob Oct 04 '23 at 00:37
  • @Rob You could initially add nameservers to /etc/resolv.conf, then install what you need with apt. In my case, /etc/resolv.conf was reset after reboot. – m_ocean Nov 01 '23 at 11:05
12

As of 20.04 Focal Fossa, Ubuntu Server uses systemd-resolved to manage nameserver configuration. /etc/resolv.conf is a static file and should not be edited.

To configure your resolver, you set it in your netplan configuration file, like this:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s25:
      addresses:
        - 192.168.0.100/24
      gateway4: 192.168.0.1
      nameservers:
          search: [mydomain, otherdomain]
          addresses: [1.1.1.1, 8.8.8.8, 4.4.4.4]

For more information on how to use the netplan system, see the Ubuntu article on network configuration.

gcode
  • 407
6

NetworkManager TUI: nmtui

Beside this alternative using nmcli connection edit you might like a more ncurses approach with nmtui, the GNOME's CLITUI alternative to nm-connection-editor.

screenshot nmtui main window

screenshot editing a connection

Note: This answer is more related to Ubuntu Desktop, but since the suitable question was marked as duplicate of this one (incorrectly, IMO) I post it here.

Pablo Bianchi
  • 15,657