We help IT Professionals succeed at work.

resolv.conf is was deleted, and now things are not correct:  I am trying to install dnsmaq, and I issued the following commands

The issue is strait forward.  On Ubuntu 18.04 I have deleted reolve.conf from the /ETC dir.  Resolv.conf appears to get recreated on reboot but it does not contain any DNS server entries save 127.0.0.53.  This will not work, you cannot edit the file as it is symbolically linked.  How do I correct, and provide it with the correct entries.  I was trying to install Dnsmasq.  The following commands were issue

john@VBserver1:~$ sudo systemctl stop systemd-resolved
john@VBserver1:~$ sudo ls -lh /etc/resolv.conf
lrwxrwxrwx 1 root root 37 Feb 17 21:35 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf
john@VBserver1:~$ sudo rm /etc/resolv.conf
john@VBserver1:~$ echo "nameserver 8.8.8.8" > /etc/resolv.conf
-bash: /etc/resolv.conf: Permission denied
john@VBserver1:~$ sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf   This one is where the failure results.
-bash: /etc/resolv.conf: Permission denied
john@VBserver1:~$
Comment
Watch Question

David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You must prepend "sudo" in front of your "echo" command for this to work.

/etc/resolv.conf is root owned, so requires sudo, su or other root access to update this file.

Other items...

1) If you're using dnsmasq, be sure you've enabled reading the system /etc/resolv.conf file or it will be ignored.

2) Just running systemctl stop systemd-resolved is completely insufficient to keep dnsmasq working.

Every reboot + many package updates will restart systemd-resolved, so best to remove this code... as it's badly broken anyway...

https://www.experts-exchange.com/questions/29162917/Huge-DNS-queries-from-openshift.html provides instructions for completely nuking systemd-resolved forever.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Just elevate, sudo -i
Then issue your
"Nameserver 8.8.8.8" >> /etc/resolv.conf


The sudo might elevate echo, while the redirect to /etc/resolv.conf was not, or is being blocked by the system's config.  Did your delete attempt succeed?

You might need to configure your system differently.
Usually, resolv.conf is configured by network when it is based on DHCP versus static.
Murugesan NWe like KT across the universe.  C C++ shell script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
CERTIFIED EXPERT

Commented:
@Johnny

$ /usr/bin/sudo echo Nameserver 8.8.8.8 > /root/Test.txt
-bash: /root/Test.txt: Permission denied
since executing command using sudo and redirecting output at current user terminal to root directory. Hence Permission denied

$ /usr/bin/sudo /usr/bin/bash -c "echo Nameserver 8.8.8.8 >> /etc/resolv.conf"
$#OR
$ /usr/bin/sudo /usr/bin/bash -c "echo Nameserver 8.8.8.8 >> /etc/resolv.conf;echo multi line commands;/usr/bin/cat /etc/resolv.conf"
$#
$ /usr/bin/sudo /usr/bin/vi /etc/resolv.conf
$ #OR (arnold sudo -i) one line command
$ /usr/bin/sudo -i "echo Nameserver 8.8.8.8 >> /etc/resolv.conf"
$ #OR (arnold sudo -i) one line command
$ /usr/bin/sudo -i "echo Nameserver 8.8.8.8 >> /etc/resolv.conf;/usr/bin/cat /etc/resolv.conf"

Open in new window

Change the path
/usr/bin/sudo or /bin/sudo
/bin/cat or /usr/bin/cat
/usr/bin/vi or /bin/vim or /usr/bin/vim
/usr/bin/bash or /bin/bash or ...
as per your system file locations.
Murugesan NWe like KT across the universe.  C C++ shell script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
CERTIFIED EXPERT

Commented:
@Johnny

Other way using a script from other user and /usr/bin/sudo
$ /usr/bin/cat  ./29173112.sh
#!/bin/bash
LOGNAME=$(/usr/bin/whoami)
if [ "root" = "$LOGNAME" ]
then
        for NAMESERV in 8.8.8.8 8.8.8.1 8.8.8.2
        do
                /usr/bin/grep $NAMESERV /etc/resolv.conf >/dev/null 2>&1
                if [ 0 -ne $? ]
                then
                        echo "echo Nameserver $NAMESERV >> /etc/resolv.conf"
                        echo Nameserver $NAMESERV >> /etc/resolv.conf
                        sleep 1
                else
                        echo /etc/resolv.conf having $NAMESERV
                fi
        done
else
        echo "Execute $0 using root user or sudo"
fi

Open in new window


$ ./29173112.sh
Execute ./29173112.sh using root user or sudo

Open in new window


$ /usr/bin/sudo ./29173112.sh
echo Nameserver 8.8.8.8 >> /etc/resolv.conf
echo Nameserver 8.8.8.1 >> /etc/resolv.conf
echo Nameserver 8.8.8.2 >> /etc/resolv.conf

Open in new window


$ /usr/bin/sudo ./29173112.sh
/etc/resolv.conf having 8.8.8.8
/etc/resolv.conf having 8.8.8.1
/etc/resolv.conf having 8.8.8.2

Open in new window


When executing ./29173112.sh using sudo all actions inside script belongs to root terminal.
The output from script redirected to current user terminal.
Hence we cannot redirect script output to root folders including sudo

$ /usr/bin/sudo ./29173112.sh >> /root/Test.txt
-bash: /root/Test.txt: Permission denied

Open in new window


$ /usr/bin/sudo bash -c "./29173112.sh > /root/Test.txt 2>&1"
$ /usr/bin/sudo /usr/bin/cat /root/Test.txt
/etc/resolv.conf having 8.8.8.8
/etc/resolv.conf having 8.8.8.1
/etc/resolv.conf having 8.8.8.2

Open in new window

Murugesan NWe like KT across the universe.  C C++ shell script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
CERTIFIED EXPERT

Commented:
@Johnny

Few more ways:
$ for NAMESERV in 8.8.8.8 8.8.8.1 8.8.8.2
do
        /usr/bin/grep $NAMESERV /etc/resolv.conf >/dev/null 2>&1
        if [ 0 -ne $? ]
        then
                echo "echo Nameserver $NAMESERV >> /etc/resolv.conf"
                echo Nameserver $NAMESERV >> /etc/resolv.conf
                sleep 1
        else
                echo /etc/resolv.conf having $NAMESERV
        fi
done
echo Nameserver 8.8.8.8 >> /etc/resolv.conf
-bash: /etc/resolv.conf: Permission denied
echo Nameserver 8.8.8.1 >> /etc/resolv.conf
-bash: /etc/resolv.conf: Permission denied
echo Nameserver 8.8.8.2 >> /etc/resolv.conf
-bash: /etc/resolv.conf: Permission denied

Open in new window


$ /usr/bin/sudo /usr/bin/bash -c "for NAMESERV in 8.8.8.8 8.8.8.1 8.8.8.2
do
        /usr/bin/grep \$NAMESERV /etc/resolv.conf >/dev/null 2>&1
        if [ 0 -ne \$? ]
        then
                echo \"echo Nameserver \$NAMESERV >> /etc/resolv.conf\"
                echo Nameserver \$NAMESERV >> /etc/resolv.conf
                sleep 1
        else
                echo /etc/resolv.conf having \$NAMESERV
        fi
done"
echo Nameserver 8.8.8.8 >> /etc/resolv.conf
echo Nameserver 8.8.8.1 >> /etc/resolv.conf
echo Nameserver 8.8.8.2 >> /etc/resolv.conf

Open in new window


$ /usr/bin/sudo /usr/bin/bash -c "for NAMESERV in 8.8.8.8 8.8.8.1 8.8.8.2
do
        /usr/bin/grep \$NAMESERV /etc/resolv.conf >/dev/null 2>&1
        if [ 0 -ne \$? ]
        then
                echo \"echo Nameserver \$NAMESERV >> /etc/resolv.conf\"
                echo Nameserver \$NAMESERV >> /etc/resolv.conf
                sleep 1
        else
                echo /etc/resolv.conf having \$NAMESERV
        fi
done"
/etc/resolv.conf having 8.8.8.8
/etc/resolv.conf having 8.8.8.1
/etc/resolv.conf having 8.8.8.2

Open in new window

JohnnyIT Consultant!

Author

Commented:
Oh, sorry guys... I almost always use sudo, and in this case I most certainly did.  The problem is really a question of the symbolic link when your using Netplan by default.  If you delete that file I have not found anyway to recreate it under 18.04. sudo is not the answer.  Thats a no brainier.  If you know ubuntu, and your using the one user you created with the installation which most do then you almost always need sudo.   the problem with 18.04 is if you delete this file given netplan. It looks like you have to install the package resolvconf.  What  love is how so many people do not realize that this file is symbolically linked.  I would like to know how to install DNSmasq given that I am using Netplan, and the instructions here  https://computingforgeeks.com/install-and-configure-dnsmasq-on-ubuntu-18-04-lts/  I would love to chat with the person or persons who put this here.  regardless of sudo it fails.  You better not delete resolv.conf.  I think the resolvconf package it the answer.
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You can rerun the enable and start the resolver, or recreate the link  included inthe link.

Elevate your rights by running sudo -i to get a shell with root rights and see if you can perform the tasks .
Note the guide you reference as well as https://linuxhint.com/dnsmasq_ubuntu_server/

You should make sure to retrieve all the package before trying to adjust the resolver, dnsmasq setup.
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Regards...

/usr/bin/sudo ./29173112.sh >> /root/Test.txt

Open in new window


This says...

1) Change to root.

2) Run the 29173112.sh script

3) Append  /root/Test.txt as the non-root user.

Try something like this should work...

sudo bash -c "./29173112.sh >> /root/Test.txt"

Open in new window

Fractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
You're talking apples and oranges.

1) Netplan sets the IP of a machine or container. Has nothing to do with DNS, if you're using dnsmasq.

2) Whereas dnsmasq provides fast cached DNS lookups. Has nothing to do with IP of machine or container.

3) Almost all Distros (past few years) have completely retired the entire resolvconf nonsense as it was fragile + barely understandable.

4) There's no big win symlinking /etc/resolv.conf some where like /var/run (tmpfs memory based file system), because the Kernel holds commonly used files in memory buffers anyway.

5) If you're using dnsmasq, you control access to /etc/resolv.conf inside the dnsmasq config file.

My approach is to disable /etc/resolv.conf + /etc/hosts in my dnsmasq config file, then load any host overrides into the dnsmasq config file I use.

This greatly simplifies configs, as the entire DNS lookup config lives in one single file then.

6) As I recall, dnsmasq only reads /etc/resolv.conf when it starts. I'm unsure as I configure all my resolvers in my dnsmasq config file.

If this is true, keep this in mind, as this means you'll have to bounce (stop/restart) dnsmasq each time you change /etc/resolv.conf which is likely the reason I switched to only using my dnsmasq config file.

In other works, changing /etc/resolv.conf will likely have no effect, till you bounce dnsmasq.