
linux fuser命令
Linux have a lot of tools to manage processes and network. But fuser
is one of the most popular tool which combines process and network management in a single command. fuser
can display process according to their directory, file and network handles. Also it can kill these process according to various parameter.
Linux有很多工具来管理进程和网络。 但是, fuser
是最流行的工具之一,它在单个命令中将过程和网络管理结合在一起。 fuser
可以根据其目录,文件和网络句柄显示进程。 也可以根据各种参数杀死这些进程。
句法 (Syntax)
fuser [options] [file|socket]
帮帮我 (Help)
$ fuser -h

使用目录显示进程(Display Processes Using Directory)
In daily usage of a server there will be a lot of users and processes. Finding related process about opened directory can be hard. fuser
command provides feature simply find related processes according to directory usage. In the example we will provide the directory path we want to inspect which is /home/ismail
. Best way to use fuser
is with root privileges
在服务器的日常使用中,会有很多用户和进程。 查找有关打开目录的相关过程可能很困难。 fuser
命令提供的功能只需根据目录使用情况查找相关进程即可。 在示例中,我们将提供要检查的目录路径/home/ismail
。 使用fuser
最佳方法是具有root特权
$ fuser /home/ismail

From output we can see that some process ID are printed. There is also some characters after PIDs. These are used to specify access type and information about the process. Here is what they mean
从输出中,我们可以看到打印了一些进程ID。 PID之后还有一些字符。 这些用于指定访问类型和有关该过程的信息。 这就是他们的意思
c
– current directoryc
–当前目录e
– executeable is runninge
–执行中正在运行f
– open filef
–打开文件F
– open file for writingF
–打开文件进行写入r
– root directoryr
–根目录m
– mmaped file or shared librarym
–映射文件或共享库
显示更多信息,例如用户和命令(Display More Information Like User And Command)
In the previous example we have only listed PID’s. But this information can not be enough some time. We may need more information like the user name and related command. To get user name and related command more verbose output is needed. Verbose output can be displayed with -v
option. In the example we will list information about directory access of processes with user and command information in a column based output.
在前面的示例中,我们仅列出了PID。 但是这些信息在一段时间内还不够。 我们可能需要更多信息,例如用户名和相关命令。 为了获得用户名和相关命令,需要更多详细的输出。 详细输出可以使用-v
选项显示。 在示例中,我们将在基于列的输出中列出有关进程目录访问的信息以及用户和命令信息。
$ fuser -v /home/ismail

In the example out we can see that our looking path is /home/ismail
and process id is 2548
which is the PID of bash
cmomand with user ismail
在示例中,我们可以看到我们的查找路径为/home/ismail
,进程ID为2548
,这是bash
cmom和用户ismail
的PID。
使用文件系统显示进程 (Display Processes Using File System)
Process access information about the file system can be displayed with -m
option. This will create enormous amount of output.
可以使用-m
选项显示有关文件系统的进程访问信息。 这将产生大量的输出。
$ fuser -v -m /home/ismail/.bashrc

关于TCP和UDP套接字的显示过程(Display Processes About TCP and UDP Sockets)
Another useful feature of fuser
command is listing process information of TCP and UDP sockets. This can be very useful if our Apache web server do not starts because of the TCP 80 is used by another process we do not know. We can find this process easily. Furthermore this process can be killed with a single command which we will look below examples. In the example we want to list processes information listening TCP 22. We need root privileges to get information. We will provide -n
option with tcp 22
parameter
fuser
命令的另一个有用功能是列出TCP和UDP套接字的进程信息。 如果我们的Apache Web服务器由于另一个未知进程正在使用TCP 80而无法启动,那么这将非常有用。 我们可以很容易地找到这个过程。 此外,可以使用单个命令终止该过程,我们将在以下示例中查看。 在该示例中,我们希望列出侦听TCP 22的进程信息。我们需要root特权才能获取信息。 我们将为-n
选项提供tcp 22
参数
$ fuser -v -n tcp 22

In order to use with UDP ports look following command.
为了与UDP端口一起使用,请查看以下命令。
$ fuser -v -n udp 53
使用指定的TCP套接字终止进程 (Kill Process Using Specified TCP Socket)
As previously stated we can kill process by specifying TCP or UDP ports or sockets in a single command. We will provide -k
option to kill related process. In the example we will kill all ssh
or TCP port 22 process.
如前所述,我们可以通过在单个命令中指定TCP或UDP端口或套接字来终止进程。 我们将提供-k
选项以杀死相关进程。 在示例中,我们将终止所有ssh
或TCP端口22进程。
$ sudo fuser -k -n tcp 22

We can see that we have killed our current ssh connection to our server.
我们可以看到我们已经终止了当前与服务器的ssh连接。
列出信号(List Signals)
In previous example we have killed the ssh process. Killing a process is done by sending signal to the process. Signals are used to communicate with process. To kill a process kill related signals are sent. There is also different type of signals those may send to the related process. We can list signals list-signals
option like below.
在前面的示例中,我们取消了ssh进程。 通过向进程发送信号来终止进程。 信号用于与过程进行通信。 为了终止进程,发送终止信号。 那些可能会发送到相关进程的信号也有不同类型。 我们可以像下面列出信号list-signals
选项。
$ fuser -list-signals

使用特定信号杀死进程(Kill Process With Specific Signal)
As we stated we can send specific signal to the process we have specified. In the example we will send -HUP
signal to the TCP 22 or ssh port with the following command.
正如我们所说,我们可以向指定的过程发送特定的信号。 在示例中,我们将使用以下命令将-HUP
信号发送到TCP 22或ssh端口。
$ sudo fuser -k -HUP -n tcp 22

交互式杀死进程(Kill Process Interactively)
While killing process in the critical servers we have no room for mistakes. But issuing fuser
command will execute command in real time without any caution. We can prevent mistakes and kill related processes in a more controlled way with interactive option. Interactive option is provided with -i
在关键服务器中终止进程时,我们没有犯错的余地。 但是发出fuser
命令将实时执行命令,而不会引起任何警告。 我们可以通过交互式选项以更可控的方式防止错误并杀死相关进程。 -i
提供了交互式选项
$ sudo fuser -i -k -n tcp 22

翻译自: https://www.poftut.com/linux-fuser-command-tutorial-with-examples/
linux fuser命令