How annoying is it when you can't eject a flash drive because the system says the device is "busy"? [buzzer sound from SNES "Wheel of Fortune" game] Well, I've got a trick for you- the fuser command. [SID chiptune and guitar intro music] [paper ripping sound] Greetings, and welcome back to
another exciting Lil' Linux Lesson! [lo-fi bell music for the Lil' Linux Lesson] I'm Veronica, and today, we're going to talk about the
basic use of the `fuser` command. `fuser` or "fuse-er" as some folks pronounce it,
has been part of the Single UNIX Specification since version 2, with references to it
in the technical standard from 1997. So, there's a pretty good chance it's
going to be present on your Linux box. I use the `fuser` command frequently in
order to diagnose problems with mount points, as well as figure out who might have
left a file open on my COBOL servers. It's a great tool that's already in
your toolbox so let's give it a try. Here on this Debian box, I have
a file open on a flash drive. And as you can see, I can't eject
it cleanly, as the device is busy. Now, obviously here I can see that
the file is open. And as a trained Linux expert with over fifteen years
of system administration experience, I'm fairly certain I can
figure out how to exit vim. But let's say some other process has
this file on the flash drive open, or it's another user on the server, or maybe
it's hidden behind a window or something, and it's not apparent what's holding it up. This is where `fuser` is a great
option for troubleshooting. On this fresh Debian box, the flash drive is
mounted in my `/ media/` folder, inside of my user's subdirectory. On other systems, you might
see it in `/run/media/` or `/mnt/`. Remember, different distros mount different drives
in different locations. And if you don't know, `lsblk` is a great way to find your mount points. Now that we know the mount point
for the drive I'm trying to eject, we can use `fuser` followed by that mount point. It shows me two numbers and the letter "c" next to them. The numbers are the process
IDs holding up the directory, and the letter "c" means the mount point is
the current directory of the file being run. This is good, but to me, it could be more useful.
I'm going to run the command again, but with a `-v` flag to increase the verbosity. Now, with the verbose flag on, we can
see a lot more useful information- the user running the drive, the PID
again, the access type again, and perhaps most importantly, the
command keeping the directory open. For most purposes, this is probably
enough information to figure out what to do next. And here I can see that vim is my culprit. If I exit vim, and run the command again, I can see that the bash window is still up
and in that folder. So let's close it. Running it again shows that nothing
is holding open that directory, and sure enough, I can now
eject the drive cleanly. Sometimes, it won't be quite as obvious
what to do with an errant process and no obvious windows to work with. After
all, Linux isn't perfect- no operating system is. If the system is hung, and you know you just want
to kill the errant process holding up the drive, you can pass fuser the `-k` argument, to kill
whatever PIDs are holding up that file or folder. But "killing all of the processes on a drive"
can be... extreme, so use that with caution. I prefer to see what's holding it
up and troubleshoot each process to see if I can cleanly close them
down without killing them directly. Also, keep in mind that running fuser with sudo
might be necessary in order to see deeper into the system, as well as being able to kill
more tasks than your user has access to. So far, we've worked with a folder as a folder. But sometimes, you need to work with
the mounted filesystem as a whole, especially if we're dealing with block devices. When working with external drives, my
go-to is passing the partition from the `/dev/` folder using the `-m` flag,
or `-c` if your preference is for POSIX. This has a lot of advantages when
working with scripts and servers, so it's a great thing to keep in mind
when using `fuser`. Unlike mountpoints, working through the `/dev/` folder tends to
be more standardized from system to system. Speaking of servers, here's a fun
fact about `fuser`- you can use it to see what processes are using a TCP or UDP port. This is super useful when running
a dev server. Here for instance, I'll use `browser-sync` to serve up
a dev site's folder on port 3000. Now, if I want to see what process
is currently running on that port, I can use `fuser` and the `-n` flag to tell it
we're going to use a namespace. In this case, we'll use the `tcp` namespace, and
then tell it to go look at port `3000`. We can see that `node` is the process using that TCP port, which makes sense since
`browser-sync` is an npm package. This gets super useful when trying to
troubleshoot why you can't open a new service on a port, and can help you
deal with conflicts in a clean way. `fuser` is one of those utilities
you never knew you needed, but once you integrate it into your workflow,
you'll wonder how you lived without it. It's such a simple little tool and
there's a ton you can do with it. A related tool is `lsof`, which is a bit
more complex and varied in its usage, a topic for a future video. One thing I love about Linux is that there's
often so many great ways to complete a task. But that shouldn't surprise any of us
because Linux is awesome! And so are you! Now for a new segment I'm
calling "what I'm watching", where I shout out a video
that I think is really cool. Today's video recommendation is Jay's
recent video on the `lsof` command, part of his Linux Crash Course series. If you aren't already following Jay's channel over
at Learn Linux TV, I suggest you check it out. He's got tons of great resources to
get you started in the world of Linux, and his videos have helped me troubleshoot
complex problems on more than one occasion. Definitely worth checking out. And if you want to learn
more about the fuser command, check out the manual by typing
"man fuser" in your favorite terminal. [child shouting] "1, 2, 3, 4!"
[punk outro music plays] "I've got a trick for you, the few..." [annoyed sigh] "By typing 'man few-zer' (incorrect pronounciation), aaaah!" "It's not 'man few-zer', oh boy." [outro music continues]