简体   繁体   English

如何从外部连接到运行在127.0.0.1(而不是0.0.0.0)上的服务?

[英]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 | netstat -nap | grep LISTEN 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 : 我有3个问题:

1) I want to connect to the process running on 127.0.0.1 --- how can I do this externally ? 1)我想连接到127.0.0.1上运行的进程---如何在外部执行此操作? I have read elsewhere that 127.0.0.1 processes are only allowed to communicate with other localhost processes. 我在别处读过127.0.0.1进程只允许与其他localhost进程通信。

2) What is the difference between the "tcp 0" netstat records and the "unix 2" ones ? 2)“tcp 0”netstat记录与“unix 2”记录有什么区别? Im somewhat naive about networking, so feel free to overexplain this one :) 我对网络有点天真,所以随意过度解释这个:)

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. 您需要重新配置绑定到端口8020的进程以绑定到外部接口,以便能够从另一个主机连接到该外部接口。

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. 答案很长,你站点的两个地址(127.0.0.1和0.0.0.0)在某些方面都很特殊,了解你所看到的内容很有用。

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. 127.0.0.0/8 Internet协议地址块中的地址(其中127.0.0.1为一个)保留在主机内部使用。 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. 有关详细信息,请参阅rfc5735 ,但除了所有IP主机使用相同的规则并且未设置为在主机或路由器外部路由这些地址之外,这些地址没有什么特别之处。

On your computer, you'll usually see a special "loopback" network interface that has 127.0.0.1 assigned. 在您的计算机上,您通常会看到一个特殊的“环回”网络接口,该接口已分配127.0.0.1。

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. 当程序想要连接到本地计算机上的服务时使用它,因为127.0.0.1几乎总是被配置为活动网络接口。 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. 您站点的另一个地址0.0.0.0是特殊的,通常表示映射到计算机上任何网络接口的所有IP地址。 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. 当程序想要侦听到达任何网络接口或IP地址的连接时,它会将TCP / UDP端口绑定到0.0.0.0以侦听连接。

In your case, however, you're reporting netstat output listing 0.0.0.0 on lines describing TCP sockets in a LISTEN state. 但是,在您的情况下,您在报告处于LISTEN状态的TCP套接字的行上报告netstat输出列表0.0.0.0。 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. 在这种情况下,netstat列出了侦听连接的套接字,并使用0.0.0.0:*作为其输出的外部地址字段的占位符。 In this case, 0.0.0.0:* signifies that the socket is waiting for a connection from any host. 在这种情况下,0.0.0.0:*表示套接字正在等待来自任何主机的连接。

Regarding your question on "tcp 0" vs. "unix 2", these are the first two columns of your netstat output. 关于“tcp 0”与“unix 2”的问题,这些是netstat输出的前两列。 A look at the column headers from your netstat command is useful: 查看netstat命令中的列标题非常有用:

# 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. 您报告为“tcp 0”的内容仅表示使用TCP协议的套接字在接收队列中具有零字节,等待连接到此套接字的程序使用。 Similarly, "unix 2" is what's called a unix socket with two bytes waiting in its receive queue for the connected process to consume. 类似地,“unix 2”是所谓的unix套接字,其两个字节在其接收队列中等待连接进程使用。

TCP sockets are part of the TCP/IP stack that can be used locally or across IP networks for processes to communicate. TCP套接字是TCP / IP堆栈的一部分,可以在本地或跨IP网络使用,以便进行通信。 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套接字更简单,仅用于所谓的IPC或进程间通信,这只发生在本地系统上运行的两个进程之间,并且不涉及网络(无论如何都没有地址和端口)。 UNIX sockets are considered to be more efficient than TCP sockets, but they are obviously more limited in function. UNIX套接字被认为比TCP套接字更有效,但它们在功能上显然更受限制。 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. 在类UNIX系统上,UNIX套接字实现为特殊“套接字”类型的文件系统上的文件,该文件系统使用套接字读取和写入作为通信通道进行处理。

1) Without binding it to 0.0.0.0, you can still access the service through a tunnel. 1)如果不将其绑定到0.0.0.0,您仍然可以通过隧道访问服务。 This is similar to using a proxy as David Schwartz mentioned. 这与David Schwartz提到的使用代理类似。 There's a few assumptions I'm making for this example: 我为这个例子做了一些假设:

  1. The server is running a service bound to 127.0.0.1:8020, we'll call it 'myservice'. 服务器正在运行绑定到127.0.0.1:8020的服务,我们称之为'myservice'。
  2. The server is running OpenSSH server 'sshd' on the default port of TCP 22, and the user can log in with the username 'myusername'. 服务器在默认端口TCP 22上运行OpenSSH服务器'sshd',用户可以使用用户名'myusername'登录。
  3. The client is running a system with OpenSSH client installed. 客户端正在运行安装了OpenSSH客户端的系统。
  4. The server is accessible via the IP address of 10.20.30.40. 可通过10.20.30.40的IP地址访问服务器。

On the client, SSH to the server with the following command: 在客户端上,使用以下命令通过SSH连接到服务器:

ssh -L 12345:localhost:8020 myusername@10.20.30.40

Once you log in, minimize the SSH window. 登录后,最小化SSH窗口。 In another window on the client, run netstat to find listening ports. 在客户端的另一个窗口中,运行netstat以查找侦听端口。 You should see 127.0.0.1:12345, just like on the server. 您应该看到127.0.0.1:12345,就像在服务器上一样。

On the client, connect to the service on 127.0.0.1:12345. 在客户端上,连接到127.0.0.1:12345上的服务。 You should now be connected to the 'myservice' instance on the server, even though you made the connection to the client's local loopback interface. 您现在应该连接到服务器上的'myservice'实例,即使您已连接到客户端的本地环回接口。

The trick here is that SSH is tunneling a listening socket on the client to the listening socket on the server. 这里的技巧是SSH将客户端上的侦听套接字隧道连接到服务器上的侦听套接字。 I've made the port numbers different for clarity. 为清晰起见,我将端口号设置为不同。

1) You would either need to modify the server to bind to a publicly accessible address (or 0.0.0.0) or run a local proxy to handle the connection. 1)您需要修改服务器以绑定到可公共访问的地址(或0.0.0.0)或运行本地代理来处理连接。

2) TCP connections use the TCP protocol, the one used for connection-oriented traffic on the Internet. 2)TCP连接使用TCP协议,该协议用于Internet上面向连接的流量。 UNIX connections use a strictly local protocol that is much simpler than TCP (because it doesn't have to deal with dropped packets, lost routes, corrupted data, out of order packets, and so on). UNIX连接使用严格的本地协议,它比TCP简单得多(因为它不必处理丢弃的数据包,丢失的路由,损坏的数据,乱序数据包等)。

1) You cannot (if you mean from another machine - 127.0.0.1 is localhost and by definition you can only connect to it from the local machine 1)您不能(如果您的意思是来自另一台机器 - 127.0.0.1是localhost,根据定义,您只能从本地机器连接到它

2) The first column shows the domain of the sockets - tcp are tcp sockets and unix are unix domain sockets. 2)第一列显示套接字的域 - tcp是tcp套接字,unix是unix域套接字。

And as for the answer to you question 3 ;-) 至于问题3的答案;-)

3) 42 3)42

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM