Ubuntu keeps losing resolv.conf settings?

2019-09-20 14:39发布

问题:

Every time I reboot my ubuntu server, it loses it's nameserver setting. I have to run:

echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf 

each time I reboot for it to work properly again.

I tried editing resolv.conf directly and still doesn't work properly.

Any advice?

回答1:

Edit below file for making effect on every time when you reboot This is head section of resolve conf which added in resolve.conf

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



回答2:

That happens because of resolvconf. As the man page states, it allows other programs to change the DNS resolver configuration. Probably, there is a DHCP server on your network that is providing your host its IP address and the DSN servers.

You can change the DHCP configuration or force the first lines of resolv.confas @sahilKataria suggested. Using your command:

echo "nameserver 8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/head


回答3:

In Ubuntu 18.04 thats working:

Sudo rm /etc/resolv.conf
sudo echo "nameserver xxx.xxx.xxx.xxx" >> /etc/systemd/resolve/resolv.conf
sudo ln -s /etc/systemd/resolve/resolv.conf /etc/resolv.conf

But I think a better option would be to edit the /etc/network/interfaces file and configure the system correctly, including the dns you need, for example:

auto lo
iface lo inet loopback 

auto enp0s3 
iface enp0s3 inet dhcp
dns-nameservers 8.8.8.8 8.8.4.4 

In this example the first two lines define the local interface, do not touch it, the third line says that when the computer boots up the network card enp0s3 simultaneously (you can find out the name of your network card(s) with the command ifconfig -a), the fourth line tells the enp0s3 card to listen to a dhcp server and take the data needed for its network configuration from there, and the last line tells it which dns you want to use. If you know your network configuration, it will always be the same or you don't have a dhcp server, the file would be something like this:

    auto lo
    iface lo inet loopback 

    auto enp0s3 
    iface enp0s3 inet static
    address 10.10.5.67
    netmask 255.255.255.0
    gateway 10.10.5.1
    broadcast 10.10.5.255
    dns-nameservers 8.8.8.8 8.8.4.4 

Another possibility is to configure the netplan file, the new default mode to manage the network from ubuntu 17.10 Artful. Here you can see more on the subject, particularly I don't like it but I am aware of its power for complex cases.



回答4:

You can keep settings by editing a base file.

You need to install resolvconf

sudo apt-get install resolvconf

Edit /etc/resolvconf/resolv.conf.d/base

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

Add your nameserver

nameserver 8.8.8.8

Start resolvconf

sudo /etc/init.d/resolvconf start

Check /etc/resolv.conf contains line

nameserver 8.8.8.8

Then try to restart your server and check /etc/resolv.conf again



标签: linux ubuntu