# How do I find which process is using a port on Linux?

Find which process listens on a port with ss -tulpn and lsof -i, see why you need sudo to see other users' processes, and fix "address already in use".

Source: https://valakes.com/learn/process-on-port/

Last updated: 2026-11-25

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.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 25 November 2026.

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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/ssh-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`.

## In Valakes

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](https://valakes.com/features/password-auto-fill/)

[Read the docs: Credentials and auto-fill](https://docs.valakes.com/credentials/)

## 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](https://manpages.ubuntu.com/manpages/noble/man8/ss.8.html)
- [lsof(8) on Ubuntu 24.04, the options -i, -t and -s](https://manpages.ubuntu.com/manpages/noble/man8/lsof.8.html)
- [bind(2) at man7.org, the error EADDRINUSE](https://man7.org/linux/man-pages/man2/bind.2.html)
- [socket(7) at man7.org, SO_REUSEADDR](https://man7.org/linux/man-pages/man7/socket.7.html)

## Related articles

- [How do I check disk space on a Linux server?](https://valakes.com/learn/check-disk-space/): Run df -h to see how full each file system is, then run du -h --max-depth=1 on the full one to find the folder that uses the space. df reads the file system itself and du adds up the files it can reach, so the two can disagree.
- [How do I check memory and CPU usage on a Linux server?](https://valakes.com/learn/memory-and-cpu-usage/): Run free -h and read the available column. It is the memory that new programs can still get without swapping. Then run ps -eo pid,user,rss,comm --sort=-rss to see which process holds the most, and sort by -pcpu to see which one burns the CPU.
- [How do I read logs on a Linux server?](https://valakes.com/learn/read-server-logs/): Use tail -f to watch a log file live, grep -C 3 to see a match with the lines around it, and zgrep for the rotated copies that are compressed. For a service that logs to systemd, run journalctl -u name --since "1 hour ago". You need sudo or the right group for the journal.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
