Linux常见操作-查看端口占用情况

  
        可以使用  lsof  或者  netstat
  

lsof(list  open  files)

        列出当前已打开文件
  
        在  linux  中,  任何事物都是以文件形式存在,系统会为  TCP、UDP  分配一个文件描述符,所以使用  lsof  不仅可以查看
        已打开的文件,还可以查看  TCP、UDP  的信息,通过  NAME  字段,查看端口号
  
  
        lsof  查看所有已打开文件

walker@LAPTOP-IHP7E9GG:~$ lsof
COMMAND   PID TID TASKCMD     USER   FD      TYPE DEVICE SIZE/OFF             NODE NAME
bash      553               walker  255u      CHR  136,1      0t0                4 /dev/pts/1
python2.7 635               walker  mem       REG   8,16    27002            48825 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
python2.7 635               walker  mem       REG   8,16   191472            47025 /usr/lib/x86_64-linux-gnu/ld-2.31.so
python2.7 635               walker    2u      CHR  136,1      0t0                4 /dev/pts/1
python2.7 635               walker    3u     IPv4  17749      0t0              TCP *:8888 (LISTEN)
python2.7 635               walker    7r      CHR    1,9      0t0             1033 /dev/urandom
 

  
        lsof -i  查看所有已打开的网络连接

walker@LAPTOP-IHP7E9GG:~$ lsof -i
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python2.7 922 walker    3u  IPv4  17373      0t0  TCP *:8882 (LISTEN)
python2.7 923 walker    3u  IPv4  17376      0t0  TCP *:8883 (LISTEN)
python2.7 924 walker    3u  IPv4  17379      0t0  TCP *:8884 (LISTEN)
python2.7 925 walker    3u  IPv4  17382      0t0  TCP *:8885 (LISTEN)
java      947 walker   14u  IPv6  21901      0t0  TCP *:8090 (LISTEN)

  
        各列信息含义
                CONIAND:  进程的名称
                PID:  进程标识符
                USER:  贡所有者
                FD:  文件描述符,  应用程序通过文件描述符识别该文件。如0evd、  txt  等
                TIPE:  文件类型:  如  DIR  、  REC  等
                DEVICE:  指定磁盘的名称
                SIZE:  文件的大小
                NODE:  索引节点(文件在磁盘上的标识)
                NAME:打开文件的确切名称
  
        lsof -i:端口号  查看指定端口

walker@LAPTOP-IHP7E9GG:~$ lsof -i:8889
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python2.7 919 walker    3u  IPv4    423      0t0  TCP *:8889 (LISTEN)

  

netstat  -tunlp

  
        -t  (tcp)  仅显示tcp相关选项
        -u  (udp)仅显示udp相关选项
        -n  拒绝显示别名,能显示数字的全部转化为数字,如不显示域名,直接显示ip地址
        -l  仅列出在Listen(监听)的服务状态
        -p  显示建立相关链接的程序名
  
  
        netstat -tunlp  仅查看  tcp,udp  且正在监听的端口和进程的相关情况

walker@LAPTOP-IHP7E9GG:~$ sudo netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8882            0.0.0.0:*               LISTEN      922/python2.7
tcp        0      0 0.0.0.0:8883            0.0.0.0:*               LISTEN      923/python2.7
tcp        0      0 0.0.0.0:8884            0.0.0.0:*               LISTEN      924/python2.7
tcp        0      0 0.0.0.0:8885            0.0.0.0:*               LISTEN      925/python2.7
tcp6       0      0 :::8090                 :::*                    LISTEN      947/java
tcp6       0      0 :::33060                :::*                    LISTEN      845/mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      845/mysqld

  
        netstat -tunlp | grep 8090  结合  grep  查看指定端口情况

walker@LAPTOP-IHP7E9GG:~$ sudo netstat -tunlp | grep 8090
tcp6       0      0 :::8090                 :::*                    LISTEN      947/java

  
        netstat -alp  查看所有正在监听的网络连接和进程的相关情况

walker@LAPTOP-IHP7E9GG:~$ sudo netstat -alp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8883            0.0.0.0:*               LISTEN      923/python2.7
tcp        0      0 0.0.0.0:8884            0.0.0.0:*               LISTEN      924/python2.7
tcp        0      0 0.0.0.0:8885            0.0.0.0:*               LISTEN      925/python2.7
tcp6       0      0 [::]:8090               [::]:*                  LISTEN      947/java
tcp6       0      0 [::]:33060              [::]:*                  LISTEN      845/mysqld
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN      845/mysqld
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     SEQPACKET  LISTENING     20285    552/init             /run/WSL/552_interop
unix  2      [ ACC ]     STREAM     LISTENING     419      845/mysqld           /var/run/mysqld/mysqld.sock
unix  2      [ ]         STREAM     CONNECTED     21902    947/java