tags:

views:

1451

answers:

10

Is it always possible to ping localhost and it resolves to 127.0.0.1?

I know Windows Vista, XP, Ubuntu and Debian do it but does everyone do it?

+13  A: 

Any correct implementation of TCP/IP will reserve the address 127.0.0.1 to refer to the local machine. However, the mapping of the name "localhost" to that address is generally dependent on the system hosts file. If you were to remove the localhost entry from hosts, then the localhost name may no longer resolve properly at all.

Greg Hewgill
It is not dependent on the hosts files. It is part of the ip stack. Even under windows there is a separate interface for it (they call them network devices on windows.)
Chris
Greg was referring to the 'localhost' name, not the loopback device. Two different things.
Cody Brocious
i'd like to have this edited to "any CORRECT TCP/IP implementation"I've come across a couple of bad ones, one of them didn't have a loopback IP.
Mez
@Martin Meredith: fixed, thanks.
Greg Hewgill
I think it's safe to rely on the "localhost" alias existing, since it's set by default in any modern OS. Users would have to actually *remove* it from the host list, and there's no point worrying about ways users can break their computers.
John Millikin
A: 

I think localhost pretty much resolves to 127.0.0.1 for most platforms but all IPs that start with 127...* resolve to localhost as well. Try pinging 127.255.255.254 and it'll still respond.

cruizer
Not the case for me (OSX Tiger)
da5id
That's an implementation issue for OSX. 127.* is supposed to resolve to the local machine in all cases, IIRC.
Chris Charabaruk
surely that address is a loopback broadcast address?
Mez
A: 

In theory, there are cases where it might not exist. In practice, it's always there.

Cody Brocious
A: 

Ok.

The reason why it resolves it is record in %WINDOWS_DIR%\System32\drivers\etc\hosts file like this:

127.0.0.1 localhost

dimarzionist
+6  A: 

127.0.0.1 is reserved in any IP stack for the local host.

"localhost" as a host name is not guaranteed to be there. If the host/DNS settings are misconfigured, localhost will not resolve.

Example on a debian box:

topaz:/root# vi /etc/hosts
[comment out localhost entry]

topaz:/root# ping localhost 
ping: unknown host localhost
Thorsten79
Something defined in /etc/hosts is not a DNS name. Check the man page for nsswitch.conf and see the "hosts:" setting.
ΤΖΩΤΖΙΟΥ
Thanks for the comment, I've tried to make it more clear.
Thorsten79
+5  A: 

No. For a start localhost is a convention rather than a rule. Mostly it's set by default, but there's nothing to mandate it.

Secondly, there's nothing to say that you can always ping 127.0.0.1. As an example (on a unix system) try the following:

sudo ifconfig lo down ping 127.0.0.1

As cruizer said, 127.0.0.1 (if it exists) is defined to be the local machine. But it doesn't have to exist.

Andrew Edgecombe
+5  A: 

The pedantic answer (sorry, Greg :), is to read RFC 3330:

   127.0.0.0/8 - This block is assigned for use as the Internet host
   loopback address.  A datagram sent by a higher level protocol to an
   address anywhere within this block should loop back inside the host.
   This is ordinarily implemented using only 127.0.0.1/32 for loopback,
   but no addresses within this block should ever appear on any network
   anywhere [RFC1700, page 5].

(The "ordinarily" above should probably be read as "often" - most current operating systems support using all of 127.0.0.0/8 as loopback.)

With regards to whether "localhost" always resolves to 127.0.0.1 - he is correct, it's generally the same, but technically implementation specific:

~> dig localhost.t...e.org

...

;; ANSWER SECTION:
localhost.t...e.org. 86400 IN    A       127.0.0.2
Dominic Eidson
+1  A: 

If the DNS servers your client is connected to is following rfc1912 then yes, localhost should resolve to 127.0.0.1.

RFC1912
4.1
...
   Certain zones should **always be present** in nameserver configurations:
           primary         localhost               localhost
           primary         0.0.127.in-addr.arpa    127.0
...
   The "localhost" address is a "special" address which always refers to
   the local host.  It should contain the following line:

           localhost.      IN      A       127.0.0.1

   The "127.0" file should contain the line:

           1    PTR     localhost.
Zoredache
Most DNS servers don't obey RFC1912 to be honest. Its a sad thing about ISPs.
Sargun Dhillon
A: 

Decent firewalls allow you to filter access on the loopback interfaces too. So, it's possible to set up a firewall rule that drops icmp ping packets going to localhost (127.0.0.1). Also, as everyone else has already mentioned, even the existence of the localhost or 127.0.0.1 address and the loopback interface isn't guaranteed.

Alexander
A: 

The answer is:

127.0.0.1, often referred to as the "loopback", is required. Although your computer might let you do silly things, like disable it, or configure that range on a physical interface, these are all invalid.

"localhost" is just a hostname, which by convention, should be 127.0.0.1 As a system administrator or hostmaster, you should avoid configurations that allow localhost to point to other addresses.

You should not edit your hosts file to change the address of "localhost". You should configure your domains to have a localhost. and localhost.domain.com entry that points to 127.0.0.1.

You should not let your proxy servers respond to "localhost" or any FQDN that starts with localhost.

benc