What's the difference between a machine's IP address and localhost
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?
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
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
tap devices can use them too. Again, the name resolution (for example
10.0.1.1) can be configured in
/etc/hosts or can be set up to use DNS.
When you access
/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.
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).