Brief introduction
lsof (list open files) list the current system is a tool to open the file.
Format: lsof [options] [names]
Lsof output alone perform all open files in the system, we can grep to filter out what we care about, but lsof provide the appropriate parameters to help us pinpoint
lsof name the output file name using all processes
- -p pid pid list all files opened by processes
- -d FD_pattern lists the value of all the opened file is FD_pattern of FD FD are integers, TXT, MEM, etc.
- -A lsof can be added after a plurality of matching conditions, or default connection parameter into a plurality of conditions and relationships
- -i [46] [proto] [@hostname | ip] [: service | port] is used to select a port occupied by the process of
- + D / + D dir recursive or non-recursive display the file open process dir
- command -c string contained in string display process of the process of open files
- -u username display processes belonging to the user to open the file
- -g gid
Files can be opened:
- Common file
- table of Contents
- File Network File System
- Character device file
- (Function) shared library
- Pipes, named pipes
- Symbolic link
- word stream socket underlying network socket, UNIX domain socket
- In linux inside, most of the things that are treated as files
Use lsof
Here it is mainly used in the form of cases to introduce the use of lsof command
1. List all open files:
lsof
NOTE: If no arguments, it will be open all open files, it is recommended to add about specific targeting parameters
2. Check who is using a file
lsof /filepath/file
3. recursive view a catalog file information
lsof +D /filepath/filepath2/
Note: Use the + D, all subdirectories and files are listed under the corresponding directory
4. Use + D than the option to view a directory traversal method for all file information
lsof | grep ‘/filepath/filepath2/’
5. File information a user opens
lsof -u username
Note: -u option, u actually is an acronym for the user
6. File information a program that opens
lsof -c mysql
Note: -c option will list all files that start with mysql program, in fact, you can also write lsof | grep mysql, but the first method is significantly less than a few characters to play second method
7. File information and more open multiple programs
lsof -c mysql -c apache
8. File information a user and a program that opens
lsof -u test -c mysql
9. In addition to the information listed files a user opened
lsof -u ^root
NOTE: ^ this symbol before the username, the root user will open the process to prevent the display
10. Display the file be opened by a process ID
lsof -p 1
11. File information number corresponding to the multiple processes
lsof -p 123,456,789
12. File information in addition to a process ID number opened by other processes
lsof -p ^1
13. List all network connections
lsof -i
14. List all network connection information tcp
lsof -i tcp
15. List all network connection information udp
lsof -i udp
16. list who is using a port
lsof -i :3306
17. The list who is using a particular udp port
lsof -i udp:55
Specific tcp port
lsof -i tcp:80
18. List all active network ports for a user
lsof -a -u test -i
19. List all Network File System
lsof -N
20. domain socket file
lsof -u
21. User Group opened a file information
lsof -g 5555
22. The file description information corresponding to the file listed in the
lsof -d description(like 2)
File information file 23. The description of a range
lsof -d 2-3