linux命令之lsof

简介:

一、基本介绍

(1)lsof命令用于查看你进程开打的文件,打开文件的进程,进程打开的端口(TCP、UDP).

(2)找回/恢复删除的文件.


二、lsof语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
lsof (选项) filename
  
-a:列出打开文件存在的进程;
-c<进程名>:列出指定进程所打开的文件; 
-g:列出GID号进程详情; 
-d<文件号>:列出占用该文件号的进程; 
+d<目录>:列出目录下被打开的文件; 
+D<目录>:递归列出目录下被打开的文件; 
-n<目录>:列出使用NFS的文件; 
-i<条件>:列出符合条件的进程。(4、6、协议、:端口、 @ip ) 
-p<进程号>:列出指定进程号所打开的文件; 
-u:列出UID号进程详情; 
-h:显示帮助信息; 
- v :显示版本信息。

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
lsof  abc.txt 显示开启文件abc.txt的进程
lsof  -c abc 显示abc进程现在打开的文件
lsof  -c -p 1234 列出进程号为1234的进程所打开的文件
lsof  -g gid 显示归属gid的进程情况
lsof  +d  /usr/local/  显示目录下被进程开启的文件
lsof  +D  /usr/local/  同上,但是会搜索目录下的目录,时间较长
lsof  -d 4 显示使用fd为4的进程
lsof  -i 用以显示符合条件的进程情况
lsof  -i[46] [protocol][@ hostname |hostaddr][:service|port]
--> IPv4 or IPv6
   protocol --> TCP or UDP
   hostname  --> Internet host name
   hostaddr --> IPv4地址
   service -->  /etc/service 中的 service name (可以不止一个)
   port --> 端口号 (可以不止一个)


三、详解lsof输出结果

1
2
3
4
5
6
[root@node1 ~] # cd /mnt/
[root@node1 mnt] # lsof /mnt/
COMMAND  PID USER   FD   TYPE DEVICE SIZE /OFF  NODE NAME
bash     1523 root  cwd    DIR   11,1     4096 1856  /mnt
lsof     1669 root  cwd    DIR   11,1     4096 1856  /mnt
lsof     1670 root  cwd    DIR   11,1     4096 1856  /mnt

COMMAND:进程的名称 

PID:进程标识符

USER:进程所有者

FD:文件描述符,应用程序通过文件描述符识别该文件。如cwd、txt等 

TYPE:文件类型,如DIR、REG等

DEVICE:指定磁盘的名称

SIZE:文件的大小

NODE:索引节点(文件在磁盘上的标识)

NAME:打开文件的确切名称


文件描述符列表: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
cwd:表示current work dirctory,即:应用程序的当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行更改。
txt:该类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的  /sbin/init  程序。
lnn:library references (AIX); 
er:FD information error (see NAME column); 
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); 
v86  VP /ix  mapped  file
0:表示标准输出 
1:表示标准输入 
2:表示标准错误
 
 
一般在标准输出、标准错误、标准输入后还跟着文件状态模式: 
u:表示该文件被打开并处于读取/写入模式。 
r:表示该文件被打开并处于只读模式。 
w:表示该文件被打开并处于。 
空格:表示该文件的状态模式为unknow,且没有锁定。 
-:表示该文件的状态模式为unknow,且被锁定。 
 
  
同时在文件状态模式后面,还跟着相关的锁: 
N: for  a Solaris NFS lock of unknown  type
r: for  read  lock on part of the  file
R: for  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  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.

 

文件类型:

1
2
3
4
5
6
7
DIR:表示目录。 
CHR:表示字符类型。 
BLK:块设备类型。 
UNIX: UNIX 域套接字。 
FIFO:先进先出 (FIFO) 队列。 
IPv4:网际协议 (IP) 套接字。 
DEVICE:指定磁盘的名称



四、对删除的文件进行恢复(该文件删除前已被进程打开)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@node1 ~] # lsof /var/log/messages
COMMAND   PID USER   FD   TYPE DEVICE SIZE /OFF    NODE NAME
rsyslogd 1252 root    4w   REG  253,0   201170 261273  /var/log/messages
tail      1550 root    3r   REG  253,0   201170 261273  /var/log/messages
[root@node1 ~] # rm -rf  /var/log/messages 
[root@node1 ~]
[root@node1 ~] # lsof /var/log/messages
lsof : status error on  /var/log/messages : No such  file  or directory
lsof  4.82
  latest revision:  ftp : //lsof .itap.purdue.edu /pub/tools/unix/lsof/
  latest FAQ:  ftp : //lsof .itap.purdue.edu /pub/tools/unix/lsof/FAQ
  latest  man  page:  ftp : //lsof .itap.purdue.edu /pub/tools/unix/lsof/lsof_man
  usage: [-?abhlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
  [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Use the ``-h ''  option to get  more  help information.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@node1 ~]# lsof | grep /var/log/messages
rsyslogd  1252    root    4w      REG              253,0   201170     261273 /var/log/messages (deleted)
tail      1550    root    3r      REG              253,0   201170     261273 /var/log/messages (deleted)
 
[root@node1 ~]# cat /proc/1252/fd/4 >aaa.txt 
[root@node1 ~]# cat /proc/1550/fd/3 >bbb.txt
 
[root@node1 ~]# ls -lrt 
total 420
-rw-r--r--. 1 root root   3161 Jun 18 19:58 install.log.syslog
-rw-r--r--. 1 root root   9913 Jun 18 19:59 install.log
-rw-------. 1 root root   1116 Jun 18 19:59 anaconda-ks.cfg
-rw-r--r--  1 root root 201170 Jul  6 20:17 aaa.txt
-rw-r--r--  1 root root 201365 Jul  6 20:18 bbb.txt
[root@node1 ~]# diff aaa.txt bbb.txt 
[root@node1 ~]#
 
[root@node1 ~]# cp aaa.txt  /var/log/messages 
到此文件恢复完成!
本文转自   a8757906   51CTO博客,原文链接:http://blog.51cto.com/nxyboy/1945123
相关文章
|
17天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
54 23
Linux系统之whereis命令的基本使用
|
3月前
|
Linux 网络安全 数据安全/隐私保护
Linux 超级强大的十六进制 dump 工具:XXD 命令,我教你应该如何使用!
在 Linux 系统中,xxd 命令是一个强大的十六进制 dump 工具,可以将文件或数据以十六进制和 ASCII 字符形式显示,帮助用户深入了解和分析数据。本文详细介绍了 xxd 命令的基本用法、高级功能及实际应用案例,包括查看文件内容、指定输出格式、写入文件、数据比较、数据提取、数据转换和数据加密解密等。通过掌握这些技巧,用户可以更高效地处理各种数据问题。
383 8
|
3月前
|
监控 Linux
如何检查 Linux 内存使用量是否耗尽?这 5 个命令堪称绝了!
本文介绍了在Linux系统中检查内存使用情况的5个常用命令:`free`、`top`、`vmstat`、`pidstat` 和 `/proc/meminfo` 文件,帮助用户准确监控内存状态,确保系统稳定运行。
1203 6
|
3月前
|
Linux
在 Linux 系统中,“cd”命令用于切换当前工作目录
在 Linux 系统中,“cd”命令用于切换当前工作目录。本文详细介绍了“cd”命令的基本用法和常见技巧,包括使用“.”、“..”、“~”、绝对路径和相对路径,以及快速切换到上一次工作目录等。此外,还探讨了高级技巧,如使用通配符、结合其他命令、在脚本中使用,以及实际应用案例,帮助读者提高工作效率。
227 3
|
3月前
|
监控 安全 Linux
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景,包括 ping(测试连通性)、traceroute(跟踪路由路径)、netstat(显示网络连接信息)、nmap(网络扫描)、ifconfig 和 ip(网络接口配置)。掌握这些命令有助于高效诊断和解决网络问题,保障网络稳定运行。
152 2
|
4天前
|
缓存 Ubuntu Linux
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
53 25
|
2天前
|
缓存 Linux
Linux查看内存命令
1. free free命令是最常用的查看内存使用情况的命令。它显示系统的总内存、已使用内存、空闲内存和交换内存的总量。 free -h • -h 选项:以易读的格式(如GB、MB)显示内存大小。 输出示例: total used free shared buff/cache available Mem: 15Gi 4.7Gi 4.1Gi 288Mi 6.6Gi 9.9Gi Swap: 2.0Gi 0B 2.0Gi • to
14 2
|
1月前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
42 11
|
2月前
|
Linux Shell
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
117 14
Linux 10 个“who”命令示例
|
2月前
|
Ubuntu Linux
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
243 20