The lsof command
The lsof command lists open files, and because network connections count as files…
The lsof command lists open files, and because network connections count as files on Linux, sudo lsof -i :80 shows which program is using port 80.
Taught in Ninja, lesson 53
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
lsof [-i [:PORT]] [-p PID] [-u USER] [-t] [+L1] [FILE]
List open files and network ports. Shows which processes have which files and network ports open. lsof -i :8080 answers "what is using port 8080?", and lsof -t prints just the PIDs, so kill $(lsof -t -i :8080) stops it. lsof +L1 finds files that were deleted while a process still has them open: their disk space isn’t freed until that process lets go (restart it). Without sudo, you only see your own processes.
In the practice terminal:It lists the open files and ports of the practice machines’ pretend processes, inside the page.
How to use lsof
What is using a port: lsof -i
Other users’ programs show only with sudo.
ssh webThe authenticity of host 'web (10.0.0.10)' can't be established. ED25519 key fingerprint is SHA256:aaVFThjlH4/WIvuW5JbeDsUSex1ZSOHtUS3pZXDPB10. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'web' (ED25519) to the list of known hosts. learner@web's password: Welcome to Ubuntu 24.04 LTS (practice web server web) This is a pretend machine inside your browser. Type exit to go back to sandbox. Last login: Mon Jan 12 06:39:27 2026 from 10.0.0.5
sudo lsof -i :80[sudo] password for learner: COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 812 root 3u IPv4 20812 0t0 TCP *:http (LISTEN) nginx 813 www-data 4u IPv4 20813 0t0 TCP *:http (LISTEN) nginx 814 www-data 5u IPv4 20814 0t0 TCP *:http (LISTEN)
A program you started
python3 -m http.server 8000 & sleep 1; lsof -i :8000[1] 5000 Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python3 5000 learner 3u IPv4 25000 0t0 TCP *:8000 (LISTEN)
Just the PIDs: lsof -t
Useful with kill: kill $(lsof -t -i :8000) stops whatever holds the port.
python3 -m http.server 8000 & sleep 1; kill $(lsof -t -i :8000)[1] 5000 Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... [1]+ Terminated python3 -m http.server 8000
Options
| -i [:PORT] | network sockets, or the ones on this port (-i tcp:22 too) |
|---|---|
| -p PID | files this process has open |
| -u USER | files this user’s processes have open |
| -t | just the process IDs |
| +L1 | open files that have been deleted |
| -n, -P | show numbers, not host and port names |
More examples
python3 -m http.server 8000 &[1] 5000 Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
lsof -i :8000COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python3 5000 learner 3u IPv4 25000 0t0 TCP *:8000 (LISTEN)
sudo lsof +L1[sudo] password for learner:
Questions about lsof
Why is my disk still full after I deleted a big file?
A program still has the file open, so its space is freed only when the program closes it. sudo lsof +L1 lists deleted files that are still open; restart that program, often a service, to free the space.
What is the difference between lsof -i and ss?
Both show network connections and listening ports. ss is faster and only does sockets; lsof shows every kind of open file, so it can also say which program holds a file or folder.
How do I see which program has a file open?
Give lsof the path, as in sudo lsof /var/log/syslog. With sudo it shows every user’s programs.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.