fuser command

 

๐Ÿง  Command: fuser (File/Filesystem/Port User Identifier)


๐Ÿ” Purpose

The fuser command shows which processes are currently using a specific file, directory, or socket.

It is often used to:

  • Identify which process is using a file, mount point, or device

  • Detect which process is listening on a network port

  • Terminate processes that are using a specific resource


๐Ÿงฉ Basic Syntax

fuser [options] name

Where:

  • name → can be a file, directory, mount point, or network port


๐Ÿ“˜ Example 1: Identify Process Using a File

fuser test.txt

Output:

test.txt: 3210

๐Ÿงฉ Explanation

  • 3210 → Process ID (PID) of the process using the file test.txt

You can then check what that process is:

ps -p 3210

๐Ÿ“˜ Example 2: Show Processes Using a Directory

fuser /home/user/

Output:

/home/user/: 1203c 2145e 3189r

๐Ÿงฉ Explanation of Suffixes (Access Types):

SymbolMeaning
c    Current directory
e    Executable being run
f    Open file
r    Root directory
m    Memory-mapped file or library

So in the example above:

  • PID 1203 → has /home/user as current directory

  • PID 2145 → executing something from /home/user

  • PID 3189 → has /home/user as root directory


๐Ÿ“˜ Example 3: Show Processes Using a Mount Point

If you have an external drive or partition mounted (say /mnt/usb):

sudo fuser -vm /mnt/usb

Output:

USER PID ACCESS COMMAND /mnt/usb: user1 421 ..c.. bash user1 578 ..f.. cat

๐Ÿงฉ Explanation:

  • The -v option gives a verbose table showing username, PID, access type, and command name.

  • Very useful before unmounting a drive — you can see which process is using it.


๐Ÿ“˜ Example 4: Show Which Process Uses a TCP Port

sudo fuser 80/tcp

Output:

80/tcp: 1325

๐Ÿงฉ Means process ID 1325 is using TCP port 80 — typically a web server like Apache or Nginx.

Check which process that is:

ps -p 1325 -o comm=

Output:

apache2

๐Ÿ“˜ Example 5: Show Processes Using a UDP Port

sudo fuser 53/udp

Output:

53/udp: 580

๐Ÿงฉ This usually corresponds to the DNS server process (named or systemd-resolved).


๐Ÿ“˜ Example 6: Kill Processes Using a Resource

You can use -k to terminate all processes using a file or port.

Example — free up port 8080:

sudo fuser -k 8080/tcp

๐Ÿงฉ This will send a SIGKILL signal (kill -9) to processes using that port.

You can specify a different signal using -signal:

sudo fuser -k -TERM 8080/tcp

๐Ÿ“˜ Example 7: Display Usernames Along with PIDs

fuser -u test.txt

Output:

test.txt: 3210(user1)

๐Ÿงฉ Useful when multiple users are logged in and you want to see who is accessing a resource.


๐Ÿ“˜ Example 8: Combine with lsof

While fuser shows process IDs using a resource, you can use lsof for more details.

Example:

fuser /dev/sda1 lsof /dev/sda1

This gives both PID and full command details — a good comparative experiment for students.


๐Ÿงพ Common Options Summary

OptionDescription
-v    Verbose output (shows user, PID, access type, and command)
-u    Show username of process owner
-k    Kill processes using the resource
-i    Ask for confirmation before killing
-n <space>Specify namespace (file, tcp, udp)
-a    Show all specified files, even if unused
-m    Name is a mounted file system
-s    Silent mode (no messages)

๐Ÿงช Simple Lab Exercise

Aim:

To study and use the fuser command to identify processes using files and ports.


Procedure:

  1. Open a terminal and run:

    cat > test.txt

    (Keep this terminal open — the file is now being used.)

  2. In another terminal, check:

    fuser test.txt
  3. Observe the PID.

  4. Use:

    fuser -v test.txt

    to see user and access type.

  5. Find which process is using a network port:

    sudo fuser 22/tcp
  6. Kill a process using a port (be cautious):

    sudo fuser -k 8080/tcp

Sample Observation Table

CommandDescriptionSample Output
fuser test.txt    Show PID using file    test.txt: 2145
fuser -v test.txt    Verbose info    USER PID ACCESS COMMAND
sudo fuser 22/tcp    Show PID using port 22    22/tcp: 1034
sudo fuser -k 8080/tcp    Kill process on port 8080        (process terminated)

๐Ÿง  Learning Outcomes

Students will:

  • Learn how to identify active processes using a file, directory, or port

  • Understand resource locking and file usage conflicts

  • Gain experience in system administration and process control

  • Understand how to free a busy mount point or port


๐Ÿงพ Quick Reference

CommandPurpose
fuser filename        Show process IDs using a file
fuser -v dir/        Show detailed process info
sudo fuser 80/tcp        Show process using TCP port 80
sudo fuser -k 80/tcp        Kill process using port 80
fuser -u file        Show usernames
fuser -m /mnt/usb        Show processes using a mount point

Comments