4

I was playing with the /etc/hosts file on my Raspbian installation and outcommented the "127.0.1.1 raspberry" line. I know that the host file is basically there to facilitate address look up. However, by doing this, it started giving me following error whenever I try to run a command with sudo.

unable to resolve host raspberrypi

Also, I noticed an abnormal behavior. For example, running df -h when it's been outcommented. It shows my rootfs being full.

pi@raspberrypi ~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  6.9G     0 100% /
devtmpfs        364M     0  364M   0% /dev
tmpfs            74M  244K   74M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           148M     0  148M   0% /run/shm
/dev/mmcblk0p1   56M   20M   37M  35% /boot
tmpfs           148M     0  148M   0% /tm

While uncommenting it and bringing it back to normal, it makes it normal as follows:

pi@raspberrypi ~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  3.3G  3.6G  49% /
devtmpfs        364M     0  364M   0% /dev
tmpfs            74M  244K   74M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           148M     0  148M   0% /run/shm
/dev/mmcblk0p1   56M   20M   37M  35% /boot
tmpfs           148M     0  148M   0% /tmp

I followed a couple of links, like this and this, but I couldn't figure out why it's happening and how removing a host affects applications in general. What is the reason?

7

Using 127.0.1.1 in addition to 127.0.0.1 is just a Debian convention; here's the justification. It is not a bad thing. These two addresses amount to the same thing anyway; a more widespread methodology would be to use an alias:

127.0.0.1 localhost.localdomain raspberry.pi

But using a distinct address is perhaps a better practice since it would allow you to distinguish one from the other in certain situations.

The purpose of /etc/hosts is described in man 5 hosts.

whenever I try to run a command with sudo

For an explanation of why that is, see here, which I'll briefly quote:

The /etc/sudoers file is designed to be able to be distributed among multiple servers. In order to accomplish this, each permission in the file has a host portion. [...] In order for sudo to know whether this rule should be applied, it needs to lookup the host it is running on. It uses a call that relies on the /etc/hosts being correct, which is why it fails if it is not right.

As for

how removing host affects applications in general

Applications use networking mechanisms to communicate with each other, and if these are IP based they may use localhost to do this. The loopback interface is conceptual and implemented in the kernel (i.e., it doesn't involve actual network hardware). I don't think that explains your df issue, but if it is consistently reproducible (I could not get the output of df to change by commenting everything in /etc/hosts out) then obviously there's some connection.

  • Upvote. Totally agree. One thing to remember (if you are used to working with Windows hosts files), is that Raspbian does not automatically take care of the local system's hosts entry so it needs to be explicit. – SDsolar May 2 '17 at 21:11
3

The /etc/hosts file is actually there to make DNS lookup unnecessary for domains/hostnames.

e.g. if you add an entry to your /etc/hosts file of

127.0.0.1     www.google.com

the computer will not perform a DNS lookup for google.com, it will take you to the IP address in the hosts file entry.

127.0.0.1 is the internal IP address of your computer, so by removing the "127.0.0.1 raspberrypi" entry from it, your computer is trying to perform a DNS lookup on the domain "raspberrypi". This domain does not exist, hence the error message.

Read What is the use of /etc/hosts? for more.

  • But how does it affect while I run any program on shell ? and of course why ? – dhruvvyas90 Jun 28 '15 at 10:12
  • This only answers the purpose of /etc/hosts but not the reason for the 100% on / df -h issue. – Greenonline Jun 28 '15 at 19:03
  • You can also add a no-fly list in your hosts file to prevent you from going to annoying sites like Forbes.com or washingtonpost.com - Just put in an entry for it and redirect it somewhere you like, such as 127.0.0.1 or, my favorite, the address for Google. – SDsolar May 2 '17 at 21:13
  • If your Rpi has static IP addresses, it never hurts to include them in the hosts file. But there also is no reason for it if your hostname is set correctly. since you are unlikely to remote into your own system. – SDsolar May 2 '17 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.