Linux Common commands: lsof

Source: Internet
Author: User

lsof Introduction

Lsof is the abbreviation for the English list open file, which lists all the files on the system that were opened by the process. In a Linux environment, everything is file, anything is in the form of a file

exist. When any process opens a file, he applies a file descriptor to the kernel, which provides a common interface for the interaction between the process and the operating system. Because

Opens the descriptor for the file for the application, which provides a lot of information about the process itself, which we can view through the lsof command.

lsof Command Output information interpretation

We enter this command on the terminal to list all files opened by the system, since lsof needs access to core memory and system resources, so in order to see all the full

Information, we'd better use root to execute this command. The result of this command execution is as follows:

 command PID USER FD TYPE DEVICE SIZE NODE      Nameinit 1 root cwd DIR 3,3 1024x768 2/init 1 root RTD DIR 3,3 1024x768 2/init 1 root txt REG 3,3 38432 1763452/SB In/initinit 1 root mem REG 3,3 106114 1091620/lib/libdl-2.6.soinit 1 R       Oot mem Reg 3,3 7560696 1091614/lib/libc-2.6.soinit 1 root mem reg 3,3 79460 1091669/lib/libselinux.so.1init 1 root mem REG 3,3 223280 1091668/l         Ib/libsepol.so.1init 1 root mem REG 3,3 564136 1091607/lib/ld-2.6.soinit 1 Root 10u FIFO 0,15 1309/dev/initctl  

Lsof Each line shows an open file, and if not, the default is to list all open files for the process, as follows:

Command: Name of the process

PID: identifier of the process

USER: Owner of the process

FD: File descriptor

Type: Types of files

DEVICE: Specifies the name of the disk

Size: Sizes of files

Node: Index node

Name: Open the exact name of the file

Key Insights:

FD: File descriptor

CWD: Represents the current working directory of the application.

TXT: Indicates that the file is a program code, such as the binary program itself or a shared library, such as the/sbin/init program shown above.

10u: Numeric represents the application's file descriptor, which is an integer returned when the file is opened. U means the open file is in read and write mode.

? represents read-only mode, W denotes write-only mode, W represents the application's write-lock to the entire file, indicating that the file descriptor can only be used by one application at a time

Program use. There are 3 special values in the file descriptor, 0, 1, and 2, which represent standard input, standard output, and error output, respectively. So, the other files

Descriptors typically start at 3.

Type: File type. Example: Unix, REG, DIR, CHR, BLK, FIFO, etc.

lsof Common Parameters

Lsof common use is to find the name and number of files opened by the program. Linux systems often set the number of open files for a process, typically 1024, which can be increased if needed. If

You suspect that your process has exhausted all the file descriptors, then you can use the lsof command to count the number of files opened by the process.

Format:

lsof [options] FileName

lsof FileName: Displays all processes that open the specified file

Lsof-a: Indicates that two parameters must be met before the results are displayed

Lsof-c string: Lists all files opened by a process that contains a string string in the command

Lsof-u Username: Lists all files opened by the process under username user

Lsof-g GID: Shows the process of attribution to GID

Lsof +d/dir/: Show files opened by process in directory

Lsof +d/dir/: Same as above, but will search all directories under directory for a relatively long time

Lsof-d FD: The process that displays the specified file descriptor

Lsof-n: Do not convert IP to hostname, the default is not to add the-n parameter

Lsof-i: Used to show the condition of a process

Example:

1. View information about the process that occupies Port 22nd

Lsof-i: 22

2, Imix user Open, file descriptor for the process of txt

Lsof-a-u imix-d txt

lsof Usage Scenarios1. Find who is using the file system

Usually when we unload the file system, we report disk device busy error, unable to execute successfully, etc. Because when a file on this file system is opened, you will fail the operation.

The normal way to do this is to tell which processes are free to use the file system through normal means, and then you can manipulate it. Because when a process occupies a file system, we cannot

For the relevant operation. Therefore, most daemons (background processes) and specific processes, they will set the working directory to/, to avoid the above problems.

2. Recover deleted Files

The principle of file deletion:

Common file data is divided into: "Metadata" and "real data". The metadata includes the file's: type, permissions, the genus group, size, time, and pointer to the Inode and its block.

The name of the file and its inode correspondence are stored in the metadata information of the parent directory. So, when we delete a file, we delete the corresponding relationship between the filename and the inode.

So in a production environment, deleting content is very block.

Through the metadata information of the directory, the Linux system finds the correspondence between its sub-class capacity file name and its inode, and obtains the data block on the disk according to the index pointer of the inode.

So deleting a file is about deleting the correspondence.

File usage: How are files used in Linux? We behave in a way that counts.

Link count (i_link): When we create a file, we must associate the file name with an inode, which is a hard link. Therefore, the general file created by the

Time, the number of links is at least one.

Reference count (I_count): When a file is occupied by a process (including read, write, execute, and so on), his count is added 1

So, when we delete a file, the two counts are 0 to indicate that the file was deleted.

When a Linux computer is compromised, it is common for the log files to be deleted to conceal the attacker's traces. Administrative errors can also cause accidental deletion of important files, such as when cleaning up old logs,

When a process opens a file, it remains on disk as long as the process remains open for that file, even if it is deleted. This means that the process does not know that the file has been deleted and it can still

In the/proc directory, it contains various files that reflect the kernel and the process tree. The/proc directory mounts an area that is mapped in memory, so these files and directories do not exist on the disk,



The/proc/1234 contains information about the process with PID 1234. There are various files in each process directory that allow the application to simply understand the memory space of the process, the file strokes

Describes list characters, symbolic links to files on disk, and other system information. The LSOF program uses this information and other information about the internal state of the kernel to produce its output. So lsof can show the process





If the/var/log/messages file is deleted due to misoperation, then the method to restore the/var/log/messages file is as follows:

# lsof |grep/var/log/messagessyslogd   1283      root    2w      REG        3,3  5381017    1773647/var/log/ Messages (Deleted)
From the above information you can see that the PID 1283 (syslogd) Open file has a file descriptor of 2. You can also see that/var/log/messages has been flagged for deletion.

Therefore, we can view the corresponding information in/PROC/1283/FD/2 (the file descriptor for each file represented by a digitally named process under FD).

If you can view the data through a file descriptor, you can use I/O redirection to copy it to a file, such as:

CAT/PROC/1283/FD/2 >/var/log/messages

Linux Common commands: lsof

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.