查找进程打开的文件

进程可能因其他原因而无法运行,例如防火墙、tcp_wrappers 等访问服务控制或者其他一些错误配置。
使用 ping、telnet 或者 nmap 从远程系统检查是否有其他东西阻塞了请求,或者运行 tcpdump 以查看连接是离开源系统还是到达目标系统。

例子
查看名称以“t”(telnetd ...)和 bash 开头的进程打开了哪些文件。
要查看 init 打开了哪些文件,请使用以下命令:

# lsof -c t
# lsof -c bash
# lsof -c init

例子
要查看名称以“testuser”开头的进程打开哪些文件,但排除所有者是用户“tom”的进程,请使用以下命令:

# lsof -c testuser -u ^tom

如何查找进程名称的打开文件数和按打开文件数排序的进程 pid?

当出现“打开的文件太多”错误时,这会很有趣。
使用以下使用 lsof 命令的脚本:

# lsof | perl -lane '$x{"$F[0]:$F[1]"}++;END { print "$x{$_}\t$_" for sort {$x{$a}<=>$x{$b}} keys %x}'
Linux lsof 命令示例

lsof 命令列出打开的文件、套接字和管道。
我们可以使用 lsof 命令查看哪些文件保持打开状态(例如库或者日志文件)以及守护进程监听的端口。
我们可以使用 lsof 命令搜索打开的文件。
lsof 应该以超级用户 (root) 身份运行以查看所有打开的文件。
当不带任何参数/选项使用时,lsof 会列出当前活动进程的所有打开文件。

# lsof
COMMAND     PID   USER   FD      TYPE             DEVICE    SIZE/OFF       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      150352    4587561 /sbin/init (deleted)
init          1   root  DEL       REG              253,0                4849693 /lib64/libnss_files-2.12.so
..........

理解“lsof”命令的输出:

COMMAND: Command using the file.
PID: PID of the file
USER: Owner of the file
FD: File descriptor. Different flags of File descriptor are as below:
#    :  The number in front of flag(s) is the file descriptor number used by the process to associate with the file
u    :  File open with Read and Write permission
r    :  File open with Read permission
w    :  File open with Write permission
W    :  File open with Write permission and with Write Lock on entire file
mem  :   Memory mapped file, usually for shared library
TYPE: File type. Different flags of File type are as below:
REG - Regular file
DIR - Directory
DEVICE: major, minor number of the device where file resides.
SIZE/OFF: File size
NODE: inode number
NAME: File name

显示监听地址

守护进程可以绑定到全局 0.0.0.0 IPv4 地址,也可以绑定到特定地址,例如 127.0.0.1 (localhost)。
绑定到本地主机地址的守护进程只能从系统本身访问。
使用 lsof 的 -i 和 -nP 选项来显示侦听端口,而无需查找主机名和服务。
例如,下面显示了 Apache httpd 守护进程在 localhost 上的非标准端口 7777 上运行。
其他系统将无法连接到这个 httpd 进程:安全性好,远程连接性差。

例子:

# lsof -i -nP | grep httpd
httpd 8616 apache 16u IPv4 0x0455567fh 0t0 TCP 127.0.0.1:7777 (LISTEN)
httpd 8614 apache 16u IPv4 0x0455567fh 0t0 TCP 127.0.0.1:7777 (LISTEN)
httpd 8623 apache 16u IPv4 0x0455567fh 0t0 TCP 127.0.0.1:7777 (LISTEN)

例子:
相比之下,以下 OpenSSH sshd 进程将接受来自其他系统的连接,因为它绑定到 0.0.0.0 地址,如端口号前面的 * 所示。

# lsof -i -P | grep sshd
sshd 4341 root 3u IPv4 46438 TCP *:22 (LISTEN)

某些应用程序侦听许多不同的端口,例如 Berkeley Internet Name Daemon (BIND) 名为 daemon,版本 9.

例子:

# lsof -i -nP | grep ^named
named 9865 named 5u IPv6 0x03348be0 0t0 UDP *:53
named 9865 named 6u IPv6 0x0566re80 0t0 TCP *:53 (LISTEN)
named 9865 named 7u IPv4 0x03456b10 0t0 UDP 127.0.0.1:53
named 9865 named 8u IPv4 0x01870570 0t0 TCP 127.0.0.1:53 (LISTEN)
named 9865 named 9u IPv4 0x03456a40 0t0 UDP *:49164
named 9865 named 10u IPv6 0x03456970 0t0 UDP *:49165
named 8888 named 11u IPv4 0x0186fd54 0t0 TCP *:953 (LISTEN)
named 8888 named 13u IPv4 0x01387ee0 0t0 UDP 168.1.863.1:67
named 8888 named 14u IPv4 0x099899ce4 0t0 TCP 168.1.863.1:67(LISTEN)

查找挂载点下打开的文件

可以使用 lsof 找到特定目录下的打开文件。
例如,在使用 vi 编辑的目录下打开一个文本文件(不要关闭它保持打开),并在另一个会话中检查该目录下的 lsof 输出。

# cd /test
# vi a.txt

在另一个会话中检查该目录下的 lsof 输出:

# lsof /test
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
bash    2972 root  cwd    DIR  253,0     4096 262145 /test
vi      3012 root  cwd    DIR  253,0     4096 262145 /test

这也可用于卸载繁忙的挂载点。
可以使用 lsof 找到保持挂载点忙碌的进程。

查找有关特定过程的信息

有关特定进程的更多信息,请使用 lsof 的 -p 选项:

# lsof -p 8797

使用最大文件描述符查找进程

可以使用以下命令为 shell 设置文件描述符限制:

# ulimit -n 65536

使用“lsof”命令查找系统上使用文件描述的内容。

# lsof -g | awk '{print }' | sort -u > /tmp/lsof_sort.txt
# for var in `cat /tmp/lsof_sort.txt` 
do 
	echo `echo "$var ---- "`grep -x $var /tmp/lsof.txt | wc -l` 
done

这将列出所有进程以及它们打开的相应文件数。
我们可以选择打开文件数最多的进程并查看它们是什么。

on  it road.com

查找用户打开的进程

要查看用户 myuser 和 tom 打开了哪些进程,请使用以下命令:

# lsof -u myuser,tom

查找进程使用的端口

要查看哪些进程正在使用特定端口,请使用以下命令:

# lsof -i TCP:389
日期:2020-09-17 00:14:28 来源:oir作者:oir