linux lsof命令
Lsof is linux command used for output files and processes related information. lsof
support different type of file formats like regular file, directory, block special file etc. lsof
provides given command output parsable format where other tools like cut
can be used to filter output. In this tutorial we will look different use cases of lsof
command.
Lsof是用于输出文件和处理相关信息的linux命令。 lsof
支持不同类型的文件格式,例如常规文件,目录,阻止特殊文件等lsof
提供给定的命令输出可解析格式,其中诸如cut
类的其他工具可用于过滤输出。 在本教程中,我们将介绍lsof
命令的不同用例。
In order to run lsof
command without problem and full fledged mode we should provide root
privileges.
为了在没有问题和完全成熟的模式下运行lsof
命令,我们应该提供root
特权。
列出进程和文件 (List Process and Files)
The most basic usage of lsof
is executing without providing any option. We will run lsof
.
lsof
最基本用法是在不提供任何选项的情况下执行。 我们将运行lsof
。
$ lsof
COMMAND
columns shows the command or process nameCOMMAND
列显示命令或进程名称PID
columns shows process idPID
列显示进程IDUSER
columns shows the owner of processUSER
列显示流程的所有者FD
columns shows file descriptor like memory, txt etc.FD
列显示文件描述符,例如内存,txt等。TYPE
shows type like directory, memory regionTYPE
显示目录,内存区域等类型DEVICE
column shows the device major and minor idDEVICE
列显示设备的主要和次要IDSIZE/OFF
column shows theSIZE/OFF
栏显示NODE
column shows node isNODE
列显示节点为NAME
column shows the name of the opened fileNAME
列显示打开的文件的名称
Here list of FD
or File Descriptor Types.
此处列出了FD
或文件描述符类型。
CWD
current working directoryCWD
当前工作目录TXT
text fileTXT
文字档MEM
memory mapped fileMEM
内存映射文件MMAP
memory mapped deviceMMAP
内存映射的设备NUMBER
file descriptorNUMBER
文件描述符
Here list of TYPE
TYPE
清单
REG
regular fileREG
常规文件DIR
directoryDIR
目录FIFO
first in first outFIFO
CHR
character special fileCHR
字符专用文件
列出打开特定文件的过程(List Process Which Opened Specific File)
We can list processes those opened given file. We only need to specify the file with its full path.
我们可以列出那些打开给定文件的进程。 我们只需要指定文件的完整路径即可。
$ lsof /home/ismail
在给定目录中列出打开的文件(List Opened File In The Given Directory)
If we want to list currently opened files in the given directory and subdirectory recursively we can use +D
option and the directory name. If we do not want to list subdirectories recursively we can use +d
option like below. In this example we will list all opened files under /usr/bin/
如果要在给定目录和子目录中递归列出当前打开的文件,则可以使用+D
选项和目录名。 如果我们不想递归列出子目录,可以使用如下的+d
选项。 在此示例中,我们将在/usr/bin/
所有打开的文件
$ lsof +D /usr/bin
根据进程名称列出和过滤(List and Filter According To Process Name)
Another useful option is listing and filtering files according to given process name. We will use -c
option and the process name. In this example we will list files opened by the ssh
process.
另一个有用的选项是根据给定的进程名称列出和过滤文件。 我们将使用-c
选项和进程名称。 在此示例中,我们将列出ssh
进程打开的文件。
$ lsof -c ssh
根据挂载点列出过程(List Process According To Mount Point)
We can also list processes according to file mount point. Actually this is the same as with the directory option +D
. In this example we assume that /dev/sdb0
is mounted at /mnt/
and we want to list only this partition processes.
我们还可以根据文件挂载点列出进程。 实际上,这与目录选项+D
。 在此示例中,我们假设将/dev/sdb0
安装在/mnt/
并且我们只想列出该分区进程。
$ lsof +D /mnt
根据用户列出文件 (List Files According To User)
Now another useful option is listing files according to process owner. We can use -u
option with the process owner name. In this example we will list files those have opened by the processes owned by ismail
.
现在,另一个有用的选项是根据流程所有者列出文件。 我们可以在进程所有者名称中使用-u
选项。 在此示例中,我们将列出由ismail
拥有的进程打开的文件。
$ lsof -u ismail
列出打开的文件(给定用户除外)(List Files Opened Except Given User)
In previous example we have listed all files opened by given user. There is also situation where we want to list opened files except given user. We should add ^
before the user name. In this example we will list all files opened except user root
.
在前面的示例中,我们列出了给定用户打开的所有文件。 在某些情况下,我们要列出给定用户以外的打开文件。 我们应该在用户名之前添加^
。 在此示例中,我们将列出除root
用户之外所有打开的文件。
$ lsof -u ^root
按特定过程列出所有打开的文件 (List All Open Files By Specific Process)
We can list files opened by specific process by providing the process id. We will use -p
command and processes id . In this example we will list files opened by process id 1107
.
我们可以通过提供进程ID列出由特定进程打开的文件。 我们将使用-p
命令并处理id。 在此示例中,我们将列出由进程ID 1107
打开的文件。
$ sudo lsof -p 1107
反复运行Lsof(Run Lsof Repeatedly)
We may need to refresh lsof output repeatedly. This can be done with helper commands but lsof also provides option for this -r
and period in seconds. In this example we will repeat lsof output in 3 seconds.
我们可能需要重复刷新lsof输出。 可以使用辅助程序命令完成此操作,但是lsof还提供此选项-r
和以秒为单位的周期。 在此示例中,我们将在3秒内重复lsof输出。
$ sudo lsof -p 1107 -r 3
In order to exit from repeat mode we can use CTRL+C
为了退出重复模式,我们可以使用CTRL+C
和多种选择 (And Multiple Options)
lsof command can be accept multiple options to filter. The default behaviour is or
logic for all given options which will create a long list. If we need to and
given options we can use -a
.In this example we will list only files opened process id 1107
and owner ismail
.
lsof命令可以接受多个选项进行过滤。 所有给定选项的默认行为是or
逻辑,这将创建一个长列表。 如果需要and
提供选项,则可以使用-a
。在本示例中,我们将仅列出进程ID为1107
且所有者为ismail
。
$ lsof -p 1107 -u ismail -a
列出所有网络连接(List All Network Connections)
lsof command is very capable with network connections. We can list currently opened network connections with the -i
option like below.
lsof命令非常适合网络连接。 我们可以使用-i
选项列出当前打开的网络连接,如下所示。
$ lsof -i
列出所有IPv4网络连接(List All IPv4 Network Connections)
We can list only IPv4 opened network connections with -i4
option like below.
我们可以使用-i4
选项仅列出IPv4打开的网络连接,如下所示。
$ lsof -i4
列出所有IPv6网络连接(List All IPv6 Network Connections)
We can list only IPv4 opened network connections with -i6
option like below.
我们可以使用-i6
选项仅列出IPv4打开的网络连接,如下所示。
$ lsof -i6
根据端口列出进程(List Processes According Port)
We can list processes according to their opened ports. We will use -i :
option and the port number. In this example we will list processes opened port 22
.
我们可以根据进程打开的端口列出它们。 我们将使用-i :
选项和端口号。 在此示例中,我们将列出打开端口22
进程。
$ lsof -i :22
列出TCP连接(List TCP Connections)
We can list only TCP connections with the -i tcp
option.
我们只能使用-i tcp
选项列出TCP连接。
$ lsof -i tcp
$ lsof -i tcp
列出UDP连接(List UDP Connections)
We can list only TCP connections with the -i udp
option.
我们只能使用-i udp
选项列出TCP连接。
$ lsof -i udp
列出NFS文件(List NFS Files)
We can list NFS files by using -N
option. We can also provide the username of the processes with -u
option too. In this example we will list NFS processes opened processes owned ismail
我们可以使用-N
选项列出NFS文件。 我们也可以使用-u
选项提供进程的用户名。 在此示例中,我们将列出拥有ismail
打开进程的NFS进程
$ lsof -N -u ismail -a
翻译自: https://www.poftut.com/linux-lsof-command-tutorial-examples/
linux lsof命令