lsof (list open files)
查看進(jìn)程打開(kāi)那些文件 或者 查看文件給那個(gè)進(jìn)程使用
lsof命令是什么?
可以列出被進(jìn)程所打開(kāi)的文件的信息。在linux環(huán)境下,任何事物都以文件的形式存在。被打開(kāi)的文件可以是
1.普通的文件
2.目錄
3.網(wǎng)絡(luò)文件系統(tǒng)的文件
4.字符設(shè)備文件
5.(函數(shù))共享庫(kù)
6.管道,命名管道
7.符號(hào)鏈接
8.底層的socket字流,網(wǎng)絡(luò)socket,unix域名socket
參數(shù)
4.87版本信息
usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges.
-?|-h list help -a AND selections (OR) -b avoid kernel blocks
-c c cmd c ^c /c/[bix] +c w COMMAND width (9) +d s dir s files
-d s select by FD set +D D dir D tree *SLOW?* +|-e s exempt s *RISKY*
-i select IPv[46] files -K list tasKs (threads) -l list UID numbers
-n no host names -N select NFS files -o list file offset
-O no overhead *RISKY* -P no port names -R list paRent PID
-s list file size -t terse listing -T disable TCP/TPI info
-U select Unix socket -v list version info -V verbose search
+|-w Warnings (+) -X skip TCP&UDP* files -Z Z context [Z]
-- end option scan
+f|-f +filesystem or -file names +|-f[gG] flaGs
-F [f] select fields; -F? for help
+|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0)
+m [m] use|create mount supplement
+|-M portMap registration (-) -o o o 0t offset digits (8)
-p s exclude(^)|select PIDs -S [t] t second stat timeout (15)
-T qs TCP/TPI Q,St (s) info
-g [s] exclude(^)|select and print process group IDs
-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
+|-r [t[m<fmt>]] repeat every t seconds (15); + until no files, - forever.
An optional suffix to t is m<fmt>; m must separate t from <fmt> and
<fmt> is an strftime(3) format for the marker line.
-s p:s exclude(^)|select protocol (p = TCP|UDP) states by name(s).
-u s exclude(^)|select login|UID set s
-x [fl] cross over +d|+D File systems or symbolic Links
names select named files or files on named file systems
列出點(diǎn)常用的
-h: 顯示幫助信息;
-v: 顯示版本信息;
-c: 列出進(jìn)程名打開(kāi)文件詳情
eg: lsof -c bash列出指定進(jìn)程所打開(kāi)的文件;也可以只使用 lsof -c ba 搜索
-g: 列出GID號(hào)進(jìn)程詳情;
-d: 列出占用該fd文件號(hào)的進(jìn)程;
+d: 列出目錄下被打開(kāi)的文件;
+D: 遞歸列出目錄下被打開(kāi)的文件;
eg: lsof +D /usr/lib
-n: 列出所有NFS(網(wǎng)絡(luò)文件系統(tǒng))文件
-i: 可以列出所有打開(kāi)了網(wǎng)絡(luò)套接字(TCP和UDP)的進(jìn)程。
eg: lsof -i :25 列出占用TCP或UDP的25端口的進(jìn)程。
lsof -i udp:53找到使用某個(gè)udp端口號(hào)的進(jìn)程
可以使用/etc/services中制定的端口名稱來(lái)代替端口號(hào),比如: lsof -i :smtp
lsof -i @192.168.0.1 獲取與指定IP相關(guān)聯(lián)的信息(支持IPV6)
-p: 列出指定進(jìn)程號(hào)所打開(kāi)的文件;
-u: 列出UID號(hào)進(jìn)程詳情;
-U: 列出所有UNIX域Socket文件;
-s: 列出當(dāng)前文件的總長(zhǎng)度;
-o: 列出當(dāng)前文件的偏移;
-a:可以將多個(gè)選項(xiàng)的組合條件由或變?yōu)榕c。
eg: lsof -a -u root -c bash
上面的命令會(huì)顯示所有由root 用戶以及bash進(jìn)程打開(kāi)的文件。
其他
通過(guò)組合管道,配合其他命令就能輸出相應(yīng)的結(jié)果。有空在列幾個(gè)。
待補(bǔ)充