檢視你程序開打的檔案,開啟檔案的程序

2022-06-08 05:09:08 字數 4555 閱讀 6009

lsof命令_linux lsof 命令用法詳解:顯示linux系統當前已開啟的所有檔案列表

lsof命令用於檢視你程序開打的檔案,開啟檔案的程序,程序開啟的埠(tcp、udp)。找回/恢復刪除的檔案。是十分方便的系統監視工具,因為lsof命令需要訪問核心記憶體和各種檔案,所以需要root使用者執行。

在linux環境下,任何事物都以檔案的形式存在,通過檔案不僅僅可以訪問常規資料,還可以訪問網路連線和硬體。所以如傳輸控制協議 (tcp) 和使用者資料報協議 (udp) 套接字等,系統在後台都為該應用程式分配了乙個檔案描述符,無論這個檔案的本質如何,該檔案描述符為應用程式與基礎作業系統之間的互動提供了通用介面。因為應用程式開啟檔案的描述符列表提供了大量關於這個應用程式本身的資訊,因此通過lsof工具能夠檢視這個列表對系統監測以及排錯將是很有幫助的。

lsof(選項)
-a:列出開啟檔案存在的程序;

-c《程序名》:列出指定程序所開啟的檔案;

-g:列出gid號程序詳情;

-d《檔案號》:列出占用該檔案號的程序;

+d《目錄》:列出目錄下被開啟的檔案;

+d《目錄》:遞迴列出目錄下被開啟的檔案;

-n《目錄》:列出使用nfs的檔案;

-i《條件》:列出符合條件的程序。(4、6、協議、:埠、 @ip )

-p《程序號》:列出指定程序號所開啟的檔案;

-u:列出uid號程序詳情;

-h:顯示幫助資訊;

-v:顯示版本資訊。

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輸出各列資訊的意義如下:

檔案描述符列表:

cwd:表示current work dirctory,即:應用程式的當前工作目錄,這是該應用程式啟動的目錄,除非它本身對這個目錄進行更改

txt:該型別的檔案是程式**,如應用程式二進位制檔案本身或共享庫,如上列表中顯示的 /sbin/init 程式

lnn:library references (aix);

er:fd information error (see name column);

jld:jail directory (freebsd);

ltx:shared library text (code and data);

pd:parent directory;

rtd:root directory;

tr:kernel trace file (openbsd);

0:表示標準輸出

1:表示標準輸入

2:表示標準錯誤

一般在標準輸出、標準錯誤、標準輸入後還跟著檔案狀態模式:

u:表示該檔案被開啟並處於讀取/寫入模式。

r:表示該檔案被開啟並處於唯讀模式。

w:表示該檔案被開啟並處於。

空格:表示該檔案的狀態模式為unknow,且沒有鎖定。

-:表示該檔案的狀態模式為unknow,且被鎖定。

n:for a solaris nfs lock of unknown type;

r:for read lock on part of the file;

r:for a read lock on the entire file;

w:for a write lock on part of the file;(檔案的部分寫鎖)

w:for a write lock on the entire file;(整個檔案的寫鎖)

u:for a read and write lock of any length;

u:for a lock of unknown type;

x:for an sco openserver xenix lock on part      of the file;

x:for an sco openserver xenix lock on the      entire file;

space:if there is no lock.

檔案型別:

dir:表示目錄。

chr:表示字元型別。

blk:塊裝置型別。

unix: unix 域套接字。

fifo:先進先出 (fifo) 佇列。

ipv4:網際協議 (ip) 套接字。

device:指定磁碟的名稱

size:檔案的大小

node:索引節點(檔案在磁碟上的標識)

name:開啟檔案的確切名稱

lsof 檢視你程序開打的檔案

lsof命令用於檢視你程序開打的檔案,開啟檔案的程序,程序開啟的埠 tcp udp 找回 恢復刪除的檔案。是十分方便的系統監視工具,因為lsof命令需要訪問核心記憶體和各種檔案,所以需要root使用者執行。在linux環境下,任何事物都以檔案的形式存在,通過檔案不僅僅可以訪問常規資料,還可以訪問網路...

Linux下檢視程序開啟的檔案與開啟檔案的程序

有許多情況下,我們需要檢視乙個程序當前開啟了哪些檔案,反過來我們也希望知道某乙個檔案正在被哪些程序所讀寫。在linux下有許多有用的工具可以幫我們完成這兩個工作,下面介紹這兩個工具 lsof與fuser。fuser find files or sockets user 格式 fuser 選項 fna...

檢視程序開啟了哪些檔案

檢視程序開啟了哪些檔案 sudo lsof p pid 如果是分析乙個你不太了解的程序,這個命令比較有用。可以使用lsof p pid grep tcp檢視程序中的 tcp 連線資訊。檢視檔案被哪個程序使用 使用這個命令檢視乙個檔案被哪些程序正在使用sudo lsof path to file,示例...