Linux lsof Explained: Find Which Process Owns a Port or File
Every Linux admin has been here: you try to start a service and it fails with "address already in use". Or your disk is full, but
du shows nothing big. Or a process refuses to die. The answer to all three is the same command: lsof — list open files. It shows you every file, socket and port that every process on your system currently has open. Once you learn it, you will wonder how you debugged anything without it.
The Idea: Everything Is a File
On Linux, almost everything is a file — real files, network sockets, pipes, devices. lsof lists them all, tied to the process that opened them. Run it plain and you get a flood, so the skill is in filtering. Install it first if it is missing: sudo apt install lsof on Debian/Ubuntu, or sudo dnf install lsof on Fedora/RHEL.
Who Is Using That Port?
The most common use. A service failed to start because port 8080 is taken — find the culprit:
sudo lsof -i :8080
The -i flag filters to network sockets, and :8080 picks the port. The output shows the command, the PID, the user, and whether it is listening or connected. Kill it with kill <PID> or restart it properly now that you know what it is. To see every listening port at once, use sudo lsof -i -P -n | grep LISTEN — the -P stops port-name lookups and -n skips DNS lookups, so the output is fast and readable.
Which Files Is a Process Using?
If a process is misbehaving, see exactly what it has open:
lsof -p 1234
Replace 1234 with the real PID. You will see its config files, log files, libraries and sockets. This is how you confirm which config file a daemon actually loaded — not the one you think it loaded, the one it really opened. It is also how you spot a process writing to a log file you did not expect.
The Deleted-File Disk Mystery
Here is the trick that makes lsof legendary. When you delete a file on Linux, the disk space is not freed until every process holding it open closes it. A log file deleted while a service still writes to it keeps eating disk invisibly:
sudo lsof | grep deleted
If you see a huge deleted file held by a process, restart that process (or truncate the file via /proc/<PID>/fd/<FD>) and the space comes back instantly. This one command has saved many admins from a "disk full" crisis at midnight.
Watch One User or One Directory
Narrow the search with these filters:
lsof -u anony # everything opened by user anony
lsof +D /var/log # everything open under /var/log (recursive)
lsof -c nginx # everything opened by processes named nginx
The +D flag is especially handy during incident response: run it on a suspicious directory and you instantly see which processes are touching it. Combined with -u, you can answer "what is this user doing on my server right now?" in one line.
lsof for Security
From a defense point of view, lsof is a quick lie detector. An unknown process listening on a strange port, a web server opening files outside its document root, a user account with dozens of outbound connections — all of these show up in seconds. Run sudo lsof -i on a server you manage and read the list: every line should be something you can explain. The line you cannot explain is the one worth investigating.
lsof turns "something is wrong" into a name, a PID and a file. That is usually all you need to fix it.



Comments
Post a Comment