加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux-lsof

发布时间:2020-12-13 23:46:07 所属栏目:Linux 来源:网络整理
导读:Linux-lsof ? 1. lsof简介 2. 输出列 2.1 FD 文件描述符 2.2 Type 文件类型 3. 常用选项 4. 示例 4.1 manpage-Examples 5. 特别用法 5.1 恢复删除的文件 5.2 杀死某个特定用户的所有活动 5.3 找回被删除文件占用的空间 1. lsof简介 lsof(list open files)

Linux-lsof

?

1. lsof简介
2. 输出列
2.1 FD 文件描述符
2.2 Type 文件类型
3. 常用选项
4. 示例
4.1 manpage-Examples
5. 特别用法
5.1 恢复删除的文件
5.2 杀死某个特定用户的所有活动
5.3 找回被删除文件占用的空间

1. lsof简介

lsof(list open files)列出当前系统打开的文件。
进程打开的文件;打开文件的进程;进程打开的端口(TCP,UDP);等...
由于Linux系统一切皆文件,所以lsof打开的文件可以是:
a regular file,常规文件
a directory,目录
a block special file,块特殊文件
a character special file,字符特殊文件
an executing text reference,?执行文本参考,
a library,库文件
a stream or a network file (Internet socket,NFS file or UNIX domain socket.)? 流或网络文件(网络socket,NFS文件,UNIX域socket)
A specific file or all the files? in? a? file system may be selected by path.

可用选项: $ lsof -h
详细说明: $ man lsof

2. 输出列

Lsof每次运行时动态调整输出列的大小,保证每列的最小大小。它还保证每列与其前一列至少隔开一个空格。

Lsof不会为每个进程或文件集生成所有字段,只会生成可用的字段。某些字段是互斥的:
  • 文件设备字符和文件主要/次要设备号;
  • 文件inode编号和协议名称;
  • 文件名和流识别;
  • 文件大小和偏移量。(-s/-o)
这些互斥集合中的一个或另一个成员将出现在字段输出中,但不会出现在两者中。

$ lsof |less
COMMAND 命令名(默认长度为9,+c w可设置长度,arch linux最大15);
PID 进程ID(-p 指定PID);
TID 线程ID;
TASKCMD 任务名(-K?);

PPID 父进程号(仅在使用-R时显示)
PGID 与进程关联的进程组号(仅在使用-g时显示)

USER 用户(-u 指定用户);
FD 文件描述符 (详见下表)(-d 排除或包含FD字段);
TYPE 与文件关联的节点的类型 (详见下表);

当n被指定为+f时,NODE-ID(或某些方言的INODE-ADDR)包含文件节点的唯一标识符(通常是内核vnode或inode地址,但有时也是设备和节点号的串联);

DEVICE 指定磁盘的名称;
SIZE/OFF 文件的大小/偏移量(-s/-o);
NLINK 包含指定+L时的文件链接计数;
NODE 索引节点(文件在磁盘上的标识);
NAME 打开文件的确切名称;

2.1 FD 文件描述符

FD FD是文件的文件描述符编号或: ? wc -l
cwd current working directory; 当前工作目录 381
txt program text (code and data); 程序文件或共享库 381
rtd root directory; root目录 381
mem memory-mapped file; 内存映射文件; 36728
mmap memory-mapped device; 内存映射设备; ?
Lnn library references (AIX); 库引用(AIX); ?
jld jail directory (FreeBSD); jail目录(FreeBSD); ?
ltx shared library text (code and data); 共享库文本(代码和数据); ?
Mxx hex memory-mapped type number xx. 十六进制内存映射类型号xx。 ?
m86 DOS Merge mapped file; DOS合并映射文件; ?
err information error (see NAME column); 错误的FD信息错误; ?
pd parent directory; 父目录; ?
tr kernel trace file (OpenBSD); 内核跟踪文件(OpenBSD); ?
v86 VP/ix mapped file; VP/ix映射文件; ?
DEL ? ? 3743
? ? ? ?
Mode FD后跟其中一个字符,描述文件打开的模式: ? ?
u u for read and write access; 文件处于读取/写入模式 128:5678
r r for read access; 文件处于只读模式 78:2924
w w for write access; 文件处于写入模式 92:2633
‘ ‘ space if mode unknown and no lock 未知模式,且未锁定 ?
- ’-’ if mode unknown and lock 未知模式,并锁定 ?
? ? ? ?
Lock 模式字符后跟一个锁定字符,描述应用于文件的锁定类型: ? ?
N N for a Solaris NFS lock of unknown type; 未知类型的Solaris NFS锁; ?
r r for read lock on part of the file; r用于部分文件的读锁定; ?
R R for a read lock on the entire file; R表示整个文件的读锁定; ?
w w for a write lock on part of the file; w对文件的一部分进行写锁定; ?
W W for a write lock on the entire file; W表示整个文件的写锁定; ?
u u for a read and write lock of any length; u表示任何长度的读写锁; ?
U U for a lock of unknown type; U表示未知类型的锁; ?
x x for an SCO OpenServer Xenix lock on part of the file; 部分文件的SCO OpenServer Xenix锁; ?
X X for an SCO OpenServer Xenix lock on the entire file; 整个文件的SCO OpenServer Xenix锁; ?
‘ ‘ space if there is no lock. 空格,没有锁。 ?

2.2 Type 文件类型

TYPE is the type of the node associated with the file TYPE是与文件关联的节点的类型 wc-l
BLK for a block special file 用于块特殊文件 1
CHR for a character special file 用于字符特殊文件 780
DIR for a directory 用于目录 770
REG for a regular file 用于常规文件 46350
FIFO for a FIFO special file 用于FIFO特殊文件 903
IPv4 for an IPv4 socket 用于IPv4套接字 7
IPv6 for an open IPv6 network file - even if its address is IPv4,mapped in an IPv6 address 用于开放的IPv6网络文件 - 即使其地址是IPv4,也映射在IPv6地址中 22
sock for a socket of unknown domain 用于未知域的套接字 217
unix for a UNIX domain socket unix套接字的 2925
a_inode ? ? 1384
netlink ? ? 134
unknown ? ? 72
DEL for a Linux map file that has been deleted 已删除的Linux映射文件的 ?
LINK for a symbolic link file 用于符号链接文件 ?
MPB for a multiplexed block file 用于多路复用块文件 ?
MPC for a multiplexed character file 用于多路复用字符文件 ?
ax25 for a Linux AX.25 socket 用于Linux AX.25套接字 ?
inet for an Internet domain socket 用于Internet域套接字 ?
lla for a HP-UX link level access file 用于HP-UX链路级访问文件 ?
rte for an AF_ROUTE socket 用于AF_ROUTE套接字 ?
x.25 for an HP-UX x.25 socket 用于HP-UX x.25套接字 ?
DOOR for a VDOOR file VDOOR文件的 ?
KQUEUE for a BSD style kernel event queue file 用于BSD样式的内核事件队列文件 ?
NOFD for a Linux /proc/<PID>/fd directory that can‘t be opened -- the directory path appears in the NAME column,followed by an error message 对于无法打开的Linux/proc/<PID>/fd目录的“NOFD - 目录路径出现在NAME列中,后跟一条错误消息 ?
PAS for a /proc/as file 用于/proc/as文件 ?
PAXV for a /proc/auxv file 用于/proc/auxv文件 ?
PCRE for a /proc/cred file 用于/proc/cred文件 ?
PCTL for a /proc control file 用于/proc控制文件 ?
PCUR for the current /proc process 用于当前/proc过程 ?
PCWD for a /proc current working directory 用于/proc当前工作目录 ?
PDIR for a /proc directory 作为/proc目录 ?
PETY for a /proc executable type (etype) 用于/proc可执行类型(etype) ?
PFD for a /proc file descriptor 用于/proc文件描述符 ?
PFDR for a /proc file descriptor directory 用于/proc文件描述符目录 ?
PFIL for an executable /proc file 表示可执行文件/proc文件 ?
PFPR for a /proc FP register set 用于/proc FP寄存器组 ?
PGD for a /proc/pagedata file 用于/proc/pagedata文件 ?
PGID for a /proc group notifier file 用于/proc组通知程序文件 ?
PIPE for pipes 管道 ?
PLC for a /proc/lwpctl file 表示/proc/lwpctl文件 ?
PLDR for a /proc/lpw directory 用于/proc/lpw目录 ?
PLDT for a /proc/ldt file 用于/proc/ldt文件 ?
PLPI for a /proc/lpsinfo file 用于/proc/lpsinfo文件的 ?
PLST for a /proc/lstatus file 用于/proc/lstatus文件 ?
PLU for a /proc/lusage file 用于/proc/lusage文件 ?
PLWG for a /proc/gwindows file 用于/proc/gwindows文件 ?
PLWI for a /proc/lwpsinfo file 用于/proc/lwpsinfo文件 ?
PLWS for a /proc/lwpstatus file 用于/proc/lwpstatus文件 ?
PLWU for a /proc/lwpusage file 用于/proc/lwpusage文件 ?
PLWX for a /proc/xregs file 用于/proc/xregs文件 ?
PMAP for a /proc map file (map) 用于/proc映射文件(map) ?
PMEM for a /proc memory image file 用于/proc内存映像文件 ?
PNTF for a /proc process notifier file 用于/proc进程通知程序文件 ?
POBJ for a /proc/object file 用于/proc/object文件 ?
PODR for a /proc/object directory 用于/proc/object目录 ?
POLP for an old format /proc light weight process file 用于旧格式/proc轻量级处理文件 ?
POPF for an old format /proc PID file 用于旧格式/proc PID文件 ?
POPG for an old format /proc page data file 用于旧格式/proc页面数据文件 ?
PORT for a SYSV named pipe SYSV命名管道的 ?
PREG for a /proc register file 用于/proc寄存器文件 ?
PRMP for a /proc/rmap file 用于/proc/rmap文件 ?
PRTD for a /proc root directory 用于/proc根目录 ?
PSGA for a /proc/sigact file 用于/proc/sigact文件 ?
PSIN for a /proc/psinfo file 用于/proc/psinfo文件 ?
PSTA for a /proc status file 用于/proc状态文件 ?
PSXSEM for a POSIX semaphore file 用于POSIX信号量文件 ?
PSXSHM for a POSIX shared memory file 用于POSIX共享内存文件 ?
PTS for a /dev/pts file 用于/dev/pts文件 ?
PUSG for a /proc/usage file 用于/proc/usage文件 ?
PW for a /proc/watch file 用于/proc/watch文件 ?
PXMP for a /proc/xmap file 用于/proc/xmap文件 ?
SMT for a shared memory transport file 用于共享内存传输文件 ?
STSO for a stream socket 用于流套接字 ?
UNNM for an unnamed type file 用于未命名的类型文件 ?
XNAM for an OpenServer Xenix special file of unknown type 用于未知类型的OpenServer Xenix特殊文件 ?
XSEM for an OpenServer Xenix semaphore file 用于OpenServer Xenix信号量文件 ?
XSD for an OpenServer Xenix shared data file 用于OpenServer Xenix共享数据文件 ?
? or the four type number octets if the corresponding name isn‘t known. 如果相应的名称未知,则为四个类型数字八位字节。 ?

3. 常用选项

-a And运算
-b
+c w command宽度
-c command命令或程序
+d dir目录
-d FD列内容的筛选
-D dir递归目录
-p PID进程号


? Defaults in parentheses; comma-separated set (s) items; dash-separated ranges. 括号中的默认值; 逗号分隔的集合项目; 短划线范围。
-?|-h list help 列表帮助
-v list version info 列表版本信息
-a AND selections (OR) AND选择(OR)
-c c cmd c ^c /c/[bix] cmd c ^ c / c / [bix]
+c w COMMAND width (9) 命令宽度(9)最大15?
-d s select by FD set 输出列表中排除或包含的文件描述符(FD)列表。
文件描述符在逗号分隔集合s中指定 – 例如,
‘‘cwd,1,3‘‘,‘‘^6,^2‘‘.(集合中不应有空格)
+d s dir s files dirs文件 注:不含符号链接,除非使用-x或-x l选项可包含
+D D dir D tree *SLOW?* dir D tree * SLOW?* 递归列出目录下被打开的文件
-u s exclude(^)|select login|UID set s exclude(^)|
-i select IPv[46] files 选择IPv[4,6]tcp,udp等文件
-n no host names #不将IP地址转换为hostname,预设是转换的
-P no port names #此参数禁止将port number转换为service name,预设为转换
-l list UID numbers 列出UID号码,禁止将UID转换为登录名。
-t terse listing 简洁列表,只输出PID
-s list file size 列表文件大小
-o list file offset 它会将SIZE/OFF输出列标题更改为OFFSET 偏移量
-U select Unix socket 选择Unix套接字
+|-e s exempt s *RISKY* 免除路径名为s的文件系统受到可能阻塞的内核函数调用。
+e选项免除stat(2),lstat(2)和大多数readlink(2)内核函数调用.?
-e选项仅免除stat(2)和lstat(2)内核函数调用.
可以使用单独的+|-e规范指定多个文件系统,并且每个文件系统可以具有免除或不允许的readlink(2)调用.
+|-w Warnings (+) 警告(+)
-p s exclude(^)|select PIDs exclude(^)|选择PID
-R list paRent PID 列出PPID
-g [s] exclude(^)|select and print process group IDs exclude(^)|选择并打印进程组ID
+|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0) 启用(+)或禁用(-)文件链接计数列表,它们可用-例如,它们不适用于套接字或大多数FIFO和管道.
如果指定+L且没有跟数字,则将列出所有链接计数。
指定-L(默认值)时,不会列出任何链接计数。
当+L后跟一个数字时,只列出链接数小于该数字的文件。(没有数字可以跟随-L)

4. 示例

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
$ lsof /home/toma/Documents/IO.xls ? 查找打开该文件的进程
soffice.b 20084 toma 3uW REG 8,23 242688 1480016 /home/toma/Documents/IO.xls
$ lsof /home/toma/Downloads/mov/GoT.mp4 ? ?
Thunar 676 toma 25u REG 8,23 11121302 1705712 /home/toma/Downloads/mov/GoT.mp4
vlc 7975 toma 22r REG 8,23 11121302 1705712 /home/toma/Downloads/mov/GoT.mp4
? ? ? ? ? ? ? ? ?
$ lsof -c soffice |grep .xls ? 查找该进程打开的 *.xls 文件
soffice.b 20084 toma 3uW REG 8,23 242688 1480016 /home/toma/Documents/IO.xls
$ lsof -c vlc |grep .mp4 ? 查找该进程打开的 *.mp4 文件
vlc 7975 toma 23r REG 8,23 11121302 1705712 /home/toma/Downloads/mov/GoT.mp4
? ? ? ? ? ? ? ? ?
$ lsof /bin/bash ? 查看谁正在使用某个文件,也就是说查找某个文件相关的进程
sh 528 toma txt REG 8,22 903464 1315133 /usr/bin/bash
$ lsof /usr/bin/fish ? ?
fish 738 toma txt REG 8,22 1587184 1365002 /usr/bin/fish
? ? ? ? ? ? ? ? ?
$ lsof /usr/lib/libreoffice/program/soffice.bin ? ?
soffice.b 22284 toma txt REG 8,22 14112 1456879 /usr/lib/libreoffice/program/soffice.bin
$ lsof -c soffice.b |grep office.bin ? ?
soffice.b 22284 toma txt REG 8,22 14112 1456879 /usr/lib/libreoffice/program/soffice.bin
$ lsof -c soffice |grep office.bin ? ?
soffice.b 22284 toma txt REG 8,22 14112 1456879 /usr/lib/libreoffice/program/soffice.bin
? ? ? ? ? ? ? ? ?
$ lsof /dev/sda10 ? 列出 /dev/sda10 设备上打开的所有文件
vlc 29350 toma 21r REG 8,10 8500479 9728 /run/media/toma/TjOe/TDDownload/FROZEN2.mp4

4.1 manpage-Examples

For a more extensive set of examples,documented more fully,see the 00QUICKSTART file of the lsof distribution.
有关更全面的示例,请参阅lsof发行版的00QUICKSTART文件。

To list all open Internet,x.25 (HP-UX),and UNIX domain files,use:
要列出所有打开的Internet,x.25(HP-UX)和UNIX域文件,请使用:
$ lsof -i -U

To list all open IPv4 network files in use by the process whose PID is 1234,use:
要列出PID为1234的进程正在使用的所有打开的IPv4网络文件,请使用:
$ lsof -i 4 -a -p 1234

Presuming the UNIX dialect supports IPv6,to list only open IPv6 network files,use:
假设UNIX方言支持IPv6,要仅列出打开的IPv6网络文件,请使用:
$ lsof -i 6

To list all files using any protocol on ports 513,514,or 515 of host wonderland.cc.purdue.edu,use:
要在主机wonderland.cc.purdue.edu的端口513,514或515上使用任何协议列出所有文件,请使用:
$ lsof -i @wonderland.cc.purdue.edu:513-515

To list all files using any protocol on any port of mace.cc.purdue.edu (cc.purdue.edu is the default domain),use:
要在mace.cc.purdue.edu的任何端口上使用任何协议列出所有文件(cc.purdue.edu是默认域),请使用:
$ lsof -i @mace

To list all open files for login name ‘‘abe‘‘,or user ID 1234,or process 456,or process 123,or process 789,use:
要列出登录名“abe”,或用户ID 1234,或流程456,流程123或流程789的所有打开文件,请使用:
$ lsof -p 456,123,789 -u 1234,abe

To send a SIGHUP to the processes that have /u/abe/bar open,use:
要将SIGHUP发送到打开/ u / abe / bar的进程,请使用:
$ kill -HUP ‘lsof -t /u/abe/bar‘

To find any open file,including an open UNIX domain socket file,with the name /dev/log,use:
要查找任何打开的文件,包括名为/ dev / log的打开的UNIX域套接字文件,请使用:
$ lsof /dev/log

To find processes with open files on the NFS file system named /nfs/mount/point whose server is inaccessible,and presuming your mount table supplies the device number for /nfs/mount/point,use:
要在名为/ nfs / mount / point且服务器不可访问的NFS文件系统上查找具有打开文件的进程,并假设您的挂载表提供/ nfs / mount / point的设备编号,请使用:
$ lsof -b /nfs/mount/point

To do the preceding search with warning messages suppressed,use:
要在禁用警告消息的情况下执行上述搜索,请使用:
$ lsof -bw /nfs/mount/point

To ignore the device cache file,use:
要忽略设备缓存文件,请使用:
$ lsof -Di

To obtain PID and command name field output for each process,file descriptor,file device number,and file inode number for each file of each process,use:
要为每个进程获取PID和命令名称字段输出,文件描述符,文件设备编号和每个进程的每个文件的文件inode编号,请使用:
$ lsof -FpcfDi

To list the files at descriptors 1 and 3 of every process running the lsof command for login ID ‘‘abe‘‘ every 10 seconds,use:
要列出每10秒执行登录ID“abe”的lsof命令的每个进程的描述符1和3的文件,请使用:
$ lsof -c lsof -a -d 1 -d 3 -u abe -r10

To list the current working directory of processes running a command that is exactly four characters long and has an ‘o‘ or ‘O‘ in character three,use this regular expression form of the -c c option:
要列出运行命令的进程的当前工作目录,该命令长度正好为四个字符且在字符3中具有“o”或“O”,请使用-c c选项的此正则表达式形式:
$ lsof -c /^..o.$/i -a -d cwd

To find an IP version 4 socket file by its associated numeric dot-form address,use:
要通过其关联的数字点形式地址查找IP版本4套接字文件,请使用:
$ lsof [email?protected]

To find an IP version 6 socket file (when the UNIX dialect supports IPv6) by its associated numeric colon-form address,use:
要通过关联的数字冒号形式地址查找IP版本6套接字文件(当UNIX方言支持IPv6时),请使用:
$ lsof [email?protected][0:1:2:3:4:5:6:7]

To find an IP version 6 socket file (when the UNIX dialect supports IPv6) by an associated numeric colon-form address that has a run of zeroes in it - e.g.,the loop-back address - use:
要通过关联的数字冒号形式地址查找IP版本6套接字文件(当UNIX方言支持IPv6时),其中包含一连串的零 - 例如,回送地址 - 使用:
$ lsof [email?protected][::1]

To obtain a repeat mode marker line that contains the current time,use:
要获得包含当前时间的重复模式标记线,请使用:
$ lsof -rm====%T====

To add spaces to the previous marker line,use:
要向前一个标记行添加空格,请使用:
$ lsof -r "m==== %T ===="

5. 特别用法

5.1 恢复删除的文件

对于许多应用程序,尤其是日志文件和数据库,这种恢复删除文件的方法非常有用。
$ lsof |grep .mp4 播放视频文件中... FROZEN2.mp4
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/TDDownload/FROZEN2.mp4
$ lsof |grep .mp4 放入回收站后 显示如下: 从回收站恢复即可
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/.Trash-1001/files/FROZEN2.mp4
$ lsof |grep .mp4 直接删除(shift + delete),显示如下,后面多了(deleted)标记
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/TDDownload/FROZEN2.mp4 (deleted)
$ ls -l /proc/30573/fd/20 列出pid信息,/proc/30573/fd/20 链接到已删除文件。
lr-x------ 1 /proc/30573/fd/20 -> ‘/run/media/toma/TjOe/TDDownload/FROZEN2.mp4 (deleted)‘
$ cat /proc/30573/fd/20 > /home/toma/te.mp4 使用查看命令打开并 重定向到新文件即可

$ ls -l /home/toma/te.mp4 列出恢复的新文件
-rwxrwxrwx 1 toma? ? 8500479 te.mp4
?

5.2 杀死某个特定用户的所有活动

# kill -9 `lsof -t -u named`
对于服务器或多用户共用的环境比较有用。

5.3 找回被删除文件占用的空间

找到仍被打开,但已被删除的文件 $ lsof | grep deleted 查看后关闭相应程序或杀死进程即可 $ ps -ef | grep pid $ sudo kill -9 pid

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读