Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i externally connect to a service running on 127.0.0.1 (rather than 0.0.0.0)?

I'm trying to connect to a service, and to debug it, I ran

netstat -nap | grep LISTEN

The results should rows of two types :

tcp 0 0 127.0.0.1:8020 0.0.0.0:*     LISTEN      
tcp 0 0 0.0.0.0:57140  0.0.0.0:*     LISTEN      
tcp 0 0 0.0.0.0:11000  0.0.0.0:*     LISTEN      
tcp 0 0 0.0.0.0:8088   0.0.0.0:*     LISTEN 
unix 2 [ ACC ]  STREAM LISTENING     4512   -                   
unix 2 [ ACC ]  STREAM LISTENING     9760   -                   

I have 3 questions :

1) I want to connect to the process running on 127.0.0.1 --- how can I do this externally ? I have read elsewhere that 127.0.0.1 processes are only allowed to communicate with other localhost processes.

2) What is the difference between the "tcp 0" netstat records and the "unix 2" ones ? Im somewhat naive about networking, so feel free to overexplain this one :)

like image 552
jayunit100 Avatar asked Oct 31 '11 02:10

jayunit100


People also ask

What is the IP address 127.0 0.1 used for?

Localhost is the default name of the computer you are working on. The term is a pseudo name for 127.0. 0.1, the IP address of the local computer. This IP address allows the machine to connect to and communicate with itself.

What is the difference between 0.0 0.0 and localhost?

127.0. 0.1 is the loopback address (also known as localhost). 0.0. 0.0 is a non-routable meta-address used to designate an invalid, unknown or non applicable target (a no particular address placeholder).

What is the 127.0 0.0 address?

The IP address range 127.0. 0.0 – 127.255. 255.255 is reserved for loopback, i.e. a Host's self-address, also known as localhost address. This loopback IP address is managed entirely by and within the operating system.

What is localhost in Linux?

In computer networking, localhost is a hostname that refers to the current device used to access it. It is used to access the network services that are running on the host via the loopback network interface. Using the loopback interface bypasses any local network interface hardware.


1 Answers

In short, your process is bound to a loopback interface which cannot receive packets from an external network. You'll need to reconfigure the process bound to port 8020 to bind to an external interface to be able to connect to it from another host.

The long answer is that the two addresses you site (127.0.0.1 and 0.0.0.0) are both special in certain ways, and it is useful to understand what you're seeing.

Addresses in the 127.0.0.0/8 Internet Protocol address block (of which 127.0.0.1 is one) are reserved for use internally on a host. See rfc5735 for details, but there's nothing special about these addresses except that all IP hosts use the same rules and aren't setup to route these addresses outside a host or router.

On your computer, you'll usually see a special "loopback" network interface that has 127.0.0.1 assigned.

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0

This interface is special and never connected to an external network. It is used when a program wants to connect to a service on the local machine as 127.0.0.1 will almost always be configured as an active network interface. Packets will only arrive on this interface if they are sent from a local process.

The other address you site, 0.0.0.0 is special and usually represents all IP addresses mapped to any network interface on your computer. When a program wants to listen for connections arriving on any network interface or IP address, it will bind a TCP/UDP port to 0.0.0.0 to listen for connections.

In your case, however, you're reporting netstat output listing 0.0.0.0 on lines describing TCP sockets in a LISTEN state. In this case, netstat is listing sockets listening for connections and using 0.0.0.0:* as a place holder for the foreign address field of it's output. In this case, 0.0.0.0:* signifies that the socket is waiting for a connection from any host.

Regarding your question on "tcp 0" vs. "unix 2", these are the first two columns of your netstat output. A look at the column headers from your netstat command is useful:

# netstat -nap | head -2
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name 

What you're reporting as "tcp 0" simply means a socket using the TCP protocol has zero bytes in the received queue waiting for the program connected to this socket to consume. Similarly, "unix 2" is what's called a unix socket with two bytes waiting in its receive queue for the connected process to consume.

TCP sockets are part of the TCP/IP stack that can be used locally or across IP networks for processes to communicate. UNIX sockets, on the other hand, are simpler and only used for what's called IPC or inter-process communication which only happens between two processes both running on the local system, and there's no networking involved (no addresses and ports anyway). UNIX sockets are considered to be more efficient than TCP sockets, but they are obviously more limited in function. On UNIX-like systems UNIX sockets are implemented as a file on the file system of a special "socket" type that both processes using the socket read and write to as a communication channel.

like image 64
bigendian Avatar answered Oct 28 '22 06:10

bigendian