This post was originally published on go2linux.org. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.
You are done copying files to a USB stick, you try to unmount it, and Linux refuses:
$ sudo umount /media/usb
umount: /media/usb: target is busy.
Older versions said device is busy. Either way it means that some process still has a file open on that file system, or has its current directory inside it. The kernel will not unmount it while that is true, and that protects your data: pulling the stick now could leave half-written files.
The question is which process. Two tools answer it: fuser and lsof.
fuser: the quick answer
fuser is part of the psmisc package and is installed on most distributions.
$ fuser -vm /media/usb
USER PID ACCESS COMMAND
/media/usb: root kernel mount /media/usb
ggarron 5581 ..c.. bash
ggarron 6991 F.... evince
-mtreats the argument as a mount point and reports every process using any file on that file system, not only the directory itself.-vgives the verbose table with user and command name.
The ACCESS column tells you how each process is using it:
c— current directory. Here a shell I left open aftercd /media/usb.F— open file being written. Here a PDF viewer with a document from the stick.f— open file (read).e— running executable.r— root directory (a chroot).m— memory-mapped file or shared library.
The kernel mount line is the mount itself and is always there; ignore it.
lsof: more detail
lsof (list open files) shows the same thing with file names, which helps when the process is something you do not recognize:
$ lsof /media/usb
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 5581 ggarron cwd DIR 8,49 32768 1 /media/usb
evince 6991 ggarron 15r REG 8,49 1496960 323 /media/usb/brochure.pdf
When the argument is a mount point, lsof lists every open file on that file system. If you only care about one directory tree that is not a mount point, use +D, which walks the tree recursively:
lsof +D /media/usb/photos
+D can be very slow on a big tree because it checks every file. On a mount point, plain lsof /mount/point is faster and gives the same result.
Run both commands with sudo if the busy process might belong to another user. Without it you will only see your own processes, and the list may look empty while the mount is still busy.
Free it
Once you know who is holding it, deal with it politely first:
- A shell in that directory:
cdout of it. - An open document or file manager window: close it.
- A program writing to it (a copy, a backup, a torrent): let it finish or stop it cleanly.
If the process is stuck, fuser can send it a signal. -k sends SIGKILL to every process using the mount, and -i asks before each one:
sudo fuser -kim /media/usb
Be careful with -k and -m together: on a mount point like /home it will kill your whole desktop session. Check the list from fuser -vm first.
Then unmount normally:
sudo umount /media/usb
When nothing shows up
Sometimes fuser and lsof list nothing and the mount is still busy. The usual reasons:
- Another file system is mounted inside it. You cannot unmount
/mnt/diskwhile/mnt/disk/backupis also mounted.findmnt -R /mnt/diskshows the tree; unmount the inner one first, or useumount -R /mnt/diskto do it recursively. - A swap file lives on it. Check with
swapon --showand turn it off withsudo swapoff /media/usb/swapfile. - A loop device is backed by a file on it, for example a mounted ISO image.
losetup -alists them. - The kernel itself is using it, for example an NFS export, which does not show up as a user process.
- You ran the command as a normal user and the process belongs to root. Run with
sudo.
Lazy unmount: umount -l
sudo umount -l /media/usb
A lazy unmount detaches the file system from the directory tree immediately, so new processes cannot reach it, and finishes the unmount when the last user lets go. The command returns at once and the busy error disappears.
It is useful for a dead network mount (an NFS or sshfs server that went away). Do not use it to pull a USB stick or disk. The processes that were writing to it are still writing, and the device is not really free until they stop. You no longer have an easy way to see when that happens. Find the process and close it instead.
There is also umount -f (force), which is meant for unreachable NFS servers. On local disks it does nothing useful.
On the desktop
Graphical file managers unmount through udisks. The command-line equivalent, which also lets you power the drive off safely, is:
udisksctl unmount -b /dev/sdb1
udisksctl power-off -b /dev/sdb
If the file manager says the device is busy, the cause is the same and so is the fix: fuser -vm on the mount point.
See also
man fuser, man lsof and man umount. lsof is also the tool to find which program is listening on a port.