fuser Command Practical Examples in Linux

This tutorial explains Linux “fuser” command, options and its usage with examples.

fuser – identify processes using files or sockets.

DESCRIPTION

fuser displays the PIDs of processes using the specified files or file systems.
In the default display mode, each file name is followed by a letter denoting the type of access:

c- current directory.
e- executable being run.
f- open file. f is omitted in default display mode.
F- open file for writing. F is omitted in default display mode.
r- root directory.
m- mmap’ed file or shared library.

fuser returns a non-zero return code if none of the specified files is accessed or in case of a fatal error. If at least one access has been found, fuser returns zero.

SYNOPSIS

advertisement
advertisement

fuser [-a|-s] [-4|-6] [-n space] [-signal] [-kimuv] name …
fuser -l
fuser -V

OPTIONS :

-a
Show all files specified on the command line. By default, only files that are accessed by at least one process are shown.
-k
Kill processes accessing the file. Unless changed with -signal, SIGKILL is sent. An fuser process never kills itself, but may kill other fuser processes. The effective user ID of the process executing fuser is set to its real user ID before attempting to kill.
-i
Ask the user for confirmation before killing a process. This option is silently ignored if -k is not present too.
-l
List all known signal names.
-m
name specifies a file on a mounted file system or a block device that is mounted. All processes accessing files on that file system are listed. If a directory file is specified, it is automatically changed to name/. to use any file system that might be mounted on that directory.
-n space
Select a different name space. The name spaces file (file names, the default), udp (local UDP ports), and tcp (local TCP ports) are supported. For ports, either the port number or the symbolic name can be specified. If there is no ambiguity, the shortcut notation name/space (e.g. name/proto) can be used.
-s
Silent operation. -u and -v are ignored in this mode. -a must not be used with -s.
-signal
Use the specified signal instead of SIGKILL when killing processes. Signals can be specified either by name (e.g. -HUP) or by number (e.g. -1).
-u
Append the user name of the process owner to each PID.
-v
Verbose mode. Processes are shown in a ps-like style. The fields PID, USER and COMMAND are similar to ps. ACCESS shows how the process accesses the file. If the access is by the kernel (e.g. in the case of a mount point, a swap file, etc.), kernel is shown instead of the PID.
-V
Display version information.
-4
Search only for IPv4 sockets. This option must not be used with the -6 option and only has an effect with the tcp and udp namespaces.
-6
Search only for IPv6 sockets. This option must not be used with the -4 option and only has an effect with the tcp and udp namespaces.

Reset all options and set the signal back to SIGKILL.

EXAMPLES

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

1. Display every process ID that is using the current directory (./)

$ fuser .

2. For verbose output use -v

$ fuser -v .
                     USER   PID ACCESS COMMAND
.:                   abc   1562 ..c.. gnome-session
                     abc   1620 ..c.. metacity
                     abc   1626 ..c.. polkit-gnome-au
                     abc   1627 ..c.. nm-applet
                     abc   1629 ..c.. gnome-power-man
                     abc   1630 ..c.. mintUpdate
                     abc   1631 ..c.. nautilus
                     abc   1632 ..c.. gnome-panel
                     abc   1633 ..c.. bluetooth-apple
                     abc   1655 ..c.. python
                     abc   1697 ..c.. syndaemon
                     abc   1777 ..c.. python
                     abc   1824 ..c.. gnome-terminal
                     abc   1852 ..c.. firefox
                     abc   1886 ..c.. plugin-containe
                     abc   2380 ..c.. bash
                     abc   2464 ..c.. vlc

3. Check processes using an executable

advertisement

I opted for running fuser on firefox executable which is running on my system :

$ ps -aef | grep firefox
himanshu  1852     1  7 06:57 ?        00:06:06 /usr/lib/firefox-11.0/firefox
himanshu  1886  1852 14 06:58 ?        00:12:10 /usr/lib/firefox-11.0/plugin-container /usr/lib/flashplugin-installer/libflashplayer.so -greomni /usr/lib/firefox-11.0/omni.ja 1852 true plugin
himanshu  2551  2380  0 08:24 pts/1    00:00:00 grep --colour=auto firefox

So the first entry in the output above gives the path to firefox executable running in the system. I’ll use the same path in the following fuser command

$ fuser /usr/lib/firefox-11.0/firefox
/usr/lib/firefox-11.0/firefox:  1852e

So we see that the output is the PID of the process (which can be cross checked against the output of the ‘ps’ command above). The post fix ‘e’ signifies that the file is an executable.

advertisement

4. To output process owner use -u

An extra information regrading the user name of the process owner can also be appended to each PID.

$ fuser -uv .
                     USER   PID ACCESS COMMAND
.:                   abc   2380 ..c.. (abc)bash
                     abc   2855 ..c.. (abc)gcalctool

5. Display information about all files on command line

$ fuser  ./Desktop/ ./Downloads/
./Desktop/:           1892c  1894c

If we wish to have information on all the files we supplied as input then -a option can be used.

$ fuser -a ./Desktop/ ./Downloads/
./Desktop/:           1892c  1894c
./Downloads/:

6. Check Processes Using TCP/UDP Sockets

socket_serv is a C program executable which is a TCP server that listens on a particular port(say 5000 in this case).

$ ./socket_serv

Since the above stated socket_serv sample C program executable is running on TCP port 5000,

$ fuser -v -n tcp 5000
                     USER   PID   ACCESS COMMAND
5000/tcp:            abc   4311   F....  socket_serv

7. Kill Processes that are Using a particular Program

We saw that a TCP server is running on the system which is accessing the binary file ‘socket_serv’. Now, lets try to kill the process using this binary file using fuser.

$ fuser -v -k socket_serv
                     USER   PID ACCESS COMMAND
socket_serv:         abc   4311 ...e.   socket_serv

Notice that we have used the ‘-k’ flag to kill the process using file ‘socket_serv’. Lets see on the other terminal where the server was running.

$ ./socket_serv
Killed

Note : In the above example we saw that the flag ‘-k’ is used when we want to kill the processes using a particular file but to avoid the killing of processes accidentally, another option ‘-i’ exists. If we use this option then ‘fuser’ will run in interactive mode and will ask before killing the process.

8. List all signals supported by fuser

$ fuser -l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED

9. Sending specific signal to a process

The following sends SIGHUP instead of SIGKILL, when you use the -k option.

$ fuser -v  -k -HUP -i ./

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.