How to use the command `fuser` (with examples)
- Linux
- December 25, 2023
fuser
is a command-line utility that allows you to display process IDs currently using files or sockets. It can be useful when you need to identify which processes are accessing a particular file, directory, or TCP socket, and it also provides the ability to kill these processes if necessary.
Use case 1: Find which processes are accessing a file or directory
Code:
fuser path/to/file_or_directory
Motivation: You may want to determine which processes are currently using a specific file or directory. This information can help you identify potential issues or conflicts.
Explanation:
path/to/file_or_directory
: Specify the path to the file or directory you want to check for process access.
Example output:
path/to/file_or_directory: 1234 5678
In this example, processes with IDs 1234 and 5678 are accessing the path/to/file_or_directory
.
Use case 2: Show more fields (USER
, PID
, ACCESS
and COMMAND
)
Code:
fuser --verbose path/to/file_or_directory
Motivation: By default, fuser
only displays the process IDs that are using the specified file or directory. However, you may want more detailed information about these processes, such as the user, process ID, access type, and command.
Explanation:
--verbose
: This option enables the verbose mode and provides additional information about the processes.
Example output:
USER PID ACCESS COMMAND
path/to/file_or_directory: john 1234 r cat
In this example, the process with ID 1234, owned by the user “john,” is accessing the path/to/file_or_directory
file for reading using the cat
command.
Use case 3: Identify processes using a TCP socket
Code:
fuser --namespace tcp port
Motivation: When troubleshooting network-related issues, it can be helpful to identify which processes are using a specific TCP socket. This information can assist in identifying any conflicts or misconfigurations.
Explanation:
--namespace tcp
: This option specifies the namespace as TCP, indicating that you want to search for processes using TCP sockets.port
: Specify the port number of the TCP socket you want to check.
Example output:
tcp: 1234
In this example, there is a process with the ID 1234 using the specified TCP socket.
Use case 4: Kill all processes accessing a file or directory
Code:
fuser --kill path/to/file_or_directory
Motivation: In certain scenarios, you may need to terminate all processes that are accessing a particular file or directory. This command allows you to send the SIGKILL
signal to these processes, forcibly terminating them.
Explanation:
--kill
: This option tellsfuser
to kill the identified processes.path/to/file_or_directory
: Specify the path to the file or directory for which the accessing processes should be killed.
Example output (no output produced):
In this example, the processes accessing the path/to/file_or_directory
will be terminated without producing any output.
Use case 5: Find which processes are accessing the filesystem containing a specific file or directory
Code:
fuser --mount path/to/file_or_directory
Motivation: Sometimes, you may need to identify all processes that are accessing a specific file or directory along with any other processes accessing the same filesystem. This command provides you with this information.
Explanation:
--mount
: This option allowsfuser
to search for processes accessing the same filesystem that contains the specified file or directory.path/to/file_or_directory
: Specify the path to the file or directory you want to check for process access.
Example output:
path/to/file_or_directory: 1234 5678
In this example, in addition to the processes with IDs 1234 and 5678 accessing the path/to/file_or_directory
, there may be other processes using the same filesystem.
Use case 6: Kill all processes with a TCP connection on a specific port
Code:
fuser --kill port/tcp
Motivation: In some situations, you may need to terminate all processes that have a TCP connection on a specific port. This command allows you to send the SIGKILL
signal to these processes.
Explanation:
--kill
: This option tellsfuser
to kill the identified processes.port/tcp
: Specify the port number and protocol (in this case, TCP) for which the processes should be killed.
Example output (no output produced):
In this example, the processes with TCP connections on the specified port will be terminated without producing any output.
Conclusion:
The fuser
command is a versatile tool for identifying and managing processes that are using files, directories, or TCP sockets. By utilizing the different use cases provided above, you can gain insights into process activity and have the ability to kill these processes when needed.