I deleted my resolv.conf how do I get it back or how do I create it again

28,898

Solution 1

/etc/resolv.conf is automatically generated upon reboot by resolvconf if its a symbolic link to /run/resolvconf/resolv.conf (the file resolvconf writes to), you don't need to create it or edit it. You can put your name servers in /etc/network/interfaces using the following syntax so that they get added to /etc/resolv.conf automatically:

dns-nameservers <IP_address> <IP_address> <IP_address>

For example:

dns-nameservers 8.8.8.8 8.8.4.4

you should add this line at the end of your static IP configuration block.

If you have removed the symlink /etc/resolv.conf, run sudo dpkg-reconfigure resolvconf and you will come up with a set of questions to set it right again. Now do a reboot or run the following:

sudo ifdown eth0 && sudo ifup eth0  ##Change interface name accordingly

Everything should be fine.

Solution 2

To restore the symbolic link /etc/resolv.conf -> ../run/resolvconf/resolv.conf, execute the command sudo dpkg-reconfigure resolvconf at a command prompt.

If you are configuring your Internet-facing network interface using the ifup utility (whose configuration file is /etc/network/interfaces) then add nameserver information to the corresponding stanza in /etc/network/interfaces. Usually this entails adding at least a dns-nameservers option to the stanza in question. E.g.,

iface eth0 inet static
    address 192.168.22.33
    netmask 255.255.255.0
    dns-nameservers 192.168.22.1

Then when you ifup eth0, the address 192.168.22.1 will be added by resolvconf to resolv.conf (on an option line: nameserver 192.168.22.1).

Solution 3

You could also run:

sudo ln -s /run/resolvconf/resolv.conf /etc/resolv.conf

To restore the symbolic link.

Share:
28,898

Related videos on Youtube

John Moore
Author by

John Moore

Updated on September 18, 2022

Comments

  • John Moore
    John Moore over 1 year

    I was changing my DHCP IP address to static on my server by sudo gedit /etc/network/interfaces and managed to edit the file and save it.

    I then went to change resolv.conf but then it went blank and every time I rebooted it was blank.

    I then deleted it by accident.

    Now I can't install anything with apt-get or aptitude.

  • John Moore
    John Moore about 9 years
    I tried adding that but it doesn't bring back the deleted resolv.conf file. When I run sudo cat /etc/resolv.conf it says no such file or directory.
  • heemayl
    heemayl about 9 years
    Run this sudo ln -s /var/run/resolvconf/resolv.conf /etc/resolv.conf then sudo ifdown eth0 && sudo ifup -v eth0 ..replace eth0 with your interface name..
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 9 years
    @JohnMoore you said you tried adding dns server to /etc/resolv.conf, is that right ? Dns name servers should only be added to /etc/resolvconf/resolv.conf.d/base file . Or head file.
  • steeldriver
    steeldriver about 9 years
    If you have manually overwritten the symlink with a file, you may need to run sudo dpkg-reconfigure resolvconf - it will present you with a question about preparing /etc/resolv.conf for dynamic updates - answer "Yes". It may also present you with another question about temporarily appending your existing config to the dynamic one - I suggest answering "No" to that one.
  • jdthood
    jdthood about 9 years
    Resolvconf does not write to /etc/resolv.conf. It writes to /run/resolvconf/resolv.conf. This only affects the behavior of the resolver if /etc/resolv.conf is a symbolic link to /run/resolvconf/resolv.conf.
  • John Moore
    John Moore about 9 years
    When I run ' sudo dpkg-reconfigure resolvconf ' I get the following '/usr/sbin/dpkg-reconfigure: resolvconf is broken or not fully installed.'
  • heemayl
    heemayl about 9 years
    @JohnMoore Run sudo apt-get install --reinstall resolvconf
  • John Moore
    John Moore about 9 years
    Err gb.archive.ubuntu.com/ubuntu utopic/main resolvconf all 1.69ubuntu4 Could not resolve 'gb.archive.ubuntu.com' E: Failed to fetch gb.archive.ubuntu.com/ubuntu/pool/main/r/resolvconf/… Could not resolve 'gb.archive.ubuntu.com' E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
  • heemayl
    heemayl about 9 years
    @JohnMoore Run sudo apt-get update first..
  • John Moore
    John Moore about 9 years
    When I run apt-get update I get errors and when I run apt-get install --reinstall resolvconf I get errors. This goes back to fact I can't get apt-get to work.
  • John Moore
    John Moore about 9 years
    # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 192.168.1.116 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.225 gateway 192.168.1.1 dns-nameservers 8.8.8.8 8.8.4.4
  • heemayl
    heemayl about 9 years
    @JohnMoore I think your network configuration is messed up somehow..although /etc/network/interfaces looks Ok..do you get response using ping 8.8.8.8 ?
  • John Moore
    John Moore about 9 years
    It says unreachable
  • heemayl
    heemayl about 9 years
    @JohnMoore: Then you have a networking issue..please ask it as a new question as it is a different issue that this one, provide all the current details....for this issue, please select any one of the answer as accepted by clicking on the left sided tick mark of the answer so that this issue can be mark as solved..