lsof(list opened files)命令负责列出系统中已经打开的文件,包括普通文件、目录、块特殊文件、字符特殊文件、正在执行的文本引用、库、流或网络文件(例如:网络套接字,NFS文件或UNIX域套接字)。
在 linux 环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。所以,lsof 的功能很强大。一般 root 用户才能执行 lsof 命令,普通用户可以看见/usr/sbin/lsof命令,但是普通用户执行会显示 permission denied
。
yum install lsof
lsof [-?abhKlnNoOPRtUvVX] [+|-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]
参数
-p pid: 输出指定进程打开的文件;
-l: 输出中使用ID代替用户名;
-u userName: 输出指定用户打开的文件;
-c string: 输出 COMMAND 列中包含 string 的项;
-d fd: 输出包含指定描述符的项;
fileName: 输出打开文件 fileName 的所有项;
-i [46] [protocol][@hostname|hostaddr][:service|port]: 输出符合指定条件的项,其中:
46:分别指 IPv4、IPv6;
protocol:指 TCP 或 UDP;
hostname: 网络主机名;
hostaddr: IP 地址;
service: 包含在 /etc/services 中的名称;
port: 端口号,可以是多个;
[fenglepeng@centos]# lsof
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 253,0 236 64 /
systemd 1 root rtd DIR 253,0 236 64 /
systemd 1 root txt REG 253,0 1616360 67485964 /usr/lib/systemd/systemd
systemd 1 root mem REG 253,0 20112 33750391 /usr/lib64/libuuid.so.1.3.0
systemd 1 root mem REG 253,0 265624 33750457 /usr/lib64/libblkid.so.1.1.0
……
其中,每列参数的含义如下:
[fenglepeng@centos]# lsof -p 1
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 253,0 236 64 /
systemd 1 root rtd DIR 253,0 236 64 /
systemd 1 root txt REG 253,0 1616360 67485964 /usr/lib/systemd/systemd
systemd 1 root mem REG 253,0 20112 33750391 /usr/lib64/libuuid.so.1.3.0
systemd 1 root mem REG 253,0 265624 33750457 /usr/lib64/libblkid.so.1.1.0
……
[fenglepeng@centos]# lsof -l
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 0 cwd DIR 253,0 236 64 /
systemd 1 0 rtd DIR 253,0 236 64 /
systemd 1 0 txt REG 253,0 1616360 67485964 /usr/lib/systemd/systemd
systemd 1 0 mem REG 253,0 20112 33750391 /usr/lib64/libuuid.so.1.3.0
systemd 1 0 mem REG 253,0 265624 33750457 /usr/lib64/libblkid.so.1.1.0
……
[fenglepeng@centos]# lsof -u root
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 253,0 236 64 /
systemd 1 root rtd DIR 253,0 236 64 /
systemd 1 root txt REG 253,0 1616360 67485964 /usr/lib/systemd/systemd
systemd 1 root mem REG 253,0 20112 33750391 /usr/lib64/libuuid.so.1.3.0
systemd 1 root mem REG 253,0 265624 33750457 /usr/lib64/libblkid.so.1.1.0
……
[fenglepeng@centos]# lsof -c sshd
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 9237 root cwd DIR 253,0 236 64 /
sshd 9237 root rtd DIR 253,0 236 64 /
sshd 9237 root txt REG 253,0 853040 35966750 /usr/sbin/sshd
sshd 9237 root mem REG 253,0 61560 33617054 /usr/lib64/libnss_files-2.17.so
sshd 9237 root mem REG 253,0 68192 33750408 /usr/lib64/libbz2.so.1.0.6
sshd 9237 root mem REG 253,0 100008 33750485 /usr/lib64/libelf-0.172.so
……
[fenglepeng@centos]# lsof -d 12
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root 12u unix 0xffff9e2ab51d6c00 0t0 25988 /run/systemd/private
systemd-j 4392 root 12u a_inode 0,10 0 7495 [timerfd]
auditd 8686 root 12u a_inode 0,10 0 7495 [eventfd]
dbus-daem 8724 dbus 12u unix 0xffff9e2b33fac000 0t0 44865 /run/dbus/system_bus_socket
avahi-dae 8785 avahi 12u IPv4 54551 0t0 UDP *:mdns
systemd-l 8788 root 12u unix 0xffff9e2bb62cac00 0t0 48301 socket
……
[fenglepeng@centos]# lsof /usr/lib64/ld-2.17.so
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root mem REG 253,0 163312 33554510 /usr/lib64/ld-2.17.so
systemd-j 4392 root mem REG 253,0 163312 33554510 /usr/lib64/ld-2.17.so
lvmetad 4417 root mem REG 253,0 163312 33554510 /usr/lib64/ld-2.17.so
systemd-u 4429 root mem REG 253,0 163312 33554510 /usr/lib64/ld-2.17.so
……
lsof -i 4
输出协议类型为 IPv4 的所有项,如下所示:[fenglepeng@centos]# lsof -i 4
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root 41u IPv4 47143 0t0 TCP *:sunrpc (LISTEN)
systemd 1 root 42u IPv4 47144 0t0 UDP *:sunrpc
rpcbind 8713 rpc 4u IPv4 47143 0t0 TCP *:sunrpc (LISTEN)
sshd 9237 root 3u IPv4 55748 0t0 TCP *:ssh (LISTEN)
……
lsof -i UDP
输出协议类型为 UDP 的所有项,如下所示:[fenglepeng@centos]# lsof -i UDP
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root 42u IPv4 47144 0t0 UDP *:sunrpc
systemd 1 root 45u IPv6 47146 0t0 UDP *:sunrpc
rpcbind 8713 rpc 5u IPv4 47144 0t0 UDP *:sunrpc
rpcbind 8713 rpc 7u IPv6 47146 0t0 UDP *:sunrpc
rpcbind 8713 rpc 10u IPv4 44332 0t0 UDP *:netconf-beep