What does "sudo echo nameserver 8.8.8.8 > /etc/resolv.conf" do?

Can someone please tell me what sudo echo nameserver 8.8.8.8 > /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.


Solution 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 8.8.8.8 > /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 8.8.8.8)
  • > 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 8.8.8.8 in /etc/resolv.conf makes the system using the nameserver having IP address 8.8.8.8 for resolving domain names to IP addresses. (very short version of what actually happens)
  • 8.8.8.8 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 8.8.8.8 > /etc/resolv.conf"

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

echo nameserver 8.8.8.8 | 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: 8.8.8.8
    • Hit Save

    enter image description here

Done.


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 8.8.8.8 solution might be your only option.