Introduction to lsof

Today I’m going to introduce you to the exciting world of lsof.

As you will see, it’s a nifty little tool that has various uses, and it has so many switches that you need both a and a + to use all the options available! As the description of the command says in its man-page:

lsof – list open files

So, let’s get started.

Network

List network connections

List TCP/UDP connections

Simply replace TCP with UDP to see UDP connections.

List connections related to a port

List connections related to a host

..and port:

It’s also possible to provide a port-range:

List connections with status LISTEN

Replace LISTEN with ESTABLISHED, and various other options, to grab what you’re looking for.

List IPv4/IPv6 connections

Users, processes and files

lsof not only is useful for network-related tasks, but works excellent for users, processes and files.

List open files of user

List processes by PID

List processes by name

List connections to a file

HUP a process

Kill all of a user’s processes

List all open files without a link

These files have been deleted, but one or several processes are keeping the files open. Though it might appear that the files are deleted from the filesystem, both the files and the blocks are being preserved.

These are just a few of the things you can do with lsof. I hope this tutorial has made you a little bit smarter!

Leave a Reply

Your email address will not be published. Required fields are marked *