netstat查看监听端口

21 views
Skip to first unread message

adream

unread,
Mar 9, 2017, 8:56:46 AM3/9/17
to shlug
我自己写了个测试程序,在8321上建立TCP监听,但是启动程序时报告端口已经被使用,于是用netstat命令查看哪个进程使用8321端口,结果出现这个结果:
netstat -antlp | grep 8321
tcp    0    0  0.0.0.0:8321   0.0.0.0:*    LISTEN  -
在表示进程PID是个减号
同时我使用  lsof -i TCP:8321
但是lsof这个命令没输出任何信息。
所以请教一下,到底是那个程序在使用我的8321端口。
我的环境是 CentOS6.5 64位,以上命令均使用root查看的
谢谢。

Hugh __

unread,
Mar 9, 2017, 9:13:38 PM3/9/17
to sh...@googlegroups.com


你看看这个能不能帮上你。



http://unix.stackexchange.com/questions/97752/how-to-identify-a-process-which-has-no-pid

I have a process which listen to 2 ports : 45136/tcp and 37208/udp (actually I assume it is the same process). But netstat doesn't return any pid :

There are instances where there simply isn't a PID associated to the TCP port in use. You can read about NFS, in @derobert's answer, which is one of them. There are others. I have instances where I'm using ssh tunnels to connect back to services such as IMAP. These are showing up without a process ID too.

In any case you can use a more verbose form of netstat which might shed additional light on what process is ultimately using a TCP port.

$ netstat --program --numeric-hosts --numeric-ports --extend

Example

$ netstat --program --numeric-hosts --numeric-ports --extend |grep -- '-' | head -10
Proto Recv-Q Send-Q Local Address               Foreign Address             State       User       Inode      PID/Program name   
tcp        0      0 192.168.1.103:936           192.168.1.3:60526           ESTABLISHED root       160024310  -                   
tcp        0      0 192.168.1.1:2049            192.168.1.3:841             ESTABLISHED sam        159941218  -                   
tcp        0      0 127.0.0.1:143               127.0.0.1:57443             ESTABLISHED dovecot    152567794  13093/imap-login    
tcp        0      0 192.168.1.103:739           192.168.1.3:2049            ESTABLISHED root       160023970  -                   
tcp        0      0 192.168.1.103:34013         192.168.1.3:111             TIME_WAIT   root       0          -                   
tcp        0      0 127.0.0.1:46110             127.0.0.1:783               TIME_WAIT   root       0          -                   
tcp        0      0 192.168.1.102:54891         107.14.166.17:110           TIME_WAIT   root       0          -                   
tcp        0      0 127.0.0.1:25                127.0.0.1:36565             TIME_WAIT   root       0          -                   
tcp        0      0 192.168.1.1:2049            192.168.1.6:798             ESTABLISHED tammy      152555007  -             

If you notice the output includes INODES so we could back track into the process using this info.

$ find -inum 152555007

Which will show you a file which might lead you to a process.




From: sh...@googlegroups.com <sh...@googlegroups.com> on behalf of adream <adre...@gmail.com>
Sent: Thursday, March 9, 2017 9:56:43 PM
To: shlug
Subject: [shlug] netstat查看监听端口
 
--
-- You received this message because you are subscribed to the Google Groups Shanghai Linux User Group group. To post to this group, send email to sh...@googlegroups.com. To unsubscribe from this group, send email to shlug+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/shlug?hl=zh-CN
---
您收到此邮件是因为您订阅了Google网上论坛上的“Shanghai Linux User Group”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到shlug+un...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout
Reply all
Reply to author
Forward
0 new messages