看本機上的 port 被哪個 process 佔用

有時候在本機建立 server 會碰到 port 被其他 process 佔用的狀況,但一下想不起來是哪隻程式用掉原本想用的 port。可以用以下幾種方式來找出對應的程式:

netstat

netstat 用來顯示目前的網路使用狀況。

$ sudo netstat -ltupn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      530/systemd-resolve
tcp        0      0 127.0.0.1:9999          0.0.0.0:*               LISTEN      2265693/php
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      2028642/cupsd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      736/sshd: /usr/sbin
tcp6       0      0 ::1:631                 :::*                    LISTEN      2028642/cupsd
tcp6       0      0 :::80                   :::*                    LISTEN      2251835/apache2
tcp6       0      0 :::22                   :::*                    LISTEN      736/sshd: /usr/sbin
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           583/avahi-daemon: r
udp        0      0 0.0.0.0:32798           0.0.0.0:*                           583/avahi-daemon: r
udp        0      0 127.0.0.53:53           0.0.0.0:*                           530/systemd-resolve
udp        0      0 0.0.0.0:631             0.0.0.0:*                           2028644/cups-browse
udp6       0      0 :::5353                 :::*                                583/avahi-daemon: r
udp6       0      0 :::49250                :::*                                583/avahi-daemon: r
udp6       0      0 fe80::ce82:cafe:25b:546 :::*                                595/NetworkMana

-l 表示只顯示正在 listen 的 process,-t-u 是顯示 TCP 跟 UDP(如果都沒加,會連 unix socket 一起顯示出來)。-p 會顯示對應的 process 的 PID 跟 program name。-n 則是不進行名稱轉換(顯示 IP 而不是 hostname;顯示 :80 而不是 :http

如果不是用 root 權限執行,會看不到自己沒有 own 的 process 的資訊。

$ netstat -ltupn
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:9999          0.0.0.0:*               LISTEN      2265693/php
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp6       0      0 ::1:631                 :::*                    LISTEN      -
tcp6       0      0 :::80                   :::*                    LISTEN      -
tcp6       0      0 :::22                   :::*                    LISTEN      -
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           -
udp        0      0 0.0.0.0:32798           0.0.0.0:*                           -
udp        0      0 127.0.0.53:53           0.0.0.0:*                           -
udp        0      0 0.0.0.0:631             0.0.0.0:*                           -
udp6       0      0 :::5353                 :::*                                -
udp6       0      0 :::49250                :::*                                -
udp6       0      0 fe80::ce82:cafe:25b:546 :::*                                -

ss

ss 也是個用來觀看 socket 使用狀況的工具。

$ sudo ss -ltup
Netid  State   Recv-Q  Send-Q                         Local Address:Port             Peer Address:Port  Process
udp    UNCONN  0       0                                    0.0.0.0:mdns                  0.0.0.0:*      users:(("avahi-daemon",pid=583,fd=12))
udp    UNCONN  0       0                                    0.0.0.0:32798                 0.0.0.0:*      users:(("avahi-daemon",pid=583,fd=14))
udp    UNCONN  0       0                              127.0.0.53%lo:domain                0.0.0.0:*      users:(("systemd-resolve",pid=530,fd=12))
udp    UNCONN  0       0                                    0.0.0.0:631                   0.0.0.0:*      users:(("cups-browsed",pid=2028644,fd=7))
udp    UNCONN  0       0                                       [::]:mdns                     [::]:*      users:(("avahi-daemon",pid=583,fd=13))
udp    UNCONN  0       0                                       [::]:49250                    [::]:*      users:(("avahi-daemon",pid=583,fd=15))
udp    UNCONN  0       0        [fe80::ce82:cafe:25b:b40d]%enp2s0f0:dhcpv6-client            [::]:*      users:(("NetworkManager",pid=595,fd=23))
tcp    LISTEN  0       4096                           127.0.0.53%lo:domain                0.0.0.0:*      users:(("systemd-resolve",pid=530,fd=13))
tcp    LISTEN  0       128                                  0.0.0.0:ssh                   0.0.0.0:*      users:(("sshd",pid=736,fd=3))
tcp    LISTEN  0       5                                  127.0.0.1:ipp                   0.0.0.0:*      users:(("cupsd",pid=2028642,fd=7))
tcp    LISTEN  0       4096                               127.0.0.1:9999                  0.0.0.0:*      users:(("php",pid=2265693,fd=4))
tcp    LISTEN  0       128                                     [::]:ssh                      [::]:*      users:(("sshd",pid=736,fd=4))
tcp    LISTEN  0       5                                      [::1]:ipp                      [::]:*      users:(("cupsd",pid=2028642,fd=6))
tcp    LISTEN  0       511                                        *:http                        *:*      users:(("apache2",pid=2251842,fd=4),("apache2",pid=2251841,fd=4),("apache2",pid=2251840,fd=4),("apache2",pid=2251839,fd=4),("apache2",pid=2251838,fd=4),("apache2",pid=2251835,fd=4))

-l 表示只顯示正在 listen 的 process,-t-u 是顯示 TCP 跟 UDP(如果都沒加,會連 unix socket 一起顯示出來)。-p 會顯示對應的 process 的 PID 跟 program name。(這參數跟 netstat 一個模子...)

如果不是用 root 權限執行,同樣會看不到自己沒有 own 的 process 的資訊。

$ ss -ltup
Netid   State    Recv-Q   Send-Q                           Local Address:Port               Peer Address:Port   Process
udp     UNCONN   0        0                                      0.0.0.0:mdns                    0.0.0.0:*
udp     UNCONN   0        0                                      0.0.0.0:32798                   0.0.0.0:*
udp     UNCONN   0        0                                127.0.0.53%lo:domain                  0.0.0.0:*
udp     UNCONN   0        0                                      0.0.0.0:631                     0.0.0.0:*
udp     UNCONN   0        0                                         [::]:mdns                       [::]:*
udp     UNCONN   0        0                                         [::]:49250                      [::]:*
udp     UNCONN   0        0          [fe80::ce82:cafe:25b:b40d]%enp2s0f0:dhcpv6-client              [::]:*
tcp     LISTEN   0        4096                             127.0.0.53%lo:domain                  0.0.0.0:*
tcp     LISTEN   0        128                                    0.0.0.0:ssh                     0.0.0.0:*
tcp     LISTEN   0        5                                    127.0.0.1:ipp                     0.0.0.0:*
tcp     LISTEN   0        4096                                 127.0.0.1:9999                    0.0.0.0:*       users:(("php",pid=2265693,fd=4))
tcp     LISTEN   0        128                                       [::]:ssh                        [::]:*
tcp     LISTEN   0        5                                        [::1]:ipp                        [::]:*
tcp     LISTEN   0        511                                          *:http                          *:*

lsof

lsof 原本是用來列出系統中被開啟的檔案,但也可以用來顯示網路使用狀況。

$ sudo lsof -i -P -n
COMMAND       PID            USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
systemd-r     530 systemd-resolve   12u  IPv4    29240      0t0  UDP 127.0.0.53:53
systemd-r     530 systemd-resolve   13u  IPv4    29241      0t0  TCP 127.0.0.53:53 (LISTEN)
avahi-dae     583           avahi   12u  IPv4    35056      0t0  UDP *:5353
avahi-dae     583           avahi   13u  IPv6    35057      0t0  UDP *:5353
avahi-dae     583           avahi   14u  IPv4    35058      0t0  UDP *:32798
avahi-dae     583           avahi   15u  IPv6    35059      0t0  UDP *:49250
NetworkMa     595            root   23u  IPv6    55663      0t0  UDP [fe80::ce82:cafe:25b:b40d]:546
sshd          736            root    3u  IPv4    38280      0t0  TCP *:22 (LISTEN)
sshd          736            root    4u  IPv6    38282      0t0  TCP *:22 (LISTEN)
cupsd     2028642            root    6u  IPv6 13108528      0t0  TCP [::1]:631 (LISTEN)
cupsd     2028642            root    7u  IPv4 13108529      0t0  TCP 127.0.0.1:631 (LISTEN)
cups-brow 2028644            root    7u  IPv4 13103966      0t0  UDP *:631
sshd      2218598            root    4u  IPv4 14234370      0t0  TCP 192.168.33.101:22->192.168.33.102:57948 (ESTABLISHED)
sshd      2218679              me    4u  IPv4 14234370      0t0  TCP 192.168.33.101:22->192.168.33.102:57948 (ESTABLISHED)
apache2   2251835            root    4u  IPv6 14288964      0t0  TCP *:80 (LISTEN)
apache2   2251838        www-data    4u  IPv6 14288964      0t0  TCP *:80 (LISTEN)
apache2   2251839        www-data    4u  IPv6 14288964      0t0  TCP *:80 (LISTEN)
apache2   2251840        www-data    4u  IPv6 14288964      0t0  TCP *:80 (LISTEN)
apache2   2251841        www-data    4u  IPv6 14288964      0t0  TCP *:80 (LISTEN)
apache2   2251842        www-data    4u  IPv6 14288964      0t0  TCP *:80 (LISTEN)
php       2265693              me    4u  IPv4 14352574      0t0  TCP 127.0.0.1:9999 (LISTEN)

然後就可以用 command 名稱或 PID 找到對應的程式。

-i 告訴 lsof 要顯示 network files,-n 會顯示 ip 而不是 hostname(速度比較快),-P 顯示 port 數字而不是 port 的名稱(速度比較快)。

如果不是用 root 權限執行,會看不到自己沒有 own 的 process 所開的網路連線。

$ lsof -i -n -P
COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
php     2265693   me    4u  IPv4 14352574      0t0  TCP 127.0.0.1:9999 (LISTEN)