Lsof (list open files) is a tool that lists open files for the current system. In a Linux environment, everything is in the form of files, and files can access not only regular data, but also network connections and hardware. Therefore, such as Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) sockets, the system assigns a file descriptor to the application in the background, regardless of the nature of the file, which provides a common interface for the interaction between the application and the underlying operating system. Because the application opens a descriptor list of files that provides a lot of information about the application itself, it is helpful to see the list through the Lsof tool for system monitoring and troubleshooting.
Figure 1:lsof Example
Enter lsof under Terminal to display the file opened by the system, because lsof needs to access core memory and various files, so it must run as root to fully function.
If 1 is an output example of a lsof, each line displays an open file, without specifying that all files opened by the process are displayed by default.
The meaning of the lsof output column information is as follows:
COMMAND: Name of the process
PID: Process Identifier
USER: Process Owner
FD: File descriptor that the application uses to identify the file through a file descriptor, such as CWD, TXT, and so on.
Type: File types, such as: DIR, Reg, and so on.
DEVICE: Specifies the name of the disk
Size: Sizes of files
Node: Index node (the identity of the file on disk)
Name: Open the exact name of the file
The file descriptor CWD value in the FD column represents the current working directory of the application, which is the directory that the application launches, unless it makes changes to the directory itself, and the TXT type of file is program code, such as the application binary itself or the shared library. The second value identifies the application's file descriptor, which is an integer returned when the file is opened. The Type column is more intuitive than the FD column. Files and directories are called Reg and Dir, respectively. The CHR and blk, respectively, represent characters and block devices, or UNIX, FIFO, and IPV4, respectively, representing the UNIX domain sockets, first in and Out (FIFO) queues, and Internet Protocol (IP) sockets.
Here are some useful commands:
| 123456789101112131415161718 |
lsof`whichhttpd` //那个进程在使用apache的可执行文件lsof/etc/passwd //那个进程在占用/etc/passwdlsof/dev/hda6//那个进程在占用hda6lsof/dev/cdrom//那个进程在占用光驱lsof-c sendmail //查看sendmail进程的文件使用情况lsof -c courier -u ^zahn //显示出那些文件被以courier打头的进程打开,但是并不属于用户zahnlsof-p 30297 //显示那些文件被pid为30297的进程打开lsof-D /tmp显示所有在/tmp文件夹中打开的instance和文件的进程。但是symbol文件并不在列 lsof -u1000 //查看uid是100的用户的进程的文件使用情况lsof-utony //查看用户tony的进程的文件使用情况lsof-u^tony //查看不是用户tony的进程的文件使用情况(^是取反的意思)lsof-i //显示所有打开的端口lsof -i:80 //显示所有打开80端口的进程lsof-i -U //显示所有打开的端口和UNIX domain文件lsof-i [email protected][url]www.akadia.com:123 //显示那些进程打开了到www.akadia.com的UDP的123(ntp)端口的链接lsof-i [email protected]:ftp-r //不断查看目前ftp连接的情况(-r,lsof会永远不断的执行,直到收到中断信号,+r,lsof会一直执行,直到没有档案被显示,缺省是15s刷新)lsof-i [email protected]:ftp-n //lsof-n 不将IP转换为hostname,缺省是不加上-n参数 |
You can use the Lsof-i: port number to see which programs are currently consuming a port.
Linux lsof Command Usage Summary