Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the lsof command on Linux system

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Linux system lsof command how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Lsof is a tool that lists files currently open by the system. In the linux environment, everything exists in the form of files, through which you can access not only regular data, but also network connections and hardware.

The lsof output format is:

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME [root@uyhd000225 ~] # lsof | tail-5mysqld 30275 mysql 146u REG 3 REG 1 385284 1543089 / var/lib/mysql/mysqldb/fiss_indx_info.MYDmysqld 30275 mysql 156u REG 3 35984 2752534 / var/lib/mysql/mysqldb/inf_res_cons_price_idx_m.MYDmysqld 30275 Mysql 157u REG 3,1 0 2752535 / var/lib/mysql/mysqldb/inf_res_cons_price_idx_y.MYDmysqld 30275 mysql 158u REG 3,1 4240404 2752536 / var/lib/mysql/mysqldb/insure_baseinfo.MYDmysqld 30275 mysql 159u REG 3,1 0 2752538 / var/lib/mysql/mysqldb/insure_fee.MYD [ Root@uyhd000225 ~] # commonly includes the following fields: more visible manual. 1. By default, COMMAND displays the name of a command with a length of 9 characters. You can use the + c parameter to specify the width of the display. If the parameter followed by + c is zero, the full name of the command 2, PID: the ID number of the process, and the IP number of the PPID parent process are displayed. It is not displayed by default, and can be opened when using the-R parameter. 4. The ID number of the PGID process group, which is not displayed by default and can be turned on when using the-g parameter. 5. The UID of the execution of the USER command or the user name of the login in the system. The default is the user name, and UID can be displayed when the-l parameter is used. 6. FD is the File Descriptor number of the file, or the following: (it is difficult to translate the corresponding meaning here, keep it in English)

Quote

The File Descriptor number display modes of cwd current working directory;Lnn library references (AIX), jld jail directory (FreeBSD), ltx shared library text (code and data), Mxx hex memory-mapped type number xx.m86 DOS Merge mapped file;mem memory-mapped file;mmap memory-mapped device;pd parent directory;rtd root directory;tr kernel trace file (OpenBSD), txt program text (code and data) and v86 VP/ix mapped file; files are:

Quote

R for read access;w for write access;u for read and write access;N for a Solaris NFS lock of unknown typer for read lock on part of the file;R for a read lock on the entire file;w for a write lock on part of the file;W for a write lock on the entire file;u for a read and write lock of any length;U for a lock of unknown type;x for an SCO OpenServer Xenix lock on part of the file;X for an SCO OpenServer Xenix lock on the entire file;space if there is no lock.7 and TYPE refer to IPv4 IPv4's package IPv6 uses a package in IPv6 format, and even if the address is IPv4, it will be displayed as IPv6 and mapped to the address of IPv6; for details of the LINK link file in the DIR directory, see the more comments in manual.

8. DEVICE uses the device number represented by character special and block special

9. The size of the SIZE file, if it cannot be represented by size, will be left blank. Use the-s parameter control.

10. Node code of NODE local file, or protocol, such as TCP, etc.

11. The full path of the NAME mount point and the file (the link will be resolved to the actual path), or the address, port, status of both sides of the connection, etc.

Common examples: 1. Show the process of opening the file / home/oracle/10.2.0/db_1/bin/tnslsnr

[root@svr-db-test] # lsof / home/oracle/10.2.0/db_1/bin/tnslsnrCOMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEtnslsnr 3520 oracle txt REG 253 5 431062 11408866 / home/oracle/10.2.0/db_1/bin/tnslsnr2. Know what program port 22 is running now.

[root@svr-db-test] # lsof-I: 22COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEsshd 3101 root 3u IPv6 8670 TCP *: ssh (LISTEN) sshd 4545 root 3u IPv6 4237972 TCP 203.aibo.com Gloza SSH-> win-avbmq9e8ka7.gdgg.local:nsjtp-ctrl (ESTABLISHED) 3. Displays the files now opened by the init process

[root@svr-db-test] # lsof-c initCOMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEinit 1 root cwd DIR 253 0 4096 2 / init 1 root rtd DIR 253 0 4096 2 / init 1 root txt REG 253 0 43496 524446 / sbin/initinit 1 root mem REG 253 0 130448 917826 / lib64/ld-2.5.soinit 1 root mem REG 253 0 1678480 917827 / lib64/libc-2.5.soinit 1 root mem REG 253 23520 917686 / lib64/libdl-2.5.soinit 1 root mem REG 253 lib64/libsepol.so.1init 0 247528 917844 / lib64/libsepol.so.1init 1 root mem REG 253 FIFO 0 95480 917845 / lib64/libselinux.so.1init 1 root 10u FIFO 0 162311 / dev/initctl see which files are opened by the process with process number 1

[root@svr-db-test] # lsof-p 1COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEinit 1 root cwd DIR 253 0 4096 2 / init 1 root rtd DIR 253 0 4096 2 / init 1 root txt REG 253 0 43496 524446 / sbin/initinit 1 root mem REG 253 0 130448 917826 / lib64/ld-2.5.soinit 1 root mem REG 253 root mem REG 0 1678480 917827 / lib64/libc-2.5.soinit 1 root mem REG 253 23520 917686 / lib64/libdl-2.5.soinit 1 root mem REG 253 lib64/libsepol.so.1init 0 247528 917844 / lib64/libsepol.so.1init 1 root mem REG 253 FIFO 0 95480 917845 / lib64/libselinux.so.1init 1 root 10u FIFO 0 162311 / dev/initctl shows the process belonging to 3520

[root@svr-db-test] # lsof-g 3520COMMAND PID PGID USER FD TYPE DEVICE SIZE NODE NAMEtnslsnr 3520 3520 oracle cwd DIR 253 5 4096 11059201 / home/oracletnslsnr 3520 3520 oracle rtd DIR 253 0 4096 2 / tnslsnr 3520 3520 oracle txt REG 253 5 431062 11408866 / home/oracle/10.2.0/db_1/bin/tnslsnrtnslsnr 3520 3520 oracle mem REG 253,0 130448 917826 / lib64/ld-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 1678480 917827 / lib64/libc-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 23520 917686 / lib64/libdl-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 615136 917834 / lib64/libm-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 141208 917829 / lib64/libpthread-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 109824 917839 / lib64/libnsl-2.5.sotnslsnr 3520 3520 oracle mem REG 253,5 20706622 11405436 / home/oracle/10.2.0/db_1/lib/libclntsh.so.10.1tnslsnr 3520 3520 oracle mem REG 253,5 3803097 11410641 / home/oracle/10.2.0/db_1/lib/libnnz10.sotnslsnr 3520 3520 oracle mem REG 253,5 83493 11407251 / home/oracle/10.2.0/db_1/lib/libons.sotnslsnr 3520 3520 oracle mem REG 253,0 53880 917532 / lib64/libnss_files-2.5.sotnslsnr 3520 3520 oracle mem REG 253,5 8545 11407615 / home/oracle/10.2.0/db_1/lib/libskgxn2.so...6. Search by folder / home/oracle, but will not open the subdirectory to display the files opened by the process in the directory

[root@svr-db-test ~] # lsof + d / home/oracleCOMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEtnslsnr 3520 oracle cwd DIR 253 5 4096 11059201 / home/oracle Open / home/oracle folder and its subdirectory search to display the files opened by the process in the directory

[root@svr-db-test ~] # lsof + D / home/oracle shows too much content to display.

Lsof-I is used to display the syntax of qualified processes: lsof-I [46] protocol [: service | port] 46-> IPv4 or IPv6 protocol-> TCP or UDP hostname-> Internet hostname hostaddr-> IPv4 location service-> service name (can be more than one) port-> port number (can be more than one) in / etc/service example:

[root@svr-db-test ~] # lsof-I tcp@192.168.2.245:1521-nCOMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEoracle 15633 oracle 16u IPv4 4069605 TCP 192.168.2.203 tcp@192.168.2.245:1521 31580-> 192.168.2.245:ncube-lm (ESTABLISHED) or [root@svr-db-test ~] # lsof-I tcp@192.168.2.245:1521COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEoracle 15633 oracle 16u IPv4 4069605 TCP 203.aibo.com lsof 31580-> 192.168.2.245:ncube-lm (ESTABLISHED) lsof-n does not convert IP to hostname The default is not to add the-n parameter

Displays a user's open file (or a file that has been opened by the user's executor)

[root@svr-db-test ~] # lsof-u oracle or [root@svr-db-test ~] # lsof-u 0 only print process, which is convenient for shell script to call

[root@svr-db-test ~] # lsof-tc sshd31014545 concern: process debugging commands: truss, strace and ltrace

The process cannot be started, the software suddenly slows down, and the "SegmentFault" of the program is a headache for every Unix system user, and these problems can be quickly diagnosed by using three commonly used debugging tools, truss, strace and ltrace.

Thank you for reading this article carefully. I hope the article "how to use lsof commands in Linux system" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report