为什么 rpcbind 每次重启都会打开一个新的不同的端口?

Why is rpcbind opening a new and different port anytime it's restarted?

为了我自己的理智,有谁知道为什么 rpcbind (linux) 每次重新启动时都会打开一个看似随机的端口?我知道它使用端口 111,但是另一个一直向它开放的端口是什么?谢谢

    [root@testmachine ~]# nmap -sU -p 0-65535 127.0.0.1

    Starting Nmap 5.51 ( http://nmap.org ) at 2016-03-03 16:00 EST
    Nmap scan report for localhost (127.0.0.1)
    Host is up (0.0000080s latency).
    Not shown: 65533 closed ports
    PORT    STATE         SERVICE
    111/udp open|filtered rpcbind
    819/udp open|filtered unknown

    Nmap done: 1 IP address (1 host up) scanned in 3.11 seconds
    [root@testmachine ~]# service rpcbind restart
    Stopping rpcbind:                                          [  OK  ]
    Starting rpcbind:                                          [  OK  ]
    [root@testmachine ~]# nmap -sU -p 0-65535 127.0.0.1

    Starting Nmap 5.51 ( http://nmap.org ) at 2016-03-03 16:00 EST
    Nmap scan report for localhost (127.0.0.1)
    Host is up (0.0000080s latency).
    Not shown: 65533 closed ports
    PORT    STATE         SERVICE
    111/udp open|filtered rpcbind
    846/udp open|filtered unknown

    Nmap done: 1 IP address (1 host up) scanned in 2.97 seconds
    [root@testmachine ~]# service rpcbind restart
    Stopping rpcbind:                                          [  OK  ]
    Starting rpcbind:                                          [  OK  ]
    [root@testmachine ~]# nmap -sU -p 0-65535 127.0.0.1

    Starting Nmap 5.51 ( http://nmap.org ) at 2016-03-03 16:05 EST
    Nmap scan report for localhost (127.0.0.1)
    Host is up (0.0000070s latency).
    Not shown: 65533 closed ports
    PORT    STATE         SERVICE
    111/udp open|filtered rpcbind
    892/udp open|filtered unknown

    Nmap done: 1 IP address (1 host up) scanned in 2.86 seconds

很有可能,它是一个 RPC 服务。试试 rpcinfo 命令看看它是什么。

与大多数其他网络服务(FTP、HTTP、SMTP 等)不同,RPC 服务绑定到动态端口。 RPC 客户端不是直接连接到服务器,而是首先向 RPC 端口映射器(默认情况下 UDP/111)发送请求以找出服务器所在的端口(Windows 上使用了类似的端口) .

在相关说明中,nmap 很棒,但是有更简单的方法可以了解计算机上的侦听端口。试试这个:sudo netstat -anp | grep LISTEN。它要快得多,甚至会给你进程名称和编号。

另外,nmap 5.51 现在已经有五年了。如果您经常使用它,值得升级以获得一些新功能。

rpcbind 的 Debian 手册页告诉我:

 All RPC servers must be restarted if rpcbind is restarted.

OP 没有提到他们已经这样做了,那么 RPC 服务是如何重新注册的呢?想象一下我的惊讶,然后,在重复 OP 的实验并应用@SArcher 的 rpcinfo -p 建议以查看所有 RPC 服务仍在注册......并且在它们的原始端口上,这表明 @SArcher 并不完全在钱上。

但是,如果我们也应用@SArcher 的另一个好建议,即 sudo netstat -anp,我们会得到更有趣的东西。现在我们不能像建议的那样 |grep LISTEN 因为 OP 的 post 说 udp 和 UDP 套接字永远不会处于 LISTEN 状态。我们确实发现 rpcbind 不仅在端口 111 上有套接字 - 它的工作 - 而且还有另一个 "reserved" 端口在 rpcbind 启动时看似随机选择,正如 OP 所说。

所以"what is this other port for?"你问。不好意思开玩笑,但我只是在我的描述中回答了这个问题:

Debian bug 870579:rpcbind callit 从随机保留的 udp 端口​​回复,使防火墙变得困难

在 redhat 上有一个名为 rpcbind.socket 的单独服务。这从 rpcbind.service 开始。 rpcbind.service 首先检查端口 111 是否可用,如果不可用则选择一个端口并开始侦听该端口。 在 redhat 中,rpcbind.socket 首先启动,它开始使用端口 111。在 netstat 中,端口 111 将显示为由 systemd 使用。当 rpcbind 启动时,它发现 111 端口已经被 systemd 使用,因此它选择了一个不同的端口。如果屏蔽 rpcbind.socket 服务然后启动 rpcbind.service,rpcbind 将开始侦听端口 111.