Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

fuser Command in Linux



The Linux fuser command identifies the process using a file, socket, or directory. It also provides information about the process owner and type of access.

The fuser command lists the PIDs of the specified file or filesystem. The PIDs are followed by access types denoted by a letter. The fuser returns a non-zero code if the specified file or filesystem does not exist.

Table of Contents

Here is a comprehensive guide to the options available with the fuser command in linux −

Syntax of fuser Command

The syntax of the Linux fuser command is as follows −

fuser [options] [file | directory | socket]

The [options] field is used to specify the options to modify the behavior of the command. The [file | directory | socket] field is used to specify the file, directory, or socket to display the owner, PIDs, and access code.

Options of fuser Command

The options used with the fuser command are listed below −

Options Description
-a (--all) It shows all the files specified regardless of whether they are being accessed by a process or not (by default only those files are shown that are accessed by a process)
-i (--interactive) It asks before killing a process, it ignores -k
-I (--inodes) It always uses inodes to compare files
-k (--kill) It kills the processes accessing the named files
-l (--list-signals) It lists the available signal names
-m (--mount) name It displays the processes using the specified filesystem or block device
-M (--ismountpoint) name It only fulfilled the request if the specified name is a mount point
-n (--namespace) space It is used to select a different namespace (file, udp, or tcp)
-s (--silent) It is used for silent operation
-SIGNAL It sends the signal instead of SIGKILL, the signal can be specified by name or number
-u (--user) It displays the user IDs
-v (--verbose) It provides a detailed output
-w (--writeonly) It only kills the processes with write access
-V (--version) It displays the command version
-4 (--ipv4) It only searches the IPv4 sockets
-6 (--ipv6) It only searches the IPv6 sockets
-h (--help) It is used to display the command help

Understanding fuser Command Access Codes

The default output of the fuser command displays the PID with a letter called access code. The access code indicates the access type of the specified file. The access codes of the fuser command are listed below −

Access Code Description
c This indicates the current working directory
e This indicates that the file is executable
f This indicates that the file is currently opened
F This indicates the file is specifically open for writing
r This indicates the root directory of the file system
m This indicates that the file is mapped to memory or is a shared library loaded in memory

Examples of fuser Command in Linux

The section demonstrates the usage of the fuser command in Linux with examples −

Displaying Processes in a Specific Directory

To display the processes with the access code accessing to a specific directory, use the fuser command with the directory name.

fuser [directory]

For example, to display the processes in the current working directory, use −

fuser .
Displaying Processes in Specific Directory

To display processes of another directory use, simply specify the directory path.

The default output shows only the PID and access codes. To get information on the file owner, and filename, use the -v or --verbose option −

fuser -v .
Display Processes of Another Directory fuser Command

Displaying Processes of a Specific File

To display the process accessing a file, use the fuser command with the path of the file.

fuser [filepath]

For example, to get the process of a script file in the home (~) directory, use the command given below −

fuser -v ~/script.sh
Displaying Processes of Specific File fuser

The access code f indicates that the file is currently open.

Displaying Processes of a Specific Socket

To display the process of a specific socket, mention the socket type and port number. For example, to display the process of TCP socket type on port 53, use the fuser command in the following way −

sudo fuser -v -n tcp 53
Displaying Processes of Specific Socket fuser

The -n option is used to specify the socket type. The F access code shows the file is opened for writing.

Another way of using the above command is as follows −

sudo fuser -v 53/tcp
Another way of using the fuser Command

Displaying Processes Accessing a Filesystem

To identify all the processes accessing a filesystem, use the -m or --mount option. To list the mount points, use the lsblk command. To list the processes accessing the mount point of /dev/vda3, use −

sudo fuser -m /var/snap/firefox/common/host-hunspell
Displaying Processes Accessing Filesystem fuser

Similarly, to list all the processes of the filesystem to which the script.sh file belongs to, use the command in the following way −

sudo fuser -v -m ~/script.sh
List All Processes of fuser filesystem

Killing a Specific Process

To kill the process using the fuser command, use the -k or --kill option −

sudo fuser -v -k ~/script.sh
Killing Specific Process Using fuser Command

To kill a process interactively, use the -i or --interactive option −

sudo fuser -v -ki ~/script.sh
kill Process Interactively fuser Command

Sending a Signal

The -k option sends a SIGKILL signal to kill the process. However, other signals can also be used with the fuser command.

To list all the signals, use the following command −

fuser -l
Sending Signal Using fuser Command

To kill the process using the QUIT signal interactively, use the command given below −

sudo fuser -ki -QUIT 53/tcp
Kill Process Using QUIT signal fuser Command

Conclusion

The fuser command in Linux is a process management utility that is used to display the process associated with a specific directory, file, or mount point. It also shows the state or type of file being accessed through access codes. Moreover, the fuser is used to kill the process as well.

In this tutorial, we explained the fuser command, its syntax, options, and usage in Linux through various examples.

Advertisements