linux lsof命令_带有示例Linux Lsof命令教程

linux lsof命令

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
List Process and Files
List Process and Files
列出进程和文件
  • COMMAND columns shows the command or process name

    COMMAND列显示命令或进程名称

  • PID columns shows process id

    PID列显示进程ID

  • USER columns shows the owner of process

    USER列显示流程的所有者

  • FD columns shows file descriptor like memory, txt etc.

    FD列显示文件描述符,例如内存,txt等。

  • TYPE shows type like directory, memory region

    TYPE显示目录,内存区域等类型

  • DEVICE column shows the device major and minor id

    DEVICE列显示设备的主要和次要ID

  • SIZE/OFF column shows the

    SIZE/OFF栏显示

  • NODE column shows node is

    NODE列显示节点为

  • NAME column shows the name of the opened file

    NAME列显示打开的文件的名称

Here list of FD or File Descriptor Types.

此处列出了FD或文件描述符类型。

  • CWD current working directory

    CWD当前工作目录

  • TXT text file

    TXT文字档

  • MEM memory mapped file

    MEM内存映射文件

  • MMAP memory mapped device

    MMAP内存映射的设备

  • NUMBER file descriptor

    NUMBER文件描述符

Here list of TYPE

TYPE清单

  • REG regular file

    REG常规文件

  • DIR directory

    DIR目录

  • FIFO first in first out

    FIFO

  • CHR character special file

    CHR字符专用文件

列出打开特定文件的过程(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 Process Which Opened Specific File
List Process Which Opened Specific File
列出打开特定文件的过程

在给定目录中列出打开的文件(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 Opened File In The Given Directory
List Opened File In The Given Directory
在给定目录中列出打开的文件

根据进程名称列出和过滤(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 and Filter According To Process Name
List and Filter According To Process Name
根据进程名称列出和过滤

根据挂载点列出过程(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 According To User
List Files According To User
根据用户列出文件

列出打开的文件(给定用户除外)(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
List All Open Files By Specific Process
List All Open Files By Specific Process
按特定过程列出所有打开的文件

反复运行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
Run Lsof Repeatedly
Run Lsof Repeatedly
反复运行Lsof

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
And Multiple Options
And Multiple Options
和多种选择

列出所有网络连接(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
List All Network Connections
List All Network Connections
列出所有网络连接

列出所有IPv4网络连接(List All IPv4 Network Connections)

We can list only IPv4 opened network connections with -i4 option like below.

我们可以使用-i4选项仅列出IPv4打开的网络连接,如下所示。

$ lsof -i4
List All IPv4 Network Connections
List All IPv4 Network Connections
列出所有IPv4网络连接

列出所有IPv6网络连接(List All IPv6 Network Connections)

We can list only IPv4 opened network connections with -i6 option like below.

我们可以使用-i6选项仅列出IPv4打开的网络连接,如下所示。

$ lsof -i6
List All IPv6 Network Connections
List All IPv6 Network Connections
列出所有IPv6网络连接

根据端口列出进程(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
List Processes According Port
List Processes According Port
根据端口列出进程

列出TCP连接(List TCP Connections)

We can list only TCP connections with the -i tcp option.

我们只能使用-i tcp选项列出TCP连接。

$ lsof -i tcp

$ lsof -i tcp

LEARN MORE  Network Classless Inter Domain Routing (CIDR) Cheat Sheet
了解更多网络无类域间路由(CIDR)速查表
List TCP Connections
List TCP Connections
列出TCP连接

列出UDP连接(List UDP Connections)

We can list only TCP connections with the -i udp option.

我们只能使用-i udp选项列出TCP连接。

$ lsof -i udp
List UDP Connections
List UDP Connections
列出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命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值