簡體   English   中英

地址已在使用中,但 netstat 或 lsof 中沒有

[英]Address already in use but nothing in netstat or lsof

我嘗試在端口 7054 上啟動 Python SimpleHTTPServer:

$ sudo python -m SimpleHTTPServer 7054
...
socket.error: [Errno 98] Address already in use

所以,我運行了以下命令:

$ sudo netstat -ntpu | grep 7054
$ sudo lsof -i -n -P | grep 7054

但我沒有結果。

netstat頁:

netstat   [address_family_options]   [--tcp|-t]   [--udp|-u]   [--raw|-w]   [--listening|-l]  [--all|-a]  [--numeric|-n]  [--numeric-hosts]  [--numeric-ports]
[--numeric-users] [--symbolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v] [--continuous|-c]

我使用以下選項:

sudo netstat -tanl | grep 7054

這是--numeric--tcp--all--listening

我認為顯示在特定端口上偵聽進程的pid所需的最小netstat選項是-nlp

您指定的lsof選項適合我。 使用https://wiki.python.org/moin/UdpCommunication#Receivingpython -m SimpleHTTPServer 7054的示例代碼:

$ netstat -nlp | grep 7054
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:7054            0.0.0.0:*               LISTEN      20458/python    
udp        0      0 0.0.0.0:7054          0.0.0.0:*                           20498/python    
$ lsof -i -n -P | grep 7054
python    20458 michael    3u  IPv4 143736      0t0  TCP *:7054 (LISTEN)
python    20498 michael    3u  IPv4 173739      0t0  UDP *:7054 

額外信用:堅持別名:

listening() {
    netstat -nlp | grep $1
}

並使用它:

$ listening 7054

一個地址可以在使用中,但是一旦在SOCK_STREAM套接字上使用了bind ,但在設置命名的套接字之前LISTEN state, lsofssnetstat不會顯示地址。

這是通過使用 AlmaLinux 8.6 和4.18.0-372.19.1.el8_6.x86_64 Kernel 執行的測試發現的。 測試程序的源代碼在bind_local.c

啟動測試程序,指定要綁定到的 IPv6 鏈路本地地址和端口號 (10000):

[mr_halfword@haswell-alma ibv_message_passing]$ ibv_message_passing_c_project/bin/debug/bind_local/bind_local  -6 fe80::207:43ff:fe15:2298%4 -p 10000 -l
fd 3 bound to fe80::207:43ff:fe15:2298 scope-id 4 port 10000
Press enter to listen on port

在上面的端口上創建了一個SOCK_STREAM套接字,調用了bind並使用getsockname獲取顯示的套接字名稱(即套接字已綁定到的地址)。

測試程序綁定的套接字文件描述符顯示為套接字398999

[mr_halfword@haswell-alma ~]$ ls -l /proc/`pgrep bind_local`/fd
total 0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 0 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 1 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 2 -> /dev/pts/0
lrwx------. 1 mr_halfword mr_halfword 64 Sep 10 17:08 3 -> 'socket:[398999]'

在此 state 中,嘗試使用nc偵聽端口 10000 失敗,地址已在使用中,但lsofss均未顯示地址:

[mr_halfword@haswell-alma ~]$ nc -l 10000
Ncat: bind to :::10000: Address already in use. QUITTING.
[mr_halfword@haswell-alma ~]$ sudo lsof -i -n -P | grep 10000
[mr_halfword@haswell-alma ~]$ sudo ss -nlp | grep 10000
[mr_halfword@haswell-alma ~]$ 

通過按回車鍵使測試程序在綁定的套接字上調用listen


Press return to exit

現在綁定的套接字位於LISTEN state 中,嘗試使用nc監聽端口 10000 失敗,地址已在使用中,但現在lsofss顯示地址以及哪個程序正在使用該地址:

[mr_halfword@haswell-alma ~]$ sudo lsof -i -n -P | grep 10000
bind_loca 16929 mr_halfword    3u  IPv6 398999      0t0  TCP [fe80::207:43ff:fe15:2298]:10000 (LISTEN)
[mr_halfword@haswell-alma ~]$ sudo ss -nlp | grep 10000
tcp   LISTEN 0      1                       [fe80::207:43ff:fe15:2298]%enp1s0f4d1:10000               [::]:*     users:(("bind_local",pid=16929,fd=3))                                  

我還沒有嘗試查看 Linux Kernel 源代碼來確定SOCK_STREAM套接字是否已通過命名綁定到地址,但留在 Z9ED39E2EA931586B73A985A69EZEF 中的程序可以使用任何用戶空間方法來定位。

上述原因是調查iwpmd iWARP 端口映射器守護進程如何聲明 TCP 端口,為此無法找到列出聲明的 TCP 端口的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM