linux fuser命令_带有示例Linux fuser命令教程

本文介绍 Linux 下 fuser 命令的使用方法,包括如何利用 fuser 查看与目录、文件及网络句柄相关的进程,以及如何终止这些进程。文章还介绍了 fuser 的多种选项,如 -v 显示更多细节、-m 查看文件系统信息、-n 列出 TCP/UDP 端口等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux fuser命令

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
Help
Help
帮帮我

使用目录显示进程(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
Display Processes Using Directory
Display Processes Using Directory
使用目录显示进程

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 directory

    c –当前目录

  • e – executeable is running

    e –执行中正在运行

  • f – open file

    f –打开文件

  • F – open file for writing

    F –打开文件进行写入

  • r – root directory

    r –根目录

  • m – mmaped file or shared library

    m –映射文件或共享库

显示更多信息,例如用户和命令(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
Display More Information Like User And Command
Display More Information Like User And Command
显示更多信息,例如用户和命令

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
Display Processes Using File System
Display Processes Using File System
使用文件系统显示进程

关于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
Display Processes About TCP and UDP Sockets
Display Processes About TCP and UDP Sockets
关于TCP和UDP套接字的显示过程

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
Kill Process Using Specified TCP Socket
Kill Process Using Specified TCP Socket
使用指定的TCP套接字终止进程

We can see that we have killed our current ssh connection to our server.

我们可以看到我们已经终止了当前与服务器的ssh连接。

LEARN MORE  Linux Mtr Command Tutorial with Examples To Network Diagnostics
了解更多Linux Mtr命令教程,并带有网络诊断示例

列出信号(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
List Signals
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 With Specific Signal
Kill Process With Specific Signal
使用特定信号杀死进程

交互式杀死进程(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
Kill Process Interactively
Kill Process Interactively
交互式杀死进程

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

linux fuser命令

<think>好的,用户想了解如何在Linux上杀死占用8888端口的进程。我需要先确认用户的需求,可能他们在运行某个服务时遇到端口冲突,或者想关闭某个正在监听该端口的应用。首先,我应该分步骤指导他们如何找到占用该端口的进程,然后安全地终止它。 首先,用户需要找出哪个进程在使用8888端口。常用的命令是`netstat`或者`ss`,不过现在更推荐使用`ss`,因为它更快速。另外,`lsof`也是一个不错的选择,但可能需要安装。所以我会先建议使用`ss -tulnp | grep :8888`或者`sudo lsof -i :8888`来查找进程的PID。 接下来,用户需要根据找到的PID来终止进程。这里要提醒用户使用`kill`命令,比如`kill -9 PID`,但也要注意先尝试默认的SIGTERM信号,如果无效再使用SIGKILL。此外,如果用户知道服务名称,比如可能是Jupyter Notebook常用的8888端口,可以建议他们检查相关服务并正确停止,而不是直接kill,避免数据丢失。 还要注意权限问题,可能需要使用`sudo`来执行这些命令。同时,要确保用户不会误杀其他重要进程,所以在查找PID时要确认确实是目标端口对应的进程。最后,验证端口是否释放,可以再次运行`ss`或`netstat`检查。</think>在Linux中终止占用8888端口的进程,可以按照以下步骤操作: --- ### 一、查找占用端口的进程 #### 方法1:使用`ss`命令(推荐) ```bash sudo ss -tulnp | grep ':8888' ``` - `-t` 显示TCP连接 - `-u` 显示UDP连接 - `-l` 仅显示监听状态的连接 - `-n` 直接显示端口号(不转换为服务名称) - `-p` 显示进程信息 输出示例: ``` tcp LISTEN 0 128 *:8888 *:* users:(("python3",pid=1234,fd=3)) ``` #### 方法2:使用`lsof`命令 ```bash sudo lsof -i :8888 ``` 输出示例: ``` COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python3 1234 user 3u IPv4 123456 0t0 TCP *:8888 (LISTEN) ``` --- ### 二、终止进程 #### 1. 通过PID终止 根据上一步获取的PID(例如1234): ```bash sudo kill 1234 # 默认发送SIGTERM信号(正常终止) sudo kill -9 1234 # 强制终止(当SIGTERM无效时) ``` #### 2. 通过端口直接终止 使用`fuser`命令(需安装`psmisc`包): ```bash sudo fuser -k 8888/tcp ``` --- ### 三、验证结果 再次执行检查命令: ```bash sudo ss -tulnp | grep ':8888' # 若无输出则表示已成功释放端口 ``` --- ### 📝 常见场景 - **Jupyter Notebook**:8888是其默认端口,可通过`jupyter notebook stop 8888`停止 - **开发服务器**:如Django/Flask,建议优先使用服务自带的停止命令 - **僵尸进程**:若kill后端口仍被占用,可能需检查进程是否残留或重启系统 --- ### ⚠️ 注意事项 1. `kill -9`是强制终止,可能导致数据丢失 2. 生产环境建议优先通过服务管理命令停止进程(如`systemctl stop service_name`) 3. 确保你有权限操作目标进程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值