lsof全名list
opened files,也就是列举系统中已经被打开的文件。我们都知道,linux环境中,任何事物都是文件,设备是文件,目录是文件,甚至sockets也是文件。所以,用好lsof命令,对日常的linux管理非常有帮助。
lsof是linux最常用的命令之一,通常的输出格式为:
引用COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
常见包括如下几个字段:更多的可见manual。
1、COMMAND默认以9个字符长度显示的命令名称。可使用+c参数指定显示的宽度,若+c后跟的参数为零,则显示命令的全名2、PID:进程的ID号3、PPID父进程的IP号,默认不显示,当使用-R参数可打开。4、PGID进程组的ID编号,默认也不会显示,当使用-g参数时可打开。5、USER命令的执行UID或系统中登陆的用户名称。默认显示为用户名,当使用-l参数时,可显示UID。6、FD是文件的File Descriptor
number,或者如下的内容:(这里很难翻译对应的意思,保留英文)
引用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);
v86 VP/ix mapped file;
文件的File Descriptor
number显示模式有:
引用r for read access;
w for write access;
u for read and write access;
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.
7、TYPE
引用IPv4 IPv4的包;IPv6使用IPv6格式的包,即使地址是IPv4的,也会显示为IPv6,而映射到IPv6的地址;DIR目录LINK链接文件
详情请看manual中更多的注释。
8、DEVICE使用character
special、block
special表示的设备号9、SIZE文件的大小,如果不能用大小表示的,会留空。使用-s参数控制。10、NODE本地文件的node码,或者协议,如TCP等11、NAME挂载点和文件的全路径(链接会被解析为实际路径),或者连接双方的地址和端口、状态等
常用的参数列表
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,缺省是不加上-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 --> 端口号 (可以不只一个)
常用示例:
1.显示开启文件/home/oracle/10.2.0/db_1/bin/tnslsnr的进程
[root@svr-db-test
~]# lsof /home/oracle/10.2.0/db_1/bin/tnslsnr
COMMAND PID USER FD TYPE
DEVICE SIZE NODE NAME
tnslsnr 3520 oracle
txt REG 253,5 431062 11408866
/home/oracle/10.2.0/db_1/bin/tnslsnr
2.知道22端口现在运行什么程序
[root@svr-db-test
~]# lsof -i :22
COMMAND PID
USER FD TYPE DEVICE SIZE NODE NAME
sshd 3101
root 3u IPv6 8670 TCP *:ssh (LISTEN)
sshd 4545
root 3u IPv6
4237972 TCP 203.aibo.com:ssh->win-avbmq9e8ka7.gdgg.local:nsjtp-ctrl
(ESTABLISHED)
3.显示init进程现在打开的文件
[root@svr-db-test
~]# lsof -c init
COMMAND PID USER FD TYPE
DEVICE SIZE NODE NAME
init 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/init
init 1 root mem REG 253,0 130448 917826
/lib64/ld-2.5.so
init 1 root mem REG 253,0 1678480 917827 /lib64/libc-2.5.so
init 1 root mem REG 253,0 23520 917686 /lib64/libdl-2.5.so
init 1 root mem REG 253,0 247528 917844
/lib64/libsepol.so.1
init 1 root mem REG 253,0 95480 917845 /lib64/libselinux.so.1
init 1 root 10u FIFO 0,16 2311 /dev/initctl
4.看进程号为1的进程打开了哪些文件
[root@svr-db-test
~]# lsof -p 1
COMMAND PID USER FD TYPE
DEVICE SIZE NODE NAME
init 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/init
init 1 root mem REG 253,0 130448 917826
/lib64/ld-2.5.so
init 1 root mem REG 253,0 1678480 917827 /lib64/libc-2.5.so
init 1 root mem REG 253,0 23520 917686 /lib64/libdl-2.5.so
init 1 root mem REG 253,0 247528 917844
/lib64/libsepol.so.1
init 1 root mem REG 253,0 95480 917845 /lib64/libselinux.so.1
init 1 root 10u FIFO 0,16 2311 /dev/initctl
5.显示归属3520的进程情况
[root@svr-db-test
~]# lsof -g 3520
COMMAND PID
PGID USER FD TYPE DEVICE SIZE NODE NAME
tnslsnr 3520 3520 oracle cwd DIR 253,5 4096 11059201 /home/oracle
tnslsnr 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/tnslsnr
tnslsnr 3520 3520 oracle mem REG 253,0 130448 917826
/lib64/ld-2.5.so
tnslsnr 3520 3520 oracle mem REG 253,0 1678480 917827
/lib64/libc-2.5.so
tnslsnr 3520 3520 oracle mem REG 253,0 23520 917686
/lib64/libdl-2.5.so
tnslsnr 3520 3520 oracle mem REG 253,0 615136 917834
/lib64/libm-2.5.so
tnslsnr 3520 3520 oracle mem REG 253,0 141208 917829
/lib64/libpthread-2.5.so
tnslsnr 3520 3520 oracle mem REG 253,0 109824 917839
/lib64/libnsl-2.5.so
tnslsnr 3520 3520 oracle mem REG