Part of series: Linux
1. See what ports are in use
Often you need to know if a service is running or what program is occupying a specific port.
1.1 Using ss (Socket Statistics)
The modern command to view sockets is ss.
# -t: TCP, -u: UDP, -l: listening, -n: numeric ports
sudo ss -tuln
1.2 Using lsof (List Open Files)
If you want to know exactly which process is listening on a port (for example, 8080):
sudo lsof -i :8080
This will return something like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 12345 root 20u IPv6 123456 0t0 TCP *:8080 (LISTEN)
Here, the PID (Process ID) is 12345.
2. Process Management
Once you have the PID or the process name, you can control it.
2.1 Search processes with ps and grep
If you know the program name but not its PID:
ps aux | grep nginx
2.2 Interactive monitoring with htop
For a friendlier view, install and use htop:
sudo apt install htop
htop
You can search with F3 and kill processes with F9.
2.3 Kill processes
If you need to stop a process:
By PID (following the previous example):
# Sends SIGTERM signal (polite termination request)
kill 12345
# If it doesn't respond, force close (SIGKILL)
kill -9 12345
By name:
# Kills all processes with this name
killall nginx
With these tools, you can keep your services under control and free up ports when you need to!