Physical Devices  «Prev  Next»
Lesson 8The fuser command
Objective Mount and unmount a partition.

fuser command

The fuser command in Unix is used to identify which processes are using a particular file, directory or socket. The purpose of the fuser command is to provide information about which processes are accessing a particular resource. This can be useful for troubleshooting and for identifying which processes may be preventing a resource from being modified or deleted. When the fuser command is executed with a file or directory name as an argument, it returns a list of (PIDs) process IDs[1] that are accessing that resource. The command can also be used with various options to display additional information about the processes, such as their user IDs, the names of the processes, and the number of file descriptors in use. Overall, the fuser command is a useful tool for system administrators and users who need to identify which processes are accessing a particular resource in Unix. It can help in troubleshooting issues and can be used to determine which processes may be preventing a resource from being modified or deleted. The fuser command will reveal all processes that have files open on the device and therefore are preventing it from being unmounted. Here is a Linux example of using the fuser command:
/usr/sbin/fuser –v –m /mnt 
USER  PID  ACCESS COMMAND
userA 1011 ..c..   bash
In this example, the –m option refers to the mount point we are interested in. The –v option asks for verbose output. On Solaris, the –c option performs the same function as -m. The output from fuser lists all processes (by PID) that are using the given filesystem, their owners, and an indication of what they are doing:
  1. f means open file.
  2. e means running an executable from that filesystem.
  3. c means have current directory on that filesystem.
In the output above, we see that userA has process 1011 with current directory in the /mnt filesystem. This is why we cannot unmount /mnt. The fuser command can be used to kill all processes accessing a filesystem. For example, on Linux:
/usr/sbin/fuser –k -m /mnt

The –k option kills all processes using the specified file or filesystem. mounting Unmounting using Linux
Solaris Simulation

Mounting and unmounting using Solaris (Simulation transcript)

Here are the steps you followed in this simulation:
  1. You are logged in as a superuser and have inserted the floppy disk into the drive. Type volcheck -v /dev/diskette0 to verify that the floppy device is mounted.
  2. You can unmount the floppy using the eject command. But first, let’s look at what occurs if you attempt to eject a floppy when a directory is active. To test this, change to the /floppy0 directory. Remember that floppy0 is the root directory of the floppy disk. It is located in the /floppy directory. Solution: cd /floppy/floppy0
  3. Try to eject the floppy by typing eject /floppy
  4. You received a "device is busy" message. Type /usr/sbin/fuser -u /floppy/floppy0 to learn exactly why you received this message. The –u option will show any usernames accessing any directories.
  5. You can see that you (root) are currently accessing the /floppy/floppy0 directory. This is why the c indicator is listed. Now that you have tested this, use your floppy disk to back up the files in your /etc directory. First, change to the /etc directory.
  6. Type cp * /floppy/floppy0 to copy all files in the /etc directory to the floppy disk.
  7. Note that you copied only the files in the /etc directory. This is why you received messages that cp is omitting directories. Type ls –l /floppy/floppy0 to list all the files on the floppy disk.
  8. Normally, this list would be quite lengthy, so for the purposes of this simulation, we’re showing you only a few lines. Remember that you are in the /etc directory. Use the eject command to unmount the floppy drive.
In these simulations, you will mount and unmount the floppy disk filesystem you created earlier. The processes are very different across different versions of UNIX. Choose which UNIX version you would like to simulate by clicking either the Linux or the Solaris button.
[1] process ID (PID): A process ID (PID) is a unique number associated with each process on the system. PIDs start with number 1, which is the init process.