Linux命令之lsof详解程序员

lsof [选项] [文件]

lsof命令用于查看你进程打开的文件,进程打开的端口(TCP、UDP),找回/恢复删除的文件,打开文件的进程。

(1).常用选项

-c <进程名> 列出指定进程所打开的文件 
+D <目录> 递归列出目录下被打开的文件 
-i <条件> 列出符号条件的进程(4、6、协议、:[email protected]) 
-n <目录> 列出使用NFS的文件 
-p <进程号> 列出指定进程号所打开的文件 
-u s 列出login name或UID为s的程序

(2).实例

1)在没有任何参数时,显示当前系统已经打开的正在使用的所有文件

[[email protected] ~]$ lsof | more -10 
COMMAND     PID   TID           USER   FD      TYPE             DEVICE  SIZE/OFF     NODE NAME 
systemd       1                 root  cwd   unknown                                       /proc/1/cwd (readlink: Permission denied) 
systemd       1                 root  rtd   unknown                                       /proc/1/root (readlink: Permission denied) 
systemd       1                 root  txt   unknown                                       /proc/1/exe (readlink: Permission denied) 
systemd       1                 root NOFD                                                 /proc/1/fd (opendir: Permission denied) 
kthreadd      2                 root  cwd   unknown                                       /proc/2/cwd (readlink: Permission denied) 
kthreadd      2                 root  rtd   unknown                                       /proc/2/root (readlink: Permission denied) 
kthreadd      2                 root  txt   unknown                                       /proc/2/exe (readlink: Permission denied) 
kthreadd      2                 root NOFD                                                 /proc/2/fd (opendir: Permission denied) 
ksoftirqd     3                 root  cwd   unknown                                       /proc/3/cwd (readlink: Permission denied) 
--More-- 

1.名称详解

 COMMAND:进程名称

 PID:进程标识符

 TID:线程ID

 USER:所有者

 FD:文件描述符

 TYPE:文件类型

 DEVICE:磁盘名称

 SIZE/OFF:文件大小

 NODE:索引节点

 NAME:文件名称

2.FD(文件描述符)详解

 cwd:应用程序的当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行修改

 txt:该类型的文件是程序代码,如应用程序二进制文件本身或共享库

 rtd:根目录

 0:表示标准输出

 1:表示标准输入

 2:表示标准错误

3.文件类型详解

 DIR:目录

 CHR:字符类型

 BLK:块设备类型

 UNIX:UNIX域套接字

 FIFO:先进先出(FIFO)队列

 IPv4:国际协议(IP)套接字

2)查看指定文件相关的进程信息,即找出此文件的进程

[[email protected] ~]$ lsof /bin/bash 
COMMAND   PID USER  FD   TYPE DEVICE SIZE/OFF     NODE NAME 
bash    14073   xf txt    REG    8,3   964608 50350934 /usr/bin/bash 
bash    14119   xf txt    REG    8,3   964608 50350934 /usr/bin/bash 
bash    14870   xf txt    REG    8,3   964608 50350934 /usr/bin/bash

3)递归查询某个目录的文件信息

[[email protected] ~]$ lsof +D .local/share/ 
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME 
gnome-she 8480   xf  mem    REG    8,3    32768 52016764 .local/share/gvfs-metadata/root-ce6a002c.log 
gnome-she 8480   xf  mem    REG    8,3       64 52016740 .local/share/gvfs-metadata/root 
gnome-she 8480   xf   27r   REG    8,3       64 52016740 .local/share/gvfs-metadata/root 
gnome-she 8480   xf   33r   REG    8,3    32768 52016764 .local/share/gvfs-metadata/root-ce6a002c.log 
nautilus- 8764   xf  mem    REG    8,3    32768 52016193 .local/share/gvfs-metadata/home-c8370b51.log 
nautilus- 8764   xf  mem    REG    8,3      528 52016339 .local/share/gvfs-metadata/home 
nautilus- 8764   xf   16r   REG    8,3      528 52016339 .local/share/gvfs-metadata/home 
nautilus- 8764   xf   17r   REG    8,3    32768 52016193 .local/share/gvfs-metadata/home-c8370b51.log 
gnome-sof 8814   xf   27r   DIR    8,3       89 52015751 .local/share/flatpak/repo 
gnome-sof 8814   xf   28r   DIR    8,3        6   843301 .local/share/flatpak/repo/objects 
gnome-sof 8814   xf   29r   DIR    8,3       19 17315607 .local/share/flatpak/repo/tmp 
gnome-sof 8814   xf   30r   DIR    8,3        6 52015800 .local/share/flatpak/repo/tmp/cache 
tracker-s 8875   xf   12w   REG    8,3   284395 35774061 .local/share/tracker/data/tracker-store.journal 
evolution 8911   xf   11u   REG    8,3    24576    35380 .local/share/evolution/addressbook/system/contacts.db 
gvfsd-met 9829   xf  mem    REG    8,3    32768 52016764 .local/share/gvfs-metadata/root-ce6a002c.log 
gvfsd-met 9829   xf  mem    REG    8,3       64 52016740 .local/share/gvfs-metadata/root 
gvfsd-met 9829   xf    8r   REG    8,3       64 52016740 .local/share/gvfs-metadata/root 
gvfsd-met 9829   xf    9u   REG    8,3    32768 52016764 .local/share/gvfs-metadata/root-ce6a002c.log 

4)不使用+D选项得到与某个文件有关的进程信息的方法

[[email protected] ~]$ lsof | grep '/home/xf/.local/share' 
gnome-she  8480                   xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
gnome-she  8480                   xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
gnome-she  8480                   xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
gnome-she  8480                   xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
gmain      8480  8482             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
gmain      8480  8482             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
gmain      8480  8482             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
gmain      8480  8482             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
dconf      8480  8483             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
dconf      8480  8483             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
dconf      8480  8483             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
dconf      8480  8483             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
gdbus      8480  8485             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
gdbus      8480  8485             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
gdbus      8480  8485             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
gdbus      8480  8485             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8490             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8490             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8490             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8490             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8491             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8491             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8491             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8491             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8492             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8492             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8492             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8492             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8493             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8493             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8493             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8493             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
threaded-  8480  8509             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
threaded-  8480  8509             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
threaded-  8480  8509             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
threaded-  8480  8509             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8510             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8510             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8510             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8510             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8511             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8511             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8511             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8511             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8512             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8512             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8512             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8512             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8513             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8513             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8513             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8513             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8514             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8514             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8514             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8514             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8515             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8515             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8515             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8515             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8516             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8516             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8516             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8516             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
JS         8480  8517             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
JS         8480  8517             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
JS         8480  8517             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
JS         8480  8517             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8521             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8521             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8521             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8521             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8522             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8522             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8522             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8522             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8523             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8523             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8523             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8523             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
llvmpipe-  8480  8524             xf  DEL       REG                8,3           52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log 
llvmpipe-  8480  8524             xf  DEL       REG                8,3           52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ 
llvmpipe-  8480  8524             xf   27r      REG                8,3        64 52016740 /home/xf/.local/share/gvfs-metadata/root.DN12WZ (deleted) 
llvmpipe-  8480  8524             xf   33r      REG                8,3     32768 52016764 /home/xf/.local/share/gvfs-metadata/root-ce6a002c.log (deleted) 
nautilus-  8764                   xf  mem       REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
nautilus-  8764                   xf  mem       REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
nautilus-  8764                   xf   16r      REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
nautilus-  8764                   xf   17r      REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
gmain      8764  8772             xf  mem       REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
gmain      8764  8772             xf  mem       REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
gmain      8764  8772             xf   16r      REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
gmain      8764  8772             xf   17r      REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
gdbus      8764  8773             xf  mem       REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
gdbus      8764  8773             xf  mem       REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
gdbus      8764  8773             xf   16r      REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
gdbus      8764  8773             xf   17r      REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
dconf      8764  8927             xf  mem       REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
dconf      8764  8927             xf  mem       REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
dconf      8764  8927             xf   16r      REG                8,3       528 52016339 /home/xf/.local/share/gvfs-metadata/home 
dconf      8764  8927             xf   17r      REG                8,3     32768 52016193 /home/xf/.local/share/gvfs-metadata/home-c8370b51.log 
gnome-sof  8814                   xf   27r      DIR                8,3        89 52015751 /home/xf/.local/share/flatpak/repo 
gnome-sof  8814                   xf   28r      DIR                8,3         6   843301 /home/xf/.local/share/flatpak/repo/objects 
gnome-sof  8814                   xf   29r      DIR                8,3        19 17315607 /home/xf/.local/share/flatpak/repo/tmp 
gnome-sof  8814                   xf   30r      DIR                8,3         6 52015800 /home/xf/.local/share/flatpak/repo/tmp/cache 
gmain      8814  8841             xf   27r      DIR                8,3        89 52015751 /home/xf/.local/share/flatpak/repo 
gmain      8814  8841             xf   28r      DIR                8,3         6   843301 /home/xf/.local/share/flatpak/repo/objects 
gmain      8814  8841             xf   29r      DIR                8,3        19 17315607 /home/xf/.local/share/flatpak/repo/tmp 
gmain      8814  8841             xf   30r      DIR                8,3         6 52015800 /home/xf/.local/share/flatpak/repo/tmp/cache 
gdbus      8814  8847             xf   27r      DIR                8,3        89 52015751 /home/xf/.local/share/flatpak/repo 
gdbus      8814  8847             xf   28r      DIR                8,3         6   843301 /home/xf/.local/share/flatpak/repo/objects 
gdbus      8814  8847             xf   29r      DIR                8,3        19 17315607 /home/xf/.local/share/flatpak/repo/tmp 
gdbus      8814  8847             xf   30r      DIR                8,3         6 52015800 /home/xf/.local/share/flatpak/repo/tmp/cache 
dconf      8814  8920             xf   27r      DIR                8,3        89 52015751 /home/xf/.local/share/flatpak/repo 
dconf      8814  8920             xf   28r      DIR                8,3         6   843301 /home/xf/.local/share/flatpak/repo/objects 
dconf      8814  8920             xf   29r      DIR                8,3        19 17315607 /home/xf/.local/share/flatpak/repo/tmp 
dconf      8814  8920             xf   30r      DIR                8,3         6 52015800 /home/xf/.local/share/flatpak/repo/tmp/cache 
tracker-s  8875                   xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
dconf      8875  8883             xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
gmain      8875  8884             xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
gdbus      8875  8885             xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
pool       8875  8886             xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
pool       8875  8887             xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
pool       8875  8888             xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
pool       8875  8889             xf   12w      REG                8,3    284395 35774061 /home/xf/.local/share/tracker/data/tracker-store.journal 
evolution  8911                   xf   11u      REG                8,3     24576    35380 /home/xf/.local/share/evolution/addressbook/system/contacts.db 
gmain      8911  8931             xf   11u      REG                8,3     24576    35380 /home/xf/.local/share/evolution/addressbook/system/contacts.db 
dconf      8911  8933             xf   11u      REG                8,3     24576    35380 /home/xf/.local/share/evolution/addressbook/system/contacts.db 
evolution  8911  8934             xf   11u      REG                8,3     24576    35380 /home/xf/.local/share/evolution/addressbook/system/contacts.db 
gdbus      8911  8935             xf   11u      REG                8,3     24576    35380 /home/xf/.local/share/evolution/addressbook/system/contacts.db 
pool       8911  8959             xf   11u      REG                8,3     24576    35380 /home/xf/.local/share/evolution/addressbook/system/contacts.db 
gvfsd-met  9829                   xf  mem       REG                8,3     32768 52016756 /home/xf/.local/share/gvfs-metadata/root-5245f849.log 
gvfsd-met  9829                   xf  mem       REG                8,3        64 52016746 /home/xf/.local/share/gvfs-metadata/root 
gvfsd-met  9829                   xf    8r      REG                8,3        64 52016746 /home/xf/.local/share/gvfs-metadata/root 
gvfsd-met  9829                   xf    9u      REG                8,3     32768 52016756 /home/xf/.local/share/gvfs-metadata/root-5245f849.log 
gmain      9829  9831             xf  mem       REG                8,3     32768 52016756 /home/xf/.local/share/gvfs-metadata/root-5245f849.log 
gmain      9829  9831             xf  mem       REG                8,3        64 52016746 /home/xf/.local/share/gvfs-metadata/root 
gmain      9829  9831             xf    8r      REG                8,3        64 52016746 /home/xf/.local/share/gvfs-metadata/root 
gmain      9829  9831             xf    9u      REG                8,3     32768 52016756 /home/xf/.local/share/gvfs-metadata/root-5245f849.log 
gdbus      9829  9833             xf  mem       REG                8,3     32768 52016756 /home/xf/.local/share/gvfs-metadata/root-5245f849.log 
gdbus      9829  9833             xf  mem       REG                8,3        64 52016746 /home/xf/.local/share/gvfs-metadata/root 
gdbus      9829  9833             xf    8r      REG                8,3        64 52016746 /home/xf/.local/share/gvfs-metadata/root 
gdbus      9829  9833             xf    9u      REG                8,3     32768 52016756 /home/xf/.local/share/gvfs-metadata/root-5245f849.log 

5)列出某个用户打开的所有文件相关的进程信息

[[email protected] ~]$ lsof -u xf | more -10 
COMMAND     PID USER   FD      TYPE             DEVICE  SIZE/OFF     NODE NAME 
gnome-key  8232   xf  cwd   unknown                                       /proc/8232/cwd (readlink: Permission denied) 
gnome-key  8232   xf  rtd   unknown                                       /proc/8232/root (readlink: Permission denied) 
gnome-key  8232   xf  txt   unknown                                       /proc/8232/exe (readlink: Permission denied) 
gnome-key  8232   xf NOFD                                                 /proc/8232/fd (opendir: Permission denied) 
gnome-ses  8239   xf  cwd       DIR                8,3      4096 52016724 /home/xf 
gnome-ses  8239   xf  rtd       DIR                8,3       259       64 / 
gnome-ses  8239   xf  txt       REG                8,3    298688 17575626 /usr/libexec/gnome-session-binary 
gnome-ses  8239   xf  mem       REG                8,3     54856 34038802 /usr/lib64/gio/modules/libdconfsettings.so 
gnome-ses  8239   xf  mem       REG                8,3 106075056 33853815 /usr/lib/locale/locale-archive 
--More-- 

6)使用不同方法列出与程序有关的文件

方法一:使用-c选项

[[email protected] ~]$ lsof -c sleep 
COMMAND   PID USER   FD      TYPE DEVICE SIZE/OFF NODE NAME 
sleep   15392 root  cwd   unknown                      /proc/15392/cwd (readlink: Permission denied) 
sleep   15392 root  rtd   unknown                      /proc/15392/root (readlink: Permission denied) 
sleep   15392 root  txt   unknown                      /proc/15392/exe (readlink: Permission denied) 
sleep   15392 root NOFD                                /proc/15392/fd (opendir: Permission denied) 

方法二:使用grep重定向

[[email protected] ~]$ lsof | grep sleep 
sleep     15462                 root  cwd   unknown                                       /proc/15462/cwd (readlink: Permission denied) 
sleep     15462                 root  rtd   unknown                                       /proc/15462/root (readlink: Permission denied) 
sleep     15462                 root  txt   unknown                                       /proc/15462/exe (readlink: Permission denied) 
sleep     15462                 root NOFD                                                 /proc/15462/fd (opendir: Permission denied)

方法三:使用-p选项

[[email protected] ~]$ ps aux | grep sleep 
root      16071  0.0  0.0 107952   616 ?        S    16:45   0:00 sleep 60 
xf        16087  0.0  0.0 112724   988 pts/0    S+   16:45   0:00 grep --color=auto sleep 
[[email protected] ~]$ lsof -p 16071 
COMMAND   PID USER   FD      TYPE DEVICE SIZE/OFF NODE NAME 
sleep   16071 root  cwd   unknown                      /proc/16071/cwd (readlink: Permission denied) 
sleep   16071 root  rtd   unknown                      /proc/16071/root (readlink: Permission denied) 
sleep   16071 root  txt   unknown                      /proc/16071/exe (readlink: Permission denied) 
sleep   16071 root NOFD                                /proc/16071/fd (opendir: Permission denied)

7)列出所有网络连接,或指定条件的进程

列出所有网络连接

[[email protected] ~]# lsof -i 
COMMAND    PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME 
systemd      1   root   39u  IPv4  33780      0t0  TCP *:sunrpc (LISTEN) 
systemd      1   root   40u  IPv4  33781      0t0  UDP *:sunrpc  
systemd      1   root   41u  IPv6  33782      0t0  TCP *:sunrpc (LISTEN) 
systemd      1   root   42u  IPv6  33783      0t0  UDP *:sunrpc  
avahi-dae 6814  avahi   12u  IPv4  35540      0t0  UDP *:mdns  
avahi-dae 6814  avahi   13u  IPv4  35541      0t0  UDP *:57126  
rpcbind   6816    rpc    4u  IPv4  33780      0t0  TCP *:sunrpc (LISTEN) 
rpcbind   6816    rpc    5u  IPv4  33781      0t0  UDP *:sunrpc  
rpcbind   6816    rpc    6u  IPv6  33782      0t0  TCP *:sunrpc (LISTEN) 
rpcbind   6816    rpc    7u  IPv6  33783      0t0  UDP *:sunrpc  
rpcbind   6816    rpc   10u  IPv4  35511      0t0  UDP *:3com-amp3  
rpcbind   6816    rpc   11u  IPv6  35512      0t0  UDP *:3com-amp3  
chronyd   6860 chrony    1u  IPv4  38834      0t0  UDP localhost:323  
chronyd   6860 chrony    2u  IPv6  38835      0t0  UDP localhost:323  
dhclient  7280   root    6u  IPv4  43307      0t0  UDP *:bootpc  
cupsd     7480   root   11u  IPv6  42956      0t0  TCP localhost:ipp (LISTEN) 
cupsd     7480   root   12u  IPv4  42957      0t0  TCP localhost:ipp (LISTEN) 
sshd      7482   root    3u  IPv4  46015      0t0  TCP *:italk (LISTEN) 
sshd      7482   root    4u  IPv6  46024      0t0  TCP *:italk (LISTEN) 
sshd      7482   root    5u  IPv4  46026      0t0  TCP *:ssh (LISTEN) 
sshd      7482   root    6u  IPv6  46028      0t0  TCP *:ssh (LISTEN) 
master    7733   root   13u  IPv4  46278      0t0  TCP localhost:smtp (LISTEN) 
master    7733   root   14u  IPv6  46279      0t0  TCP localhost:smtp (LISTEN) 
dnsmasq   7770 nobody    3u  IPv4  49235      0t0  UDP *:bootps  
dnsmasq   7770 nobody    5u  IPv4  49238      0t0  UDP xuexi:domain  
dnsmasq   7770 nobody    6u  IPv4  49239      0t0  TCP xuexi:domain (LISTEN) 

列出使用指定网络条件的进程

[[email protected] ~]# lsof -i 4 
COMMAND    PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME 
systemd      1   root   39u  IPv4  33780      0t0  TCP *:sunrpc (LISTEN) 
systemd      1   root   40u  IPv4  33781      0t0  UDP *:sunrpc  
avahi-dae 6814  avahi   12u  IPv4  35540      0t0  UDP *:mdns  
avahi-dae 6814  avahi   13u  IPv4  35541      0t0  UDP *:57126  
rpcbind   6816    rpc    4u  IPv4  33780      0t0  TCP *:sunrpc (LISTEN) 
rpcbind   6816    rpc    5u  IPv4  33781      0t0  UDP *:sunrpc  
rpcbind   6816    rpc   10u  IPv4  35511      0t0  UDP *:3com-amp3  
chronyd   6860 chrony    1u  IPv4  38834      0t0  UDP localhost:323  
dhclient  7280   root    6u  IPv4  43307      0t0  UDP *:bootpc  
cupsd     7480   root   12u  IPv4  42957      0t0  TCP localhost:ipp (LISTEN) 
sshd      7482   root    3u  IPv4  46015      0t0  TCP *:italk (LISTEN) 
sshd      7482   root    5u  IPv4  46026      0t0  TCP *:ssh (LISTEN) 
master    7733   root   13u  IPv4  46278      0t0  TCP localhost:smtp (LISTEN) 
dnsmasq   7770 nobody    3u  IPv4  49235      0t0  UDP *:bootps  
dnsmasq   7770 nobody    5u  IPv4  49238      0t0  UDP xuexi:domain  
dnsmasq   7770 nobody    6u  IPv4  49239      0t0  TCP xuexi:domain (LISTEN) 

8)列出指定目录下使用NFS文件系统的所有文件

[[email protected] ~]$ lsof -n /home/xf | more -10 
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME 
gnome-ses  8239   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
dbus-laun  8248   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
dbus-daem  8249   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
gvfsd      8316   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
gvfsd-fus  8321   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
at-spi-bu  8443   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
dbus-daem  8448   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
at-spi2-r  8451   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
gnome-she  8480   xf  cwd    DIR    8,3     4096 52016724 /home/xf 
--More-- 

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/2910.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论