How do I find which process is using a port on Linux?
Run sudo ss -tulpn and look for the port in the Local Address column. The last column names the program and its PID. lsof -i :PORT answers the same question for one port. Without sudo ss still lists every listener but names only your own processes.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
Run sudo ss -tulpn and find the port in the Local Address column. The last column shows the program and its process ID. To ask about one port, use sudo lsof -i :8000. Both need sudo when the process belongs to another user, because the kernel only lets you look into your own processes.
List what listens
ss shows sockets. -t is TCP, -u is UDP, -l keeps the sockets that wait for connections, -p adds the process and -n keeps numbers instead of names. This server runs three listeners. The command runs as the user dev, without sudo. ss lists in no fixed order, so sort puts the lines in the order of the address column. You can leave the pipe out:
$ ss -tulpn | sort -k1,1 -k5,5
Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
tcp LISTEN 0 128 [::]:22 [::]:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 5 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=799,fd=3))
tcp LISTEN 0 1 127.0.0.1:9090 0.0.0.0:*
Read the Local Address column from the right. The number after the last colon is the port. The address before it says who can connect: 0.0.0.0 is every IPv4 address of the machine and [::] every IPv6 address, while 127.0.0.1 means only the machine itself. SSH on port 22 is open to everyone. The service on 8000 is reachable only from the machine.
Only port 8000 has a Process column. It is a Python web server with PID 799. The listeners on 22 and 9090 show nothing, and the next section explains why.
Ask about one port
ss takes a filter after the options. sport = :8000 keeps the sockets whose source port is 8000. The quotes keep the filter in one argument, which you need once a filter has ( or > in it:
$ ss -tlnp 'sport = :8000'
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 5 127.0.0.1:8000 0.0.0.0:* users:(("python3",pid=799,fd=3))
lsof gives the same answer from the side of the files. -i TCP:8000 selects the sockets of that port and -sTCP:LISTEN keeps only the listener. -nP skips name lookups. The awk part drops two columns of kernel numbers that change on every run:
$ lsof -nP -iTCP:8000 -sTCP:LISTEN | awk '{print $1, $2, $3, $5, $8, $9, $10}'
COMMAND PID USER TYPE NODE NAME
python3 799 dev IPv4 TCP 127.0.0.1:8000 (LISTEN)
lsof -t prints only the PID, which you can use inside another command. Before you do anything with a process, look at what it is and who started it:
$ ps -o pid,user,args -p $(lsof -t -iTCP:8000 -sTCP:LISTEN)
PID USER COMMAND
799 dev python3 -m http.server 8000 --bind 127.0.0.1
Why you need sudo for other users’ processes
Port 9090 has a listener, and ss shows no process for it:
$ ss -tlnp 'sport = :9090'
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 1 127.0.0.1:9090 0.0.0.0:*
The program belongs to root. ss finds the owner of a socket by looking at the open files in /proc/<pid>/fd, and only the owner of a process and root may read that folder. The socket itself is still listed, because the kernel tells everyone which sockets exist. With sudo the owner shows:
$ sudo ss -tlnp 'sport = :9090'
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 1 127.0.0.1:9090 0.0.0.0:* users:(("metrics-agent",pid=800,fd=3))
The same holds for lsof. Run it with sudo to see processes of other users. If you only need root for this one command, sudo for one command shows how to give just that.
Fix “address already in use”
A program that wants a port asks the kernel with bind. When another socket already listens on that address, bind fails with the error EADDRINUSE, and the message you see is “Address already in use”. Start a second web server on port 8000. Python prints a long traceback, and the last line is the message. The pipe to tail -1 keeps that line:
$ python3 -m http.server 8000 --bind 127.0.0.1 2>&1 | tail -1
OSError: [Errno 98] Address already in use
The steps are always the same. Find the listener with ss or lsof, look at it with ps, and decide. If it is the old copy of your own program, stop it. If it is something else, give your program another port. kill sends the polite signal that lets the program end by itself:
$ kill $(lsof -t -iTCP:8000 -sTCP:LISTEN)
$ ss -tln 'sport = :8000'
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
The list is empty, so the port is free. If a service manager starts the program again at once, stop the service through the manager instead of killing the process. If ss shows no listener and the error stays, connections of the old program may still be closing. A program that sets SO_REUSEADDR can bind again at once. Most server software does, and socket(7) explains the rule.
Mistakes people make
You run ss -tulpn without sudo and read an empty Process column as “nobody”. The listener exists. You are not allowed to see its owner.
You read 127.0.0.1 as open to the world. It is not. Another machine can reach a service on 127.0.0.1 only through something on the same host that forwards to it, such as an SSH tunnel; port forwarding shows how.
You use kill -9 first. It gives the program no chance to close files and sockets. Use plain kill, wait a few seconds, and escalate only when the process stays.
You act on a PID you looked up minutes ago. Process numbers are reused. Look it up again right before you run kill.
When sudo asks for a password, Valakes can answer the prompt from its vault without showing the value. sudo is one of the ten tools it knows by default. Feature: Password auto-fill
Questions
Do I always need sudo for ss -p?
Only to see processes of other users. ss lists every socket either way, but it can name the process only when you may look into that process. Your own programs show without sudo.
What is the difference between 0.0.0.0 and 127.0.0.1 in the Local Address column?
0.0.0.0 means the service listens on every IPv4 address of the machine, and [::] means every IPv6 address. 127.0.0.1 means only the machine itself can connect.
Can I use netstat instead?
netstat belongs to the older net-tools package, which many servers do not install any more. ss is part of iproute2 and gives the same facts, and it is the tool to learn.
References
- ss(8) on Ubuntu 24.04, the options -t, -u, -l, -p and -n and the filter syntax manpages.ubuntu.com
- lsof(8) on Ubuntu 24.04, the options -i, -t and -s manpages.ubuntu.com
- bind(2) at man7.org, the error EADDRINUSE man7.org
- socket(7) at man7.org, SO_REUSEADDR man7.org
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.