Resolving the /etc/hosts localhost Issue in Apache

This week I once again ran into a problem I've encountered before: issues caused by the default values in /etc/hosts used in several Linux distributions, including older versions of Debian and Ubuntu. It seems to have been fixed in Debian Lenny, but I've encountered it on at least one recently installed machine, so other applications may be overwriting the file incorrectly.

Tip of the Trade: The default values in /etc/hosts used in several Linux distributions have long been known to cause issues. Here's how to fix them in Apache 2.

The problematic localhost line in /etc/hosts looks like this:	localhost.localdomain	localhost

Unfortunately, this setup confuses some applications. I've seen this cause problems with kpropd and other Kerberos apps. A host with this setup will identify itself locally as host/localhost.localdomain, whereas the Kerberos setup usually expects host/localhost.

This week, I encountered problems with Apache 2 access restrictions. Access to a particular page was refused, with this error message in the logs:

[error] [client] client denied by server configuration: /oursite/status

The relevant part of the Apache2 config section for that directory was:

<Location /status>
    # other configuration options here
    Order allow,deny
    allow from localhost

Switching in for localhost in the Apache2 config got things working again. However, the better fix was to change that back and instead edit /etc/hosts to read:	localhost

Alternatively, this should also work:	localhost	localhost.localdomain

The important point is to have localhost as the canonical name for There's some discussion of this on an old thread on the debian-devel list.

Note: Another option would have been to edit the Apache2 config to read

allow from localhost.localdomain

but as this setting in /etc/hosts is known to cause other problems (and, indeed, is arguably Just Wrong!), it makes more sense to fix it there.

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of "Linux System Administration Recipes: A Problem-Solution Approach" (Apress, 2009).

Follow ServerWatch on Twitter

This article was originally published on Feb 1, 2010
Page 1 of 1

Thanks for your registration, follow us on our social networks to keep up-to-date