应用场景

  • 查看你进程打开的文件,打开文件的进程,进程打开的端口(TCP、UDP)
  • 找回/恢复删除的文件(前提:被删除的文件存在有于文件描述符)
  • 在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。所以如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等,系统在后台都为该应用程序分配了一个文件描述符

    语法

    ``` lsof (选项)

-a:列出打开文件存在的进程; -c<进程名>:列出指定进程所打开的文件; -g:列出GID号进程详情; -d<文件号>:列出占用该文件号的进程; +d<目录>:列出目录下被打开的文件; +D<目录>:递归列出目录下被打开的文件; -n<目录>:列出使用NFS的文件; -i<条件>:列出符合条件的进程(协议、:端口、 @ip ) -p<进程号>:列出指定进程号所打开的文件; -u:列出UID号进程详情; -h:显示帮助信息; -v:显示版本信息

  1. <a name="Ak8UN"></a>
  2. ### 属性含义
  3. ```bash
  4. lsof
  5. command PID USER FD type DEVICE SIZE NODE NAME
  6. init 1 root cwd DIR 8,2 4096 2 /
  7. init 1 root rtd DIR 8,2 4096 2 /
  8. init 1 root txt REG 8,2 43496 6121706 /sbin/init
  9. init 1 root mem REG 8,2 143600 7823908 /lib64/ld-2.5.so
  10. init 1 root mem REG 8,2 1722304 7823915 /lib64/libc-2.5.so
  11. init 1 root mem REG 8,2 23360 7823919 /lib64/libdl-2.5.so
  12. init 1 root mem REG 8,2 95464 7824116 /lib64/libselinux.so.1
  13. init 1 root mem REG 8,2 247496 7823947 /lib64/libsepol.so.1
  14. init 1 root 10u FIFO 0,17 1233 /dev/initctl
  15. migration 2 root cwd DIR 8,2 4096 2 /
  16. migration 2 root rtd DIR 8,2 4096 2 /
  17. migration 2 root txt unknown /proc/2/exe
  18. ksoftirqd 3 root cwd DIR 8,2 4096 2 /
  19. ksoftirqd 3 root rtd DIR 8,2 4096 2 /
  20. ksoftirqd 3 root txt unknown /proc/3/exe
  21. migration 4 root cwd DIR 8,2 4096 2 /
  22. migration 4 root rtd DIR 8,2 4096 2 /
  23. migration 4 root txt unknown /proc/4/exe
  24. ksoftirqd 5 root cwd DIR 8,2 4096 2 /
  25. ksoftirqd 5 root rtd DIR 8,2 4096 2 /
  26. ksoftirqd 5 root txt unknown /proc/5/exe
  27. events/0 6 root cwd DIR 8,2 4096 2 /
  28. events/0 6 root rtd DIR 8,2 4096 2 /
  29. events/0 6 root txt unknown /proc/6/exe
  30. events/1 7 root cwd DIR 8,2 4096 2 /

lsof输出各列信息的意义如下:

  • COMMAND:进程的名称
  • PID:进程标识符
  • PPID:父进程标识符(需要指定-R参数)
  • USER:进程所有者
  • PGID:进程所属组
  • FD:文件描述符,应用程序通过文件描述符识别该文件

    文件描述符列表:

  1. cwd:表示current work dirctory,即:应用程序的当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行更改
  2. txt:该类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的 /sbin/init 程序
  3. lnn:library references (AIX);
  4. er:FD information error (see NAME column);
  5. jld:jail directory (FreeBSD);
  6. ltx:shared library text (code and data);
  7. mxx :hex memory-mapped type number xx.
  8. m86:DOS Merge mapped file;
  9. mem:memory-mapped file;
  10. mmap:memory-mapped device;
  11. pd:parent directory;
  12. rtd:root directory;
  13. tr:kernel trace file (OpenBSD);
  14. v86 VP/ix mapped file;
  15. 0:表示标准输出
  16. 1:表示标准输入
  17. 2:表示标准错误

    一般在标准输出、标准错误、标准输入后还跟着文件状态模式:

  18. u:表示该文件被打开并处于读取/写入模式。

  19. r:表示该文件被打开并处于只读模式。
  20. w:表示该文件被打开并处于写入模式。
  21. 空格:表示该文件的状态模式为unknow,且没有锁定。
  22. -:表示该文件的状态模式为unknow,且被锁定。

    同时在文件状态模式后面,还跟着相关的锁:

  23. N:for a Solaris NFS lock of unknown type;

  24. r:for read lock on part of the file;
  25. R:for a read lock on the entire file;
  26. w:for a write lock on part of the file;(文件的部分写锁)
  27. W:for a write lock on the entire file;(整个文件的写锁)
  28. u:for a read and write lock of any length;
  29. U:for a lock of unknown type;
  30. x:for an SCO OpenServer Xenix lock on part of the file;
  31. X:for an SCO OpenServer Xenix lock on the entire file;
  32. space:if there is no lock.

    文件类型:

  33. DIR:表示目录。

  34. CHR:表示字符类型。
  35. BLK:块设备类型。
  36. UNIX: UNIX 域套接字。
  37. FIFO:先进先出 (FIFO) 队列。
  38. IPv4:网际协议 (IP) 套接字。
  39. DEVICE:指定磁盘的名称
  40. SIZE:文件的大小
  41. NODE:索引节点(文件在磁盘上的标识)
  42. NAME:打开文件的确切名称
  43. REG:常规文件

    实例

    列出指定进程号所打开的文件

    ```bash sudo lsof -p $pid

or

sudo ls -l /proc/$pid/fd

<a name="mkZFf"></a>
#### 列出指定进程名所打开的文件
```bash
sudo lsof -c $pname

获取端口对应的进程

sudo lsof -i :9981

列出打开文件的进程

sudo lsof $filename

恢复被删除的文件

文件描述符存放在/proc目录下

查看指定进程号下面的文件描述符
sudo ls -l /proc/$pid/fd/

查看指定描述符下面的文件
sudo ls /proc/$pid/fd/$fid

恢复文件
sudo cp /proc/$pid/fd/$fid/fname $dir