Avatar of Johnny
Johnny
Flag for United States of America asked on

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:~$
DNSUbuntuLinuxLinux OS DevLinux Distributions

Avatar of undefined
Last Comment
David Favor

8/22/2022 - Mon
David Favor

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.
arnold

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 N

@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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
MURUGESAN N

@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 N

@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

Johnny

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
arnold

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 Favor

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

ASKER CERTIFIED SOLUTION
David Favor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.