Linux命令 lsof - 顯示Linux系統(tǒng)當(dāng)前已打開的所有文件列表 `lsof -p pid`

2021-11-22 17:27 更新

lsof

顯示Linux系統(tǒng)當(dāng)前已打開的所有文件列表 lsof -p pid

補(bǔ)充說明

lsof命令 用于查看你進(jìn)程打開的文件,打開文件的進(jìn)程,進(jìn)程打開的端口(TCP、UDP)。找回/恢復(fù)刪除的文件。是十分方便的系統(tǒng)監(jiān)視工具,因?yàn)閘sof命令需要訪問核心內(nèi)存和各種文件,所以需要root用戶執(zhí)行。

在linux環(huán)境下,任何事物都以文件的形式存在,通過文件不僅僅可以訪問常規(guī)數(shù)據(jù),還可以訪問網(wǎng)絡(luò)連接和硬件。所以如傳輸控制協(xié)議 (TCP) 和用戶數(shù)據(jù)報(bào)協(xié)議 (UDP) 套接字等,系統(tǒng)在后臺(tái)都為該應(yīng)用程序分配了一個(gè)文件描述符,無論這個(gè)文件的本質(zhì)如何,該文件描述符為應(yīng)用程序與基礎(chǔ)操作系統(tǒng)之間的交互提供了通用接口。因?yàn)閼?yīng)用程序打開文件的描述符列表提供了大量關(guān)于這個(gè)應(yīng)用程序本身的信息,因此通過lsof工具能夠查看這個(gè)列表對(duì)系統(tǒng)監(jiān)測(cè)以及排錯(cuò)將是很有幫助的。

語法

lsof (選項(xiàng))

選項(xiàng)

-a:列出打開文件存在的進(jìn)程;
-c<進(jìn)程名>:列出指定進(jìn)程所打開的文件;
-g:列出GID號(hào)進(jìn)程詳情;
-d<文件號(hào)>:列出占用該文件號(hào)的進(jìn)程;
+d<目錄>:列出目錄下被打開的文件;
+D<目錄>:遞歸列出目錄下被打開的文件;
-n<目錄>:列出使用NFS的文件;
-i<條件>:列出符合條件的進(jìn)程(協(xié)議、:端口、 @ip )
-p<進(jìn)程號(hào)>:列出指定進(jìn)程號(hào)所打開的文件;
-u:列出UID號(hào)進(jìn)程詳情;
-h:顯示幫助信息;
-v:顯示版本信息

實(shí)例

lsof
command     PID USER   FD      type             DEVICE     SIZE       NODE NAME
init          1 root  cwd       DIR                8,2     4096          2 /
init          1 root  rtd       DIR                8,2     4096          2 /
init          1 root  txt       REG                8,2    43496    6121706 /sbin/init
init          1 root  mem       REG                8,2   143600    7823908 /lib64/ld-2.5.so
init          1 root  mem       REG                8,2  1722304    7823915 /lib64/libc-2.5.so
init          1 root  mem       REG                8,2    23360    7823919 /lib64/libdl-2.5.so
init          1 root  mem       REG                8,2    95464    7824116 /lib64/libselinux.so.1
init          1 root  mem       REG                8,2   247496    7823947 /lib64/libsepol.so.1
init          1 root   10u     FIFO               0,17                1233 /dev/initctl
migration     2 root  cwd       DIR                8,2     4096          2 /
migration     2 root  rtd       DIR                8,2     4096          2 /
migration     2 root  txt   unknown                                        /proc/2/exe
ksoftirqd     3 root  cwd       DIR                8,2     4096          2 /
ksoftirqd     3 root  rtd       DIR                8,2     4096          2 /
ksoftirqd     3 root  txt   unknown                                        /proc/3/exe
migration     4 root  cwd       DIR                8,2     4096          2 /
migration     4 root  rtd       DIR                8,2     4096          2 /
migration     4 root  txt   unknown                                        /proc/4/exe
ksoftirqd     5 root  cwd       DIR                8,2     4096          2 /
ksoftirqd     5 root  rtd       DIR                8,2     4096          2 /
ksoftirqd     5 root  txt   unknown                                        /proc/5/exe
events/0      6 root  cwd       DIR                8,2     4096          2 /
events/0      6 root  rtd       DIR                8,2     4096          2 /
events/0      6 root  txt   unknown                                        /proc/6/exe
events/1      7 root  cwd       DIR                8,2     4096          2 /

lsof輸出各列信息的意義如下:

  • COMMAND:進(jìn)程的名稱
  • PID:進(jìn)程標(biāo)識(shí)符
  • PPID:父進(jìn)程標(biāo)識(shí)符(需要指定-R參數(shù))
  • USER:進(jìn)程所有者
  • PGID:進(jìn)程所屬組
  • FD:文件描述符,應(yīng)用程序通過文件描述符識(shí)別該文件。

文件描述符列表:

  1. cwd:表示current work dirctory,即:應(yīng)用程序的當(dāng)前工作目錄,這是該應(yīng)用程序啟動(dòng)的目錄,除非它本身對(duì)這個(gè)目錄進(jìn)行更改
  2. txt:該類型的文件是程序代碼,如應(yīng)用程序二進(jìn)制文件本身或共享庫,如上列表中顯示的 /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:表示標(biāo)準(zhǔn)輸出
  16. 1:表示標(biāo)準(zhǔn)輸入
  17. 2:表示標(biāo)準(zhǔn)錯(cuò)誤

一般在標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯(cuò)誤、標(biāo)準(zhǔn)輸入后還跟著文件狀態(tài)模式:

  1. u:表示該文件被打開并處于讀取/寫入模式。
  2. r:表示該文件被打開并處于只讀模式。
  3. w:表示該文件被打開并處于寫入模式。
  4. 空格:表示該文件的狀態(tài)模式為unknow,且沒有鎖定。
  5. -:表示該文件的狀態(tài)模式為unknow,且被鎖定。

同時(shí)在文件狀態(tài)模式后面,還跟著相關(guān)的鎖:

  1. N:for a Solaris NFS lock of unknown type;
  2. r:for read lock on part of the file;
  3. R:for a read lock on the entire file;
  4. w:for a write lock on part of the file;(文件的部分寫鎖)
  5. W:for a write lock on the entire file;(整個(gè)文件的寫鎖)
  6. u:for a read and write lock of any length;
  7. U:for a lock of unknown type;
  8. x:for an SCO OpenServer Xenix lock on part      of the file;
  9. X:for an SCO OpenServer Xenix lock on the      entire file;
  10. space:if there is no lock.

文件類型:

  1. DIR:表示目錄。
  2. CHR:表示字符類型。
  3. BLK:塊設(shè)備類型。
  4. UNIX: UNIX 域套接字。
  5. FIFO:先進(jìn)先出 (FIFO) 隊(duì)列。
  6. IPv4:網(wǎng)際協(xié)議 (IP) 套接字。
  7. DEVICE:指定磁盤的名稱
  8. SIZE:文件的大小
  9. NODE:索引節(jié)點(diǎn)(文件在磁盤上的標(biāo)識(shí))
  10. NAME:打開文件的確切名稱
  11. REG:常規(guī)文件

列出指定進(jìn)程號(hào)所打開的文件:

lsof -p $pid

獲取端口對(duì)應(yīng)的進(jìn)程ID=>pid

lsof -i:9981 -P -t -sTCP:LISTEN

列出打開文件的進(jìn)程:

lsof $filename


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)