Can someone please tell me what sudo echo nameserver > /etc/resolv.conf does?

I used to have random dropping of internet connection for 15 seconds to 3 mins but after using this command, internet connection is working like a charm. I am just curious what this magic command line did.

  • 3
    I'm surprised this actually works -- sudo should only apply to echo here, not to the redirection, and a non-root user shouldn't have access to /etc/resolv.conf
    – Max
    Jan 30, 2013 at 8:12
  • @Max You're absolutely right. I missed that part - updated my answer.
    – gertvdijk
    Jan 30, 2013 at 9:36

1 Answer 1


It replaces the nameserver (DNS) used on your machine by the Google Public DNS service. If running that command helps you out, you're having DNS issues in the default settings provided by your network probably (DHCP). While it would be better to resolve the root cause of the issue, this is a general answer to your question.

Explanation of the command

sudo echo nameserver > /etc/resolv.conf
  • sudo elevates privileges to be root in this case.
  • echo just outputs the arguments following to the standard output (in this case it outputs nameserver
  • > redirects standard output to a file (overwrites it) in a regular Unix shell.
  • /etc/resolv.conf is the file where the output is written to.
  • Having a line nameserver in /etc/resolv.conf makes the system using the nameserver having IP address for resolving domain names to IP addresses. (very short version of what actually happens)
  • is one of the addresses published by Google as to where their public DNS services are listening on.

sudo mistake

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

Ubuntu and Network Manager

On Ubuntu this is only a temporary change, as Network Manager "manages" the /etc/resolv.conf file. Local modifications will be overwritten. That's why you should configure this in Network Manager to have it persistent.

Configuration using Network Manager

I would recommend configuring your PC properly using Network Manager. I'm assuming you're using DHCP on your network here (most common). Then do this to perform the same as equivalent to the command you were using:

  1. Open Edit connections:

    enter image description here

  2. Edit the connection profile you're using.

    enter image description here

  3. On the IPv4 Settings tab:

    • Method: Automatic (DHCP) addresses only

      This setting makes it doing a regular DHCP request for IP address configuration of your host, but it will ignore other non-mandatory options like DNS servers suggested.

    • DNS Servers:
    • Hit Save

    enter image description here


Important notes

In a local or corporate network the local DNS server might be used for local hosts. You'll loose this functionality by directly asking Google's servers to resolve names for you. So, if for example the local DNS server would know what "printer" is, then you'll loose using your printer by that name.

So, please, fix your local DNS server instead. If it's just a simple forwarder to a broken DNS server of your ISP and you can't change that (ISP-managed all-in-one-router for example), then this solution might be your only option.

  • 1
    Another important note: google is presumably collecting on who is looking up what in DNS, so think about whether you are comfortable with that. There are plenty of other public DNS servers you could use, and they generally are reliable. Sounds like the OP was using a bad DNS server, which may or may not have been a local one. (I've had commodity ADSL routers that do a remarkably poor job with dns).
    – mc0e
    Jan 27, 2015 at 5:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .