Linux commands (124) - lsof command

1. Introduction command

File lsof (list open files) to view the open process, it is very easy system monitoring tool. Since lsof command requires access to core memory, and various system files, so you need root privileges to execute.

In the Linux system, everything is a file. Files can be accessed only through regular data, you can also access the network connectivity and hardware. socket-related information so lsof can not only view the process open regular file, directory, you can also view the process of listening ports. Each process opens a file, the system in the background whom is assigned a file descriptor, regardless of the nature of this document, the file descriptor interaction between applications and the underlying operating system provides a common interface. Because the application opens a file descriptor list provides a wealth of information about the application itself, it is possible to view the list by lsof tool, system monitoring, and troubleshooting help.

lsof open files can be viewed:

普通文件
目录
字符或块设备文件
共享库
管道、命名管道
符号链接
网络文件(如 NFS file、网络 socket,Unix 域名 socket)
其它类型的文件,等等

2. Command Format

lsof [OPTIONS] [--] [NAMES]

In the absence of any options, lsof lists all open files belonging to active processes.

3. Option Description

-?, -h 
	显示帮助信息
-a
参数被视为逻辑与 AND,会影响全部的参数
-A A
	在配置了 AFS 分布式网络文件系统的系统上可用,其 AFS 内核代码是通过动态模块实现的。通过 A 指定备用名称列表文件,在该文件中可以找到动态模块的内核地址
-b
	避免 lsof 因调用可能阻塞的内核函数而产生阻塞,比如 lstat(2)、readlink(2) 和 stat(2) 等内核函数
-c C
	显示出以字符或字符串 C 开头的命令程序开启的文件,如 lsof -c init。如果 C 以斜杠 / 开头和结尾,则斜杠之间的字符被解释为正则表达式。该选项可多次指定
+c W
	指定 COMMAND 列的宽度,单位字符。默认为 9
-C
	禁用从内核的名称缓存中报告任何路径名
-D D
	指导 lsof 使用设备缓存文件。该选项的使用有时受到限制。-D 必须后面跟着一个函数字母,函数字母后面可以有一个路径名称。lsof 识别以下功能字母:
	?	报告设备缓存文件路径
	b	构建设备缓存文件
	i	忽略设备缓存文件
	r	读取设备缓存文件
	u	读取并更新设备缓存文件
+D D
	递归搜索目录 D。如显示在 /usr/local 及其子目录下被程序开启的文件:lsof +D /usr/local
+d D
	 非递归搜索目录 D。如显示在 /usr/local 下被程序开启的文件:lsof +d /usr/local
-d FD
	指定文件描述符列表,可以采用逗号分隔,也可以指定范围。比如 1,2,3 或 1-3。如果前面包含尖号,表示排除。如显示 FD 为 4 的进程:lsof -d 4
+|-e S
	豁免(exempt)路径名称为 S 的文件系统不受可能阻塞的内核函数调用的影响。+e 选项豁免 stat(2)、lstat(2) 和大多数 readlink(2) 内核函数调用。-e 选项只能豁免 stat(2) 和 lstat(2) 内核函数调用
+|-E
	+E 指定使用端点信息显示 Linux 管道、Linux UNIX 套接字和 Linux 伪终端文件,并显示端点的文件。-E 则不显示端点的文件
-F LIST
	指定字符列表 LIST,选择输出给另一程序处理的字段,各字段对应的字符见下文
+|-f [cfgGn]
	f 本身澄清了路径名参数的解释方式。当后面跟着 c、f、g、G 或 n 时,它指定要启用(+)或抑制(-)内核文件结构信息。
	c 文件结构使用计数(not Linux)
	f 文件结构地址(not Linux)
	g 文件标志缩写(Linux 2.6.22 及更高版本)
	G 十六进制文件标志(Linux 2.6.22 及更高版本)
	n 文件结构节点地址(not Linux)
-g [PGID]
	选择或排除属于指定进程组的进程打开的文件。 进程组 ID 使用逗号分隔,如果 PGID 前面包含尖号,表示排除。若没有指定 PGID,则显示全部。如显示 PGID 为 6 和 7 的进程:lsof -g6,7
-i [I]
	选择其 Internet 地址与 -i 中指定的地址匹配的文件,若没有相关地址被指定,则监听全部。
	用法: lsof -i [46][protocol][@hostname|hostaddr][:serivce|port]
	说明:4 6 分别表示 IPv4 和 IPv6   
    protocol: TCP or UDP   
    hostname:主机名
    hostaddr:IPv4 或 IPv6 地址
    service:主机提供的服务的名称,即 /etc/services 中的 service name
    port:端口号
-K
	在支持任务(线程)报告方式的系统上输出进程的任务(线程)列表
-k K
	指定内核名称列表文件,代替 /vmunix、/mach 等
-l
	禁止将 user ID 转换为登录的名称,默认是登录名称
+|-L [L]
	+ 或 - 表示开启或关闭显示文件连接数,如果只有单纯的 +L,后面没有任何数字,则表示显示全部,如果后面有数字,只有文件连接数少于该数字的会被列出
+|-m M
	-m 指定一个内核内存文件 M ,代替 /dev/kmem 或 /dev/mem。+m 将装载补充文件写入标准输出文件
+|-M
	启用或禁用报告本地 TCP、UDP 和 UDPLITE 端口的端口映射器注册
-n
	不将 IP 地址转换为主机名
-N
	显示 NFS 文件
-o
	始终显示文件偏移量。它导致 SIZE/OFF 输出列标题更改为 OFFSET
-o O
	指定在文件偏移量的 0t 之后要打印的小数位数
-O
	指示 lsof 避免被某些内核操作阻塞。即在分叉的子进程中执行它们。虽然使用此选项将减少 lsof 启动开销,但也可能导致 lsof 在内核不响应函数时挂起。谨慎使用此选项
-P
	禁止将网络文件的端口号转换为端口名
-p S
	排除或选择进程的文件列表,进程 ID 列表使用逗号分隔,如 123 或 123,^456。尖号表示排除指定 PID
-R
	使用列 PPID 列出父进程的 PID
+|-r [T[mFMT]]
 	控制 lsof 不断重复执行,间隔 T 秒,默认为 15s。-r 永远不断地执行,直到收到中断讯号(ctrl+ c),+r 一直执行,直到没有文件被显示。可选的 mFMT 参数指定标记线的格式,格式 FMT 遵循 C 语言标准库函数 strftime(3) 的规范
-S [T]
	指定内核函数 lstat(2)、readlink(2) 和 stat(2) 的可选超时秒值,否则可能会死锁。t 的最小值是 2;默认值是 15
-s [P:S]
	列出文件的大小,若该文件没有大小,则留下空白。它导致 SIZE/OFF 输出列标题更改为 SIZE。P 表示协议名称 TCP  or  UDP,S 表示逗号分隔的协议状态
-T [T]
	-T 没有参数则禁用 TCP/TPI 信息报告。跟如下参数,则显示指定 TCP/TPI 信息:
	f 选择报告套接字选项,状态和值,以及 TCP标志和值
	q 选择队列长度
	s 选择连接状态
	w 选择窗口大小
-t
	生成只有进程标识符而没有标题的简洁输出,这样输出可以通过管道传递给 kill(1) 杀死
-U
	选择 UNIX 域套接字文件的列表
-u USERS
	选择登录名或用户 ID 位于逗号分隔集 USERS 中的用户的文件列表。如 root 或 548,root”,如果用户名或用户 ID 前有尖号 ^,表示排除
-V
	指示被要求列出但找不到的项
-v
    显示版本信息
+|-w
	   启用(+)或禁用(-)警告消息
-X
	Linux 下跳过所有打开的 TCP、UDP 和 UDPLITE IPv4 和 IPv6 文件的信息报告
-x [fl]
	一般与选项 +d 和 +D 选项,指示搜索时是否跨文件系统和符号链接。-x 不跟任何参数时,表示跨文件系统和符号链接
-Z [Z]
	指定如何处理 SELinux 安全上下文。当在运行的 Linux 内核中禁用SELinux时,Z 字段将被抑制输出。-Z 选项不跟参数,如 -Z -,安全上下文将列在 SECURITY-CONTEXT 列中输出
--
	双减号表示选项结束
NAMES
	列出指定文件,符号链接在使用前将被解析

4. Output Field Description

When -F option is specified, lsof will generate suitable output processing by another program (e.g., Perl scripts or awk or C program).

The following are fields lsof can be output. It represents a single character field identifier.

a	文件访问模式
c	进程命令名
C	文件结构共享计数
d	文件的设备字符码
D	文件的主要/次要设备号
F	文件结构地址
f	文件描述符
G	文件标志
g	进程组 ID
i	文件 inode 编号
K	任务 ID
k	链接计数
L	进程登录名
m	重复输出之间的标记
N	节点标识符
n	文件名、注释、Internet 地址
o	文件偏移量(十进制)
P	协议名称
p	进程 ID
R	父进程 ID
r	原始设备号
S	文件的流标识
s	文件大小(十进制)
T	TCP/TPI 信息
t	文件类型
u	进程用户 ID
Z	SELinux 安全上下文(禁用 SELinux 时禁用)
z	Solaris 10 及更高版本的区域名
0	使用NUL字段结束符字符代替NL
1-9	系统特定的字段标识符

You can use the command lsof -F?to view the instructions above information field.

Example 5. Common

(1) without any parameters, lists all open files belonging to active processes.

lsof | head
COMMAND     PID   TID USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
systemd       1       root  cwd       DIR              252,1     4096          2 /
systemd       1       root  rtd       DIR              252,1     4096          2 /
systemd       1       root  txt       REG              252,1  1616248      47908 /usr/lib/systemd/systemd
systemd       1       root  mem       REG              252,1    20032      25897 /usr/lib64/libuuid.so.1.3.0
systemd       1       root  mem       REG              252,1   252704      25043 /usr/lib64/libblkid.so.1.1.0
systemd       1       root  mem       REG              252,1    90632      25988 /usr/lib64/libz.so.1.2.7
systemd       1       root  mem       REG              252,1   153192      25496 /usr/lib64/liblzma.so.5.0.99
systemd       1       root  mem       REG              252,1    23968      25065 /usr/lib64/libcap-ng.so.0.0.0
systemd       1       root  mem       REG              252,1    19888      25018 /usr/lib64/libattr.so.1.1.0

Meaning lsof output of each column information is as follows:

COMMAND:进程的名称
PID:进程标识符
TID:任务 ID。Linux 下 TID 为空表示该行为进程
USER:进程所有者
FD:文件描述符。主要有:
	cwd:应用程序当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行更改
	txt:该类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的 /sbin/init 程序
	lnn:库引用(AIX)
	err:FD 信息错误
	jld:监狱目录(FreeBSD)
	ltx:共享库文本(代码和数据)
	mxx:十六进制内存映射类型号 xx
	m86:DOS合并映射文件
	mem:内存映射文件
	mmap:内存映射设备
	pd:父目录
	rtd:根目录
	tr:内核跟踪文件(OpenBSD)
	v86:VP/ix 映射文件
	0:标准输出
	1:标准输入
	2:标准错误
	
	文件描述符后一般还跟着文件状态模式:
	r:只读模式
	w:写入模式
	u:读写模式
	空格:文件的状态模式为 unknow,且没有锁定
	-:文件的状态模式为 unknow,且被锁定
	
	同时在文件状态模式后面,还跟着相关的锁:
	N:对于未知类型的 Solaris NFS 锁
	r:文件部分的读锁
	R:整个文件的读锁
	w:文件的部分写锁
	W:整个文件的写锁
	u:任何长度的读写锁
	U:用于未知类型的锁
	x:用于部分文件上的 SCO OpenServer Xenix 锁
	X:用于整个文件上的 SCO OpenServer Xenix 锁
	space:无锁

TYPE:文件类型。常见的文件类型有:
	REG:普通文件
	DIR:表示目录
	CHR:表示字符类型
	BLK:块设备类型
	UNIX:UNIX 域套接字
	FIFO:先进先出队列
	IPv4:IPv4 套接字
DEVICE:磁盘名称
SIZE:文件的大小或文件偏移量(以字节为单位)
NODE:索引节点
NAME:打开文件的确切名称

(2) to see who is using a file, a file that is associated with the discovery process.

lsof /bin/bash
COMMAND     PID USER  FD   TYPE DEVICE SIZE/OFF  NODE NAME
watchdog.  7639 root txt    REG  252,1   922760 45210 /usr/bin/bash
bash       8591 root txt    REG  252,1   922760 45210 /usr/bin/bash
bash       9694 root txt    REG  252,1   922760 45210 /usr/bin/bash
bash      20151 root txt    REG  252,1   922760 45210 /usr/bin/bash

(3) recursive view all open files under a directory information.

lsof +D ./test
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
bash    8518 root  cwd    DIR  252,1     4096 799036 ./test/shell

(4) File information a user open.

lsof -u root

(5) are listed in the file information about a process opened up.

 lsof -c sshd

-c option will list all the sshd process begins with the open files. In fact, you can also command lsof | grep sshdto view, but the first method is more concise.

(6) File information multiple processes open.

lsof -c mysql -c apache

(7) lists the file information for a user and a process opened up.

lsof -u test -c mysql 

Note that, or is the relationship between -u -c option. can use -

(8) lists all open files in addition to a user's information.

lsof -u ^root

Caret ^ before the user name, it indicates excluded, i.e. does not display information about the file opened by the root user.

(9) the process open files displayed by a process ID.

lsof -p 1

(10) File information corresponding to the number of multiple processes.

lsof -p 1,2,3

(11) In addition to a list the process ID, process ID of the other open file information.

lsof -p ^1

(12) lists all network connections.

lsof -i

(13) lists all TCP network connection information.

lsof -i tcp

(14) lists all UDP network connection information.

lsof -i udp

(15) list who is using a port.

lsof -i :3306

(16) list who is using a particular TCP or UDP port.

lsof -i udp:55
lsof -i tcp:80

(17) lists all active network ports for a user.

lsof -a -u test -i

(18) lists all network file systems.

lsof -N

(19) UNIX domain socket selection list file.

lsof -U

(20) view the file information to a user group open.

lsof -g 5555

(21) information from the specified file descriptor file.

lsof -d txt
lsof -d 1
lsof -d 2

0 standard input, standard output represents 1, 2 represents the standard error, and thus found that: so most applications are opened file from the FD 3 starts.

(22) view the file information specified file descriptor range.

lsof -d 2-3

(23) COMMAND column lists the type and the file contains the string sshd described character for txt file information.

lsof -c sshd -a -d txt
COMMAND   PID USER  FD   TYPE DEVICE SIZE/OFF  NODE NAME
sshd     8254 root txt    REG  252,1   819608 27632 /usr/sbin/sshd
sshd    20149 root txt    REG  252,1   819608 27632 /usr/sbin/sshd

(24) list is the process ID for all of 1234 IPV4 network file opened by the process.

lsof -p 1234 -a -i 4

(25) currently connected to the host are listed peida.linux port: 20,21,22,25 all files related information, and continue to execute instructions lsof every 3 seconds.

lsof -i @peida.linux:20,21,22,25 -r 3

references

[. 1] the lsof (. 8) Manual
[2] Baidu Encyclopedia .Andrew file system
[3] . Blog every day a linux park command (51): lsof command

发布了519 篇原创文章 · 获赞 1567 · 访问量 202万+

Guess you like

Origin blog.csdn.net/K346K346/article/details/104235400