LinuxCommandLibrary

fuser

Identify processes using files or sockets

TLDR

Find which processes are accessing a file or directory

$ fuser [path/to/file_or_directory]
copy

Show more fields (USER, PID, ACCESS and COMMAND)
$ fuser [[-v|--verbose]] [path/to/file_or_directory]
copy

Identify processes using a TCP socket
$ fuser [[-n|--namespace]] tcp [port]
copy

Kill all processes accessing a file or directory (sends the SIGKILL signal)
$ fuser [[-k|--kill]] [path/to/file_or_directory]
copy

Find which processes are accessing the filesystem containing a specific file or directory
$ fuser [[-m|--mount]] [path/to/file_or_directory]
copy

Kill all processes with a TCP connection on a specific port
$ fuser [[-k|--kill]] [port]/tcp
copy

SYNOPSIS

fuser [OPTIONS] FILES...
fuser -l | -V

PARAMETERS

-a, --all
    Show all files specified on the command line, even if not used by any process. By default, only files in use are shown.

-c, --mounts
    Operate on the mount point. All processes using any file on the specified mount point (filesystem) are listed.

-k, --kill
    Kill the processes identified by fuser. A specific signal can be sent with -SIGNAL (e.g., -9 for SIGKILL).

-i, --interactive
    Used with -k, it prompts for confirmation before killing each process.

-m, --mount
    Same as -c, operates on the mount point. This is particularly useful for identifying processes preventing a filesystem from being unmounted.

-n, --no-fork
    When using -k, fuser does not fork for each process killed. This can be faster for many processes but may have implications for signal handling.

-s, --silent
    Silent operation. No output is printed to standard output. Only the exit code indicates success or failure.

-u, --user
    Append the username of the process owner to the PID in the output.

-v, --verbose
    Verbose output. Provides more detailed information about processes and files.

-4, --ipv4
    Only match IPv4 sockets. Useful when querying network connections.

-6, --ipv6
    Only match IPv6 sockets. Useful when querying network connections.

-l, --list-signals
    List all known signal names that can be sent with the -k option.

-V, --version
    Display version information and exit.

DESCRIPTION

fuser is a command-line utility used to identify processes that are currently using specific files, directories, or network sockets. It displays the Process IDs (PIDs) of these processes. This tool is invaluable for system administrators and users alike, especially when attempting to unmount a filesystem that is reported as "busy" or when diagnosing why a file cannot be deleted.

Beyond just listing PIDs, fuser can also provide additional information such as the user owning the process (with -u) and even send a signal to (or kill) the identified processes (with -k), making it a powerful utility for managing system resources and troubleshooting. It supports various file types, including regular files, block devices, mount points, and network sockets (IPv4/IPv6).

CAVEATS

fuser's ability to identify processes can be limited by insufficient user permissions. To see all processes, it often needs to be run as root or with sudo.
Not all types of filesystems or kernel objects are fully supported, though common ones are.
The exact output format can sometimes vary slightly between versions or Linux distributions, which might affect shell scripting that parses fuser's output. For robust scripting, lsof is often preferred for its more consistent output, though it can be slower.

UNMOUNTING BUSY FILESYSTEMS

One of the most common uses of fuser is to identify and terminate processes preventing a filesystem from being unmounted. For example, fuser -m /mnt/mydata will list all processes using files on /mnt/mydata. To kill these processes, you can use fuser -km /mnt/mydata.

NETWORK SOCKET USAGE

fuser can also identify processes using network sockets. For instance, fuser -n tcp 80 will show processes listening on or connected to TCP port 80. You can specify a protocol (tcp, udp) and a port number, or even specific local/remote IP/port combinations like tcp:8080 or udp:53@192.168.1.1.

HISTORY

The fuser command has been a part of Unix-like operating systems for many years, serving as a fundamental utility for system administration. It was originally developed to address the common problem of identifying processes that hold open files or resources, particularly when trying to unmount a filesystem.

On Linux, it's typically part of the psmisc package, which provides various utilities for process management. Its design emphasizes speed and simplicity for its primary function, often making it a quicker alternative to more comprehensive tools like lsof when the goal is simply to find PIDs associated with a file or device.

SEE ALSO

lsof(8), ps(1), kill(1), umount(8)

Copied to clipboard