> Linux教程 > 系统运维 >

查看linux中某个端口是否被占用

netstat -tunlp 会显示所有端口和所有对应的程序,用grep管道可以过滤出想要的关键字段.
列一下22端口占用的程序
view plainprint?
  1. [root@leiwan tmp]# netstat -tunlp |grep 22  
  2. tcp        0      0 0.0.0.0:42957               0.0.0.0:*                   LISTEN      2230/rpc.statd        
  3. tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2443/sshd             
  4. tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2292/cupsd            
  5. tcp        0      0 :::22                       :::*                        LISTEN      2443/sshd             
  6. tcp        0      0 ::1:631                     :::*                        LISTEN      2292/cupsd            
  7. tcp        0      0 :::57609                    :::*                        LISTEN      2230/rpc.statd        
  8. udp        0      0 0.0.0.0:5353                0.0.0.0:*                               2211/avahi-daemon     
  9. udp        0      0 0.0.0.0:631                 0.0.0.0:*                               2292/cupsd            
  10. udp        0      0 0.0.0.0:37167               0.0.0.0:*                               2230/rpc.statd        
  11. udp        0      0 0.0.0.0:52291               0.0.0.0:*                               2211/avahi-daemon     
  12. udp        0      0 0.0.0.0:68                  0.0.0.0:*                               2207/dhclient         
  13. udp        0      0 0.0.0.0:710                 0.0.0.0:*                               2230/rpc.statd        
  14. udp        0      0 :::39834                    :::*                                    2230/rpc.statd  


查看某一端口的占用情况: lsof -i:端口号
[root@www ~]# lsof -i:21
  COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  pure-ftpd 2651 root 4u IPv4 7047 TCP *:ftp (LISTEN)
  pure-ftpd 2651 root 5u IPv6 7048 TCP *:ftp (LISTEN)
这里显示出21号端口正在被pure-ftpd使用,状态是listen。



netstat -anp 显示系统端口使用情况


(责任编辑:IT)