����Linux�Ż�
Linux����

Linux lsof����ʹ�����

����ʱ��:2014-08-03 21:25:46��Դ:linux��վ����:wangxiang

��Linux�ն�������lsof������ʾϵͳ�򿪵��ļ�����Ϊ lsof ��Ҫ���ʺ����ڴ�͸����ļ������Ա����� root �û����������������ܹ���ֵط����书�ܡ�


COMMAND    PID      USER  FD      TYPE    DEVICE    SIZE      NODE      NAME
init      1        root  cwd      DIR      3,3      1024      2        /
init      1        root  rtd      DIR      3,3      1024      2        /
init      1        root  txt      REG      3,3      38432      1763452  /sbin/init
init      1        root  mem      REG      3,3      106114    1091620  /lib/libdl-2.6.so
init      1        root  mem      REG      3,3      7560696    1091614  /lib/libc-2.6.so
init      1        root  mem      REG      3,3      79460      1091669  /lib/libselinux.so.1
init      1        root  mem      REG      3,3      223280    1091668  /lib/libsepol.so.1
init      1        root  mem      REG      3,3      564136    1091607  /lib/ld-2.6.so
init      1        root  10u      FIFO      0,15                  1309    /dev/initctl


ÿ����ʾһ���򿪵��ļ�������ָ������Ĭ�Ͻ���ʾ���н��̴򿪵������ļ���lsof���������Ϣ���������£�

COMMAND�����̵�����
PID�����̱�ʶ��
USER������������
FD���ļ���������Ӧ�ó���ͨ���ļ�������ʶ����ļ�����cwd��txt��
TYPE���ļ����ͣ���DIR��REG��
DEVICE��ָ�����̵�����
SIZE���ļ��Ĵ�С
NODE�������ڵ㣨�ļ��ڴ����ϵı�ʶ��
NAME�����ļ���ȷ������


����FD ���е��ļ�������cwd ֵ��ʾӦ�ó���ĵ�ǰ����Ŀ¼�����Ǹ�Ӧ�ó���������Ŀ¼�����������������Ŀ¼���и��ġ�txt ���͵��ļ��dz�����룬��Ӧ�ó���������ļ����������⣬�����б�����ʾ�� /sbin/init ���������ֵ��ʾӦ�ó�����ļ������������Ǵ򿪸��ļ�ʱ���ص�һ�����������ϵ����һ���ļ�/dev/initctl�����ļ�������Ϊ 10��u ��ʾ���ļ����򿪲����ڶ�ȡ/д��ģʽ��������ֻ�� ? ��ֻд (w) ģʽ��ͬʱ���д�д ��W ��ʾ��Ӧ�ó�����ж������ļ���д�������ļ�����������ȷ��ÿ��ֻ�ܴ�һ��Ӧ�ó���ʵ������ʼ��ÿ��Ӧ�ó���ʱ�������������ļ����������� 0 �� 2���ֱ��ʾ��׼���롢����ʹ����������Դ����Ӧ�ó������򿪵��ļ��� FD ���Ǵ� 3 ��ʼ��

�� FD ����ȣ�Type ����Ƚ�ֱ�ۡ��ļ���Ŀ¼�ֱ��Ϊ REG �� DIR����CHR �� BLK���ֱ��ʾ�ַ��Ϳ��豸������ UNIX��FIFO �� IPv4���ֱ��ʾ UNIX ���׽��֡��Ƚ��ȳ� (FIFO) ���к�����Э�� (IP) �׽��֡�


lsof����
lsof �������÷��Dz���Ӧ�ó���򿪵��ļ������ƺ���Ŀ�������ڲ��ҳ�ij���ض�Ӧ�ó�����־���ݼ�¼���δ����������ڸ���ij�����⡣���磬linux�����˽� ���ܹ����ļ�����Ŀ��ͨ�������ֵ�ܴ����Բ���������⣬��������Ҫʱ��Ӧ�ó��������������ֵ��ֱ��ij�����ޣ�������㻳��Ӧ�ó���ľ����ļ��� ��������ô����ʹ�� lsof ͳ�ƴ򿪵��ļ���Ŀ���Խ�����֤��


lsof�﷨��ʽ�ǣ�

lsof ��options�� filename

���õIJ����б���

lsof  filename ��ʾ��ָ���ļ������н���
lsof -a ��ʾ������������������ʱ����ʾ���
lsof -c string  ��ʾCOMMAND���а���ָ���ַ��Ľ������д򿪵��ļ�
lsof -u username  ��ʾ����user���̴򿪵��ļ�
lsof -g gid ��ʾ����gid�Ľ������
lsof +d /DIR/ ��ʾĿ¼�±����̴򿪵��ļ�
lsof +D /DIR/ ͬ�ϣ����ǻ�����Ŀ¼�µ�����Ŀ¼��ʱ����Խϳ�
lsof -d FD ��ʾָ���ļ��������Ľ���
lsof -n ����IPת��Ϊhostname��ȱʡ�Dz�����-n����
lsof -i ������ʾ���������Ľ������
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
46 --> IPv4 or IPv6
protocol --> TCP or UDP
hostname --> Internet host name
hostaddr --> IPv4��ַ
service --> /etc/service�е� service name (���Բ�ֻһ��)
port --> �˿ں� (���Բ�ֻһ��)


���磺�鿴22�˿��������е����

# lsof -i :22
COMMAND  PID USER  FD  TYPE DEVICE SIZE NODE NAME
sshd    1409 root    3u  IPv6  5678      TCP *:ssh (LISTEN)


�鿴����root�û��������򿪵��ļ�����Ϊtxt���ļ�:

# lsof -a -u root -d txt
COMMAND    PID USER  FD      TYPE DEVICE    SIZE    NODE NAME
init      1    root txt      REG    3,3  38432 1763452 /sbin/init
mingetty  1632 root txt      REG    3,3  14366 1763337 /sbin/mingetty
mingetty  1633 root txt      REG    3,3  14366 1763337 /sbin/mingetty
mingetty  1634 root txt      REG    3,3  14366 1763337 /sbin/mingetty
mingetty  1635 root txt      REG    3,3  14366 1763337 /sbin/mingetty
mingetty  1636 root txt      REG    3,3  14366 1763337 /sbin/mingetty
mingetty  1637 root txt      REG    3,3  14366 1763337 /sbin/mingetty
kdm        1638 root txt      REG    3,3  132548 1428194 /usr/bin/kdm
X          1670 root txt      REG    3,3 1716396 1428336 /usr/bin/Xorg
kdm        1671 root txt      REG    3,3  132548 1428194 /usr/bin/kdm
startkde  2427 root txt      REG    3,3  645408 1544195 /bin/bash
... ...


lsofʹ��ʵ��

һ������˭��ʹ���ļ�ϵͳ
��ж���ļ�ϵͳʱ��������ļ�ϵͳ�����κδ򿪵��ļ�������ͨ������ʧ�ܡ���ôͨ��lsof�����ҳ���Щ������ʹ�õ�ǰҪж�ص��ļ�ϵͳ�����£�

# lsof /GTES11/
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 4208 root cwd DIR 3,1 4096 2 /GTES11/
vim 4230 root cwd DIR 3,1 4096 2 /GTES11/
�����ʾ���У��û�root������/GTES11Ŀ¼�н���һЩ������һ�� bash��ʵ���������У���������ǰ��Ŀ¼Ϊ/GTES11����һ������ʾ����vim���ڱ༭/GTES11�µ��ļ���Ҫ�ɹ���ж��/GTES11��Ӧ�� ��֪ͨ�û���ȷ���������֮����ֹ��Щ���̡� ���ʾ��˵����Ӧ�ó���ĵ�ǰ����Ŀ¼�dz���Ҫ����Ϊ���Ա������ļ���Դ�����ҿ��Է�ֹ�ļ�ϵͳ��ж�ء������Ϊʲô�󲿷��ػ����̣���̨���̣������ǵ� Ŀ¼����Ϊ��Ŀ¼��������ض���Ŀ¼���� sendmail ʾ���е� /var/spool/mqueue����ԭ���Ա�����ػ�������ֹж�ز���ص��ļ�ϵͳ��


�����ָ�ɾ�����ļ�
��Linux������ܵ�����ʱ���������������־�ļ���ɾ�������ڸǹ����ߵ��ټ�����������Ҳ���ܵ�������ɾ����Ҫ���ļ�����������������־ʱ�������ɾ�������ݿ�Ļ������־����ʱ����ͨ��lsof���ָ���Щ�ļ���

�����̴���ij���ļ�ʱ��ֻҪ�ý��̱��ִ򿪸��ļ�����ʹ����ɾ��������Ȼ�����ڴ����С�����ζ�ţ����̲���֪���ļ��Ѿ���ɾ��������Ȼ������򿪸��ļ�ʱ�ṩ�������ļ����������ж�ȡ��д�롣���˸ý���֮�⣬����ļ��Dz��ɼ��ģ���Ϊ�Ѿ�ɾ��������Ӧ��Ŀ¼�����ڵ㡣

��/proc Ŀ¼�£����а����˷�ӳ�ں˺ͽ������ĸ����ļ���/procĿ¼���ص������ڴ�����ӳ���һ������������Щ�ļ���Ŀ¼���������ڴ����У���˵����Ƕ���Щ�ļ����ж�ȡ��д��ʱ��ʵ�������ڴ��ڴ��л�ȡ�����Ϣ��������� lsof ��ص���Ϣ���洢���Խ��̵� PID ������Ŀ¼�У��� /proc/1234 �а������� PID Ϊ 1234 �Ľ��̵���Ϣ��ÿ������Ŀ¼�д����Ÿ����ļ������ǿ���ʹ��Ӧ�ó���򵥵��˽���̵��ڴ�ռ䡢�ļ��������б���ָ������ϵ��ļ��ķ������Ӻ�����ϵͳ�� Ϣ��lsof ����ʹ�ø���Ϣ�����������ں��ڲ�״̬����Ϣ�����������������lsof ������ʾ���̵��ļ�����������ص��ļ�������Ϣ��Ҳ��������ͨ�����ʽ��̵��ļ������������ҵ����ļ��������Ϣ��

��ϵͳ�е�ij���ļ��������ɾ���ˣ�ֻҪ���ʱ��ϵͳ�л��н������ڷ��ʸ��ļ�����ô���ǾͿ���ͨ��lsof��/procĿ¼�»ָ����ļ������ݡ� ���������������/var/log/messages�ļ�ɾ�����ˣ���ô��ʱҪ��/var/log/messages�ļ��ָ��ķ������£�

����ʹ��lsof���鿴��ǰ�Ƿ��н��̴�/var/logmessages�ļ������£�

# lsof |grep /var/log/messages
syslogd 1283 root 2w REG 3,3 5381017 1773647 /var/log/messages (deleted)
���������Ϣ���Կ��� PID 1283��syslogd�����ļ����ļ�������Ϊ 2��ͬʱ�����Կ���/var/log/messages�Ѿ���DZ�ɾ���ˡ�������ǿ����� /proc/1283/fd/2 ��fd�µ�ÿ���������������ļ���ʾ���̶�Ӧ���ļ����������в鿴��Ӧ����Ϣ�����£�

# head -n 10 /proc/1283/fd/2
Aug 4 13:50:15 holmes86 syslogd 1.4.1: restart.
Aug 4 13:50:15 holmes86 kernel: klogd 1.4.1, log source = /proc/kmsg started.
Aug 4 13:50:15 holmes86 kernel: Linux version 2.6.22.1-8 (root@everestbuilder.linux-ren.org) (gcc version 4.2.0) #1 SMP Wed Jul 18 11:18:32 EDT 2007
Aug 4 13:50:15 holmes86 kernel: BIOS-provided physical RAM map:
Aug 4 13:50:15 holmes86 kernel: BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
Aug 4 13:50:15 holmes86 kernel: BIOS-e820: 000000000009f000 - 00000000000a0000 (reserved)
Aug 4 13:50:15 holmes86 kernel: BIOS-e820: 0000000000100000 - 000000001f7d3800 (usable)
Aug 4 13:50:15 holmes86 kernel: BIOS-e820: 000000001f7d3800 - 0000000020000000 (reserved)
Aug 4 13:50:15 holmes86 kernel: BIOS-e820: 00000000e0000000 - 00000000f0007000 (reserved)
Aug 4 13:50:15 holmes86 kernel: BIOS-e820: 00000000f0008000 - 00000000f000c000 (reserved)
���������Ϣ���Կ������鿴 /proc/8663/fd/15 �Ϳ��Եõ���Ҫ�ָ������ݡ��������ͨ���ļ��������鿴��Ӧ�����ݣ���ô�Ϳ���ʹ�� I/O �ض����临�Ƶ��ļ��У���:

cat /proc/1283/fd/2 > /var/log/messages