确定哪个进程绑定到端口

106 networking process lsof tcp

我知道使用命令:

lsof -i TCP 
Run Code Online (Sandbox Code Playgroud)

(或 lsof 参数的一些变体)我可以确定哪个进程绑定到特定端口。这很有用,如果我试图启动一些想要绑定到 8080 的东西,而其他人已经在使用该端口,但我不知道是什么。

有没有一种简单的方法可以在不使用 lsof 的情况下做到这一点?我花时间在许多系统上工作,但通常没有安装 lsof。

Cak*_*mox 136

netstat -lnp将在每个侦听端口旁边列出 pid 和进程名称。这将在 Linux 下工作,但不是所有其他人(如 AIX)。-t如果您只需要 TCP,请添加。

# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:24800           0.0.0.0:*               LISTEN      27899/synergys
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      3361/python
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2264/mysqld
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      22964/apache2
tcp        0      0 192.168.99.1:53         0.0.0.0:*               LISTEN      3389/named
tcp        0      0 192.168.88.1:53         0.0.0.0:*               LISTEN      3389/named
Run Code Online (Sandbox Code Playgroud)

等等。


fri*_*elp 12

在 AIX 上,netstat & rmsock 可用于确定进程绑定:

[root@aix] netstat -Ana|grep LISTEN|grep 80
f100070000280bb0 tcp4       0      0  *.37               *.*        LISTEN
f1000700025de3b0 tcp        0      0  *.80               *.*        LISTEN
f1000700002803b0 tcp4       0      0  *.111              *.*        LISTEN
f1000700021b33b0 tcp4       0      0  127.0.0.1.32780    *.*        LISTEN

# Port 80 maps to f1000700025de3b0 above, so we type:
[root@aix] rmsock f1000700025de3b0 tcpcb
The socket 0x25de008 is being held by process 499790 (java).
Run Code Online (Sandbox Code Playgroud)

  • @OlivierDulac:“与其名称所暗示的不同,rmsock 不会删除套接字,如果它正在被进程使用。它只是报告持有套接字的进程。” (https://www.ibm.com/developerworks/community/blogs/cgaix/entry/rmsock?lang=en) (2认同)

fri*_*elp 8

Linux 上可用的另一个工具是ss。从Fedora 上的ss手册页:

NAME
       ss - another utility to investigate sockets
SYNOPSIS
       ss [options] [ FILTER ]
DESCRIPTION
       ss is used to dump socket statistics. It allows showing information 
       similar to netstat. It can display more TCP and state informations  
       than other tools.
Run Code Online (Sandbox Code Playgroud)

下面的示例输出 - 最后一列显示了流程绑定:

[root@box] ss -ap
State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port
LISTEN     0      128                    :::http                    :::*        users:(("httpd",20891,4),("httpd",20894,4),("httpd",20895,4),("httpd",20896,4)
LISTEN     0      128             127.0.0.1:munin                    *:*        users:(("munin-node",1278,5))
LISTEN     0      128                    :::ssh                     :::*        users:(("sshd",1175,4))
LISTEN     0      128                     *:ssh                      *:*        users:(("sshd",1175,3))
LISTEN     0      10              127.0.0.1:smtp                     *:*        users:(("sendmail",1199,4))
LISTEN     0      128             127.0.0.1:x11-ssh-offset                  *:*        users:(("sshd",25734,8))
LISTEN     0      128                   ::1:x11-ssh-offset                 :::*        users:(("sshd",25734,7))
Run Code Online (Sandbox Code Playgroud)