一、基本介绍
(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
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.
|
文件类型:
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
到此文件恢复完成!
|