Find the Process on a Port
sudo lsof -i :80
Run it with sudo; without root, lsof shows only your own processes. Each line is one open connection or listening socket:
| Column | Meaning |
|---|---|
| COMMAND | Program name. |
| PID | Process ID. |
| USER | Account it runs as. |
| FD | File descriptor number. |
| NAME | Address, port, and state. |
A web server usually shows several lines, one for the main process and one per worker. If the command prints nothing, no process uses the port.
Install lsof
Minimal server images often leave it out:
sudo apt install lsof # Debian and Ubuntu
sudo dnf install lsof # AlmaLinux and Rocky Linux
Show Only Listening Ports
sudo lsof -iTCP -sTCP:LISTEN -P -n
sudo lsof -iTCP:443 -sTCP:LISTEN -P -n
-sTCP:LISTEN hides established connections. -P prints port numbers instead of names such as "https", and -n skips DNS lookups, which makes the command much faster on a busy server.
Network Filters
| Option | Shows |
|---|---|
-i tcp | All TCP sockets. |
-i udp | All UDP sockets. |
-i :22 | Port 22, TCP and UDP. |
-i tcp:3306 | TCP port 3306 only. |
-i4 | IPv4 sockets only. |
Files by User, PID, or Program
sudo lsof -u deploy
sudo lsof -p 12345
sudo lsof -c nginx
sudo lsof -a -u deploy -i
By default, lsof combines several options with OR. -a switches to AND, so the last line shows network connections of the user deploy only. Add -t to print bare PIDs, which is useful in scripts.
What Keeps a Disk Busy
If umount fails with "target is busy", list what is open on that mount:
sudo lsof /mnt/data
sudo fuser -vm /mnt/data
Often it is a shell whose current folder is inside the mount; cd out of it. For a service, stop it with systemctl, then unmount.
Deleted Files That Still Use Space
When a program keeps a deleted file open, the space is not freed until the program closes it. This is why df can show a full disk after you removed a large log:
sudo lsof +L1
The output lists those files with the program and PID. Restart that service, for example sudo systemctl restart nginx, and the space comes back. More on this in how to check disk space in Linux.
ss: the Faster Alternative for Ports
sudo ss -ltnp
sudo ss -ltnp 'sport = :80'
ss is installed on every current distribution. -l lists listening sockets, -t TCP (use -u for UDP), -n numbers, and -p the process. The process name and PID appear in the last column. Other everyday tools are in Linux network commands.
Fix "Address Already in Use"
- Find the process:
sudo lsof -i :8080 -P -n. - If it is a service you know, stop or reconfigure it with systemctl.
- If it is an old copy of your own program, end it with
kill PIDand check again; usekill -9only if it does not exit. - Otherwise, choose another port for the new program.
Before you change the port of SSH itself, read how to change the SSH port. To match a PID to a full command line, see how to list running processes.