Identify and Kill a Running Process Using a Specific Port on Windows and Ubuntu


In this blogpost, I will explore how to identify and terminate a process that is using a specific port on a Windows system & Ubuntu. This can be particularly useful when you need to free up a port that is being occupied by an unwanted or unknown application.
Win + X and select "Command Prompt (Admin)" or "Windows PowerShell (Admin)" from the menu. netstat Command:
Type the following command, replacing <port> with the actual port number you are investigating:netstat -aon | findstr :<port>This command will display a list of all processes using the specified port, along with their PID (Process ID).The output will look something like this:TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 1234Here, 1234 is the PID of the process using port 8080 Execute the command below, replacing <PID> with the PID of the process you want to stop:
taskkill /PID <PID> /FThe /F flag forces the process to terminate.
You need to find out what is currently using port 80. Use this command:
sudo lsof -i :80This will show the process ID (PID) of the service using the port.
lsof (list open files): Lists all open files and the corresponding processes. -i (Internet Files): Filters the results to only show files that are connected to the internet, such as network sockets and ports. Once you identify the process, you can decide whether to stop or configure it to use a different port. To stop the process, use:
sudo kill -9 [PID]Replace [PID] with the actual process ID you found.
-9 (SIGKILL): The -9 option in the kill command represents SIGKILL, a signal used to forcefully terminate a process. It sends a signal to the process that cannot be ignored, blocked, or handled by the process itself. If the process is a service you recognize and want to keep running (like another web server), consider reconfiguring it to use a different port.