Network comprehension question: localhost vs. local IP address

Ken D'Ambrosio ken at jots.org
Tue Jun 13 23:58:02 UTC 2017


Colin's (excellent) description, below, is on the money.  However, there 
is one extra item I would add.  You mention trying to surf using your 
phone; this will only work if your phone is on the same network 
(probably via WiFi) as your server.  The reason is the "192.168.X.X" 
address you mention.  There are several "private networks" that are 
reserved for use that does not traverse the Internet, itself.  (If 
you're feeling incredibly bored and/or ambitious, feel free to google 
'rfc 1918'.)  One of these is any subnet that starts with "192.168."  
This is a subnet used specifically for in-house use; the Internet, if 
someone attempts to surf to an IP address starting in 192.168., will 
simply tell you (pardon me, I'm from New England) "You can't get there 
from here."  Most every home router/WiFi dishes out 192.168. addresses 
for home use; many companies also use them -- internally.  You mentioned 
port forwarding through your router; if you want to see what address 
that would be, try surfing to http://whatismyip.com (among others); the 
IP it shows you is what's called the "public facing" IP address -- 
that's the one that the public side of your firewall is offering up, 
and, assuming the port forwarding is set correctly, the one you would 
surf to via your phone (if not on WiFi) or from anywhere outside of your 
house.

I hope that wasn't overly confusing...

-Ken


On 2017-06-13 19:19, Colin Watson wrote:
> On Tue, Jun 13, 2017 at 08:46:46PM +0200, Volker Wysk wrote:
>> Now I'm puzzled. What is the difference between localhost and 
>> 192.168.178.20?
> 
> A host has some number of network interfaces, each of which has various
> properties such as addresses and netmasks.  Some network interfaces
> correspond to physical network ports: for example, "eth0" is one likely
> name for the first Ethernet adapter.  However, some other network
> interfaces are virtual concepts that exist only in the operating
> system's mind.  These are useful for various purposes, such as messages
> sent to other processes on the local system, bridging between virtual
> machines, tunnels, and so on.
> 
> One of these virtual network interfaces is "lo", the loopback 
> interface.
> Packets transmitted on this interface always come back to the local
> machine.  This interface can exist even on a machine that has no
> physical network ports at all.
> 
> On your system, you probably have something like this collection of
> interface names and associated addresses ("ip address" will show you
> much more detail, and there will very likely be other addresses such as
> IPv6 addresses which I've left out for brevity):
> 
>   lo: 127.0.0.1
>   eth0: 192.168.178.20
> 
> /etc/hosts will cause localhost to resolve to 127.0.0.1, because that
> pretty much always exists and can be set as a constant.  In contrast,
> 192.168.178.20 isn't necessarily a constant property of your machine -
> if you took it away and plugged it into some other network it would
> probably get a different address - and it would be much more awkward to
> arrange for localhost to resolve to whatever it happens to be at the
> current time, not to mention the situation with hosts that have 
> multiple
> externally-visible addresses.
> 
> Now, when a server binds a socket so that it can receive packets on it,
> it typically either binds to a port on all the host's network 
> interfaces
> (programs do this using a constant called INADDR_ANY, and you may
> sometimes see references to this in documentation) or to a specific
> address and port.  If it does the latter, it will only receive packets
> whose destination is that particular address, not any other address 
> that
> the host may happen to possess even if the port is correct.  It sounds
> like the server in question here bound itself specifically to
> localhost:8080, i.e. 127.0.0.1:8080 after name resolution, in which 
> case
> it won't also be listening on 192.168.178.20:8080.
> 
> You can see (for example) which listening TCP sockets are open on your
> system like this:
> 
>   sudo ss -lnpt
> 
> ... or, without abbreviation:
> 
>   sudo ss --listening --numeric --processes --tcp
> 
> In this output, local addresses shown as "*" indicate a socket bound to
> all the host's network interfaces.
> 
> --
> Colin Watson                                       
> [cjwatson at ubuntu.com]




More information about the ubuntu-users mailing list