Auto generate /etc/resolv.conf

10,338

Solution 1

You are placing the changes you want into the wrong directory. NetManager allows custom configuration of the file by editing the base, head and tail by placing your preferences in this directory:

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

You can correct this by moving your /etc/resolvconf/base to /etc/resolvconf/resolv.conf.d/base.

The content you're seeing after reboot is the content of the /etc/resolvconf/resolv.conf.d/head file. It takes precedence over all and as you can see, is preserved.

The priority choices should be placed there.

Solution 2

You can add the nameservers in the resolv.conf and make it immutable, which means - it can not be modified.

echo 'nameserver 8.8.8.8' > /etc/resolv.conf
sudo chmod 444 /etc/resolv.conf
sudo chattr +i /etc/resolv.conf

That should do a trick. To modify the file again do: sudo chattr -i /etc/resolv.conf

Share:
10,338

Related videos on Youtube

Jiří Liška
Author by

Jiří Liška

Updated on September 18, 2022

Comments

  • Jiří Liška
    Jiří Liška over 1 year

    I have manually edited /etc/resolvconf/base to add openDNS name servers, and then run sudo resolvconf -u to update the /etc/resolv.conffile.

    However changes are discarded on each reboot.

    How do I preserve those changes?

    $ cat /etc/os-release 
    NAME="Ubuntu"
    VERSION="16.04.3 LTS (Xenial Xerus)"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu 16.04.3 LTS"
    VERSION_ID="16.04"
    HOME_URL="http://www.ubuntu.com/"
    SUPPORT_URL="http://help.ubuntu.com/"
    BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
    VERSION_CODENAME=xenial
    UBUNTU_CODENAME=xenial
    

    edit:

    /home/pkaramol
    $ cat /etc/resolvconf/resolv.conf.d/base
    nameserver 208.67.222.222
    nameserver 208.67.220.220
    /home/pkaramol
    $ sudo resolvconf -u
    /home/pkaramol
    $ cat /etc/resolv.conf 
    # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    
    nameserver 127.0.1.1
    
    • Robert Riedl
      Robert Riedl over 6 years
      Do you use the GUI and have Network Manager running ? That will override/overwrite what you are doing in resolv.conf
  • Jiří Liška
    Jiří Liška over 6 years
    Hi thx but pls check my edit
  • Apologician
    Apologician over 6 years
    I have never used the base or the tail. I always use the head so that my entry would take precedence. Will you test the /etc/resolvconf/resolv.conf.d/head option and post the results? I had mentioned base because of the content of your question, which may loose to something else taking place. The text in your email (DO NOT EDIT) is part of the /etc/resolvconf/resolv.conf.d/head file which appears to be preserved. I'll edit my answer based on the new content in your question.
  • Jiří Liška
    Jiří Liška over 6 years
    yep...head seems to work even after the reboot..thx
  • Apologician
    Apologician over 6 years
    Thanks! Please consider accepting the answer by clicking the Grey checkmark. It'll help others to easily find solutions that work.
  • Jiří Liška
    Jiří Liška over 6 years
    just accepted your answer
  • Syam kumar KK
    Syam kumar KK almost 5 years
    Its worked for me, thank you so much...