本文主要介绍:Linux “lsof” 命令;
公众号: 滑翔的纸飞机
lsof 命令是 "List Open Files(列出打开的文件)"的缩写,用于显示打开的文件以及使用这些文件的进程。
在 Linux 中,一切都以文件的形式存在。有时,特别是为了排除故障,我们需要知道当前哪些进程使用了哪些文件,还可以查看系统中某些网络连接打开的文件等。
root@dev:~# lsof | more
COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 252,1 4096 2 /
systemd 1 root rtd DIR 252,1 4096 2 /
systemd 1 root txt REG 252,1 1620224 73069 /usr/lib/systemd/systemd
systemd 1 root mem REG 252,1 1369384 4750 /usr/lib/x86_64-linux-gnu/libm-2.31.so
systemd 1 root mem REG 252,1 178528 2927 /usr/lib/x86_64-linux-gnu/libudev.so.1.6.17
systemd 1 root mem REG 252,1 1575112 4547 /usr/lib/x86_64-linux-gnu/libunistring.so.2.1.0
systemd 1 root mem REG 252,1 137584 3750 /usr/lib/x86_64-linux-gnu/libgpg-error.so.0.28.0
systemd 1 root mem REG 252,1 67912 4571 /usr/lib/x86_64-linux-gnu/libjson-c.so.4.0.0
接下去,让我们来看看 lsof 命令的一些重要选项。
按用户名过滤打开的文件:
root@dev:~# lsof -u root
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 23028 bolean cwd DIR 252,1 4096 2 /
systemd 23028 bolean rtd DIR 252,1 4096 2 /
systemd 23028 bolean txt REG 252,1 1620224 73069 /usr/lib/systemd/systemd
systemd 23028 bolean mem REG 252,1 1369384 4750 /usr/lib/x86_64-linux-gnu/libm-2.31.so
systemd 23028 bolean mem REG 252,1 178528 2927 /usr/lib/x86_64-linux-gnu/libudev.so.1.6.17
systemd 23028 bolean mem REG 252,1 1575112 4547 /usr/lib/x86_64-linux-gnu/libunistring.so.2.1.0
systemd 23028 bolean mem REG 252,1 137584 3750 /usr/lib/x86_64-linux-gnu/libgpg-error.so.0.28.0
systemd 23028 bolean mem REG 252,1 67912 4571 /usr/lib/x86_64-linux-gnu/libjson-c.so.4.0.0
systemd 23028 bolean mem REG 252,1 34872 3489 /usr/lib/x86_64-linux-gnu/libargon2.so.1
systemd 23028 bolean mem REG 252,1 431472 7146 /usr/lib/x86_64-linux-gnu/libdevmapper.so.1.02.1
除特定用户名外过滤打开的文件(反向):
root@dev:~# lsof -u ^root
COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 252,1 4096 2 /
systemd 1 root rtd DIR 252,1 4096 2 /
systemd 1 root txt REG 252,1 1620224 73069 /usr/lib/systemd/systemd
systemd 1 root mem REG 252,1 1369384 4750 /usr/lib/x86_64-linux-gnu/libm-2.31.so
systemd 1 root mem REG 252,1 178528 2927 /usr/lib/x86_64-linux-gnu/libudev.so.1.6.17
systemd 1 root mem REG 252,1 1575112 4547 /usr/lib/x86_64-linux-gnu/libunistring.so.2.1.0
systemd 1 root mem REG 252,1 137584 3750 /usr/lib/x86_64-linux-gnu/libgpg-error.so.0.28.0
systemd 1 root mem REG 252,1 67912 4571 /usr/lib/x86_64-linux-gnu/libjson-c.so.4.0.0
按特定进程过滤打开的文件:
root@dev:~# lsof -c containerd
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
container 713 root cwd DIR 252,1 4096 2 /
container 713 root rtd DIR 252,1 4096 2 /
container 713 root txt REG 252,1 52007976 3761 /usr/bin/containerd
container 713 root mem-W REG 252,1 2097152 516163 /var/lib/containerd/io.containerd.metadata.v1.bolt/meta.db
container 713 root mem REG 252,1 2029592 4745 /usr/lib/x86_64-linux-gnu/libc-2.31.so
container 713 root mem REG 252,1 157224 4761 /usr/lib/x86_64-linux-gnu/libpthread-2.31.so
根据进程 ID 过滤打开的文件:
root@dev:~# lsof -p 713
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
container 713 root cwd DIR 252,1 4096 2 /
container 713 root rtd DIR 252,1 4096 2 /
container 713 root txt REG 252,1 52007976 3761 /usr/bin/containerd
container 713 root mem-W REG 252,1 2097152 516163 /var/lib/containerd/io.containerd.metadata.v1.bolt/meta.db
按目录过滤打开的文件:
root@dev:~# lsof +D /usr/bin
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dbus-daem 688 messagebus txt REG 252,1 249032 1680 /usr/bin/dbus-daemon
networkd- 695 root txt REG 252,1 5494584 2969 /usr/bin/python3.8
container 713 root txt REG 252,1 52007976 3761 /usr/bin/containerd
按网络连接过滤打开的文件:
root@dev:~# lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-n 630 systemd-network 17u IPv4 29112968 0t0 UDP redis:bootpc
systemd-r 634 systemd-resolve 12u IPv4 20104 0t0 UDP localhost:domain
systemd-r 634 systemd-resolve 13u IPv4 20105 0t0 TCP localhost:domain (LISTEN)
sshd 756 root 3u IPv4 22042 0t0 TCP *:ssh (LISTEN)
按端口过滤打开的文件:
root@dev:~# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 756 root 3u IPv4 22042 0t0 TCP *:ssh (LISTEN)
sshd 756 root 4u IPv6 23092 0t0 TCP *:ssh (LISTEN)
sshd 1251251 root 4u IPv4 30406337 0t0 TCP redis:ssh->10.20.0.6:52875 (ESTABLISHED)
sshd 1277315 root 4u IPv4 30561119 0t0 TCP redis:ssh->10.20.0.6:53199 (ESTABLISHED)
按协议过滤打开的文件:
root@dev:~# lsof -i TCP
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-r 634 systemd-resolve 13u IPv4 20105 0t0 TCP localhost:domain (LISTEN)
sshd 756 root 3u IPv4 22042 0t0 TCP *:ssh (LISTEN)
sshd 756 root 4u IPv6 23092 0t0 TCP *:ssh (LISTEN)