Guides Resolving the /etc/hosts localhost Issue in Apache

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:

127.0.0.1	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 127.0.0.1] client denied by server configuration: /oursite/status

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

    # other configuration options here
    Order allow,deny
    allow from localhost

Switching in 127.0.0.1 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:

127.0.0.1	localhost

Alternatively, this should also work:

127.0.0.1	localhost	localhost.localdomain

The important point is to have localhost as the canonical name for 127.0.0.1. 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

Latest Posts

Get-MsolUser PowerShell Attributes & Properties

This article has been updated for 2020. Please note that WAAD was retired in 2018, but the cmdlets listed in this article are still...

Microsoft Azure PowerShell Scripts and Commands

Using PowerShell scripts and commands for quickly executing tasks in Windows operating systems offers a number of benefits over traditional scripting languages, such as...

Microsoft Hyper V Review

Microsoft Hyper-V: The Bottom line Microsoft Hyper-V lagged behind VMware's virtualization tool, one of the most popular tools in the space, when it was first...

Best Cloud Based Services & Companies

Any company that’s delayed introducing cloud-based software into their infrastructure needs to consider leveraging these new technologies to reap all the benefits cloud computing...

Best Load Balancers for 2020

The best load balancers are able to provide either hardware or software-based load balancing to ensure maximum service availability by offering network traffic distribution...

Related Stories