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?
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?
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.
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.
In theory, there are cases where it might not exist. In practice, it's always there.
Ok.
The reason why it resolves it is record in %WINDOWS_DIR%\System32\drivers\etc\hosts file like this:
127.0.0.1 localhost
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
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.
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
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.
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.
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.