DNS question 6 permission denied error

when i use below command
sudo echo “nameserver” >> /etc/resolv.conf
I get permission denied error. What is the work around?

Hello, @gurneeraj
Have you tried with sudo -i (switch into the root user)?

Hi gurneeraj,

While this explanation sounds plausible, this won’t work, because of an error you’ll get:

bash: /etc/resolv.conf: Permission denied

Why? The output redirection is handled by your shell (here: Bash) and is not taken into account for the command used for sudo . Similar to how multiplying in maths has precedence over adding. So, it’s just echo that has got elevated privileges, but your shell hasn’t! To solve this, one should run this command like

sudo sh -c "echo nameserver >> /etc/resolv.conf"

or, by using a pipe and tee to output it to a file:

echo nameserver | sudo tee /etc/resolv.conf  #prints to screen as well