11

Doubt has raised from my another question Where if i give localhost to my path, it works. But it doesn't work if i give my system ip.

127.0.0.1 is mapped to localhost in my /etc/hosts. Do i need to map my ip to localhost? Doesn't change?

Aren't they same?

13

Some services are configured to only listen on the localhost IP address.

An example would be a MySQL database - you want your PHP application running on the same server to connect to it, but don't want any external services or even hackers from the outside to connect. By configuring MySQL to only accept localhost addresses (127.0.0.1 for example) and not your server's real IP address (10.x.x.x for example) you are reducing the chance of being compromised.

So, to answer your question - yes, they are different.

localhost is given an ip address in the 127.0.0.0 network and given to a virtual loopback network device lo. This device is present on all systems, regardless of whether they have a physical network device fitted (WiFi or Ethernet, for example). A system that is not connected to any network will have this loopback device and hence a 127.0.0.0 address. The name localhost is simply a name that resolves to this IP address and is configured in /etc/hosts.

Your real IP address (10.x.x.x for example) is allocated to a network device. This is usually a physical network device (WiFi or Ethernet) although advanced setups using tun or tap devices can use them too. Again, the name resolution (for example www.example.org to 10.0.1.1) can be configured in /etc/hosts or can be set up to use DNS.

4

127.0.0.1 is localhost, it's the address of the current machine, accessed through a loopback interface (not through the network adapter - this works even if there is no network chips in the system).

The ip that you get from the router is a different story: it's the address that allows other computer on the network to find you. Well, you can use that ip on the same machine too, but it works differently as before: it's going out to the router and in again (I'm simplifying here, but that's the general idea).

And if you are connected to the internet, you also get another ip from your ISP - the ip by which other computer all over the world find you. However, these two at least refer to the same network adapter (eth0 or the wireless card or whatever). localhost is different.

The operating system, the servers and so on... can behave differently when accessed from localhost. You usually don't have a firewall for that, and many local services have a localhost interface that is meant simply to communicate with an application. Many times, you will want to test your web server by first hooking it up to listen on localhost only, so you can check if it works, but other people can't. Then, you can reconfigure to listen on your external ip address and start serving the website (and test again if everything still works).

  • Thanks for long explanation. Another doubt raised: If i say my service to localhost, it ll not go to router. But if i specify ip, it ll b redirected by router. So i must have internet connection in my second case. Not with the localhost. Please let me know – GoMan Feb 12 '15 at 14:19
  • You need a connection to the router, otherwise you will get network is unreachable or something similar. The trick is that (at least with dhcp) it's the router that gives you a LAN ip in the first place. It's possible that this varies in some cases if the route persists, but in essence, for LAN ip, you need LAN, and for external ip, you need a connection to ISP. – orion Feb 12 '15 at 14:44
3

When you access localhost, your /etc/hosts file will tell your computer not to look any further and redirects you to your own computer. When you access the local IP adress, your computer will ask the router to fetch the data, and your router will then point back to your computer.

  • 3
    @serenesat - No. When you access your 'real' IP address, you will not go as far as the router - you won't even leave that machine. Trying pinging your local IP address and look at the round trip times... – garethTheRed Feb 12 '15 at 14:38
  • This answer is plain wrong. – Rui F Ribeiro Jan 2 at 15:30

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.