Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序

1 linux查看那端口监听情况

linux中可以使用netstat命令查看端口监听情况,首先来看一下该命令的参数:

1、netstat参数

(base) shl@zhihui-mint:~$ netstat -h
usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [<Socket> ...]
       netstat { [-vWeenNac] -i | [-cnNe] -M | -s [-6tuw] }

        -r, --route              display routing table
        -i, --interfaces         display interface table
        -g, --groups             display multicast group memberships
        -s, --statistics         display networking statistics (like SNMP)
        -M, --masquerade         display masqueraded connections

        -v, --verbose            be verbose
        -W, --wide               don't truncate IP addresses
        -n, --numeric            don't resolve names
        --numeric-hosts          don't resolve host names
        --numeric-ports          don't resolve port names
        --numeric-users          don't resolve user names
        -N, --symbolic           resolve hardware names
        -e, --extend             display other/more information
        -p, --programs           display PID/Program name for sockets
        -o, --timers             display timers
        -c, --continuous         continuous listing

        -l, --listening          display listening server sockets
        -a, --all                display all sockets (default: connected)
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB
        -Z, --context            display SELinux security context for sockets

  <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-S|--sctp} {-w|--raw}
           {-x|--unix} --ax25 --ipx --netrom
  <AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25) 
(base) shl@zhihui-mint:~$ 

2、netstat比较常用的参数含义

  • t: 表示tcp
  • u:表示udp
  • n:表示数字形式显示
  • p:显示sockets对应的PID/Program
  • a:显示所有的sockets,不是用该参数,默认只显示已经连接的connected

2 Linux查看某个端口对应的进程号和程序

1、使用lsof命令(list open files)查看端口号对应的进程号和程序

lsof -i:端口号

(base) shl@zhihui-mint:~$ lsof -i:8888
COMMAND     PID USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
chrome     2768  shl   94u  IPv4 137190217      0t0  TCP localhost:45242->localhost:8888 (ESTABLISHED)
jupyter-n 17249  shl    5u  IPv4 137172011      0t0  TCP localhost:8888 (LISTEN)
jupyter-n 17249  shl   24u  IPv4 137193950      0t0  TCP localhost:8888->localhost:45242 (ESTABLISHED)
(base) shl@zhihui-mint:~$ 

2、使用netstat命令查看端口号对应的进程号

(base) shl@zhihui-mint:~$ netstat -nap|grep 8888
(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 127.0.0.1:8888          0.0.0.0:*               LISTEN      17249/python        
tcp        0      0 127.0.0.1:8888          127.0.0.1:45242         ESTABLISHED 17249/python        
tcp        0      0 127.0.0.1:45242         127.0.0.1:8888          ESTABLISHED 2768/chrome --type= 
unix  3      [ ]         SEQPACKET  CONNECTED     178888   2709/chrome          
(base) shl@zhihui-mint:~$ 

在这里插入图片描述

欢迎大家关注笔者,你的关注是我持续更博的最大动力


原创文章,转载告知,盗版必究

微信:suihailiang0816
QQ:931762054
wx公众号:仰望星空的小随
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:白松林 返回首页