When working with Linux, you might encounter situations where a specific port is occupied by a process, preventing you from using that port for other applications. Knowing how to kill a process running on a specific port can be very useful, especially for troubleshooting and managing your system effectively.

This step-by-step guide is designed to help beginners understand how to identify and terminate these processes with simple, clear instructions. We’ll walk you through the process of finding the port number, identifying the process using that port, and safely stopping the process. By the end of this guide, you’ll have the skills to manage ports on your Linux system confidently.

Step-by-Step Guide: How to Kill a Process on a Specific Port kill Linux Commands lsof pid process
Linux: Kill Process by PID

Steps to Kill a Process on a Specific Port in Linux

Use the following steps to kill a process by its process id (PID) in Linux systems.

  1. Open the Terminal:

    Press Ctrl Alt T to open the terminal on your Linux system.

  2. Find the Process ID (PID) Using the Port:

    Type the following command to find the process ID using the specific port. Replace PORT_NUMBER with the actual port number:

    sudo lsof -t -i :PID
    

    Look for the PID in the output under the PID column. This is the number you’ll need to kill the process.

  3. Kill the Process Using the PID:

    Once you have the PID, use the kill command to terminate the process. Replace PID with the actual process ID:

    sudo kill -9 25201
    

    The -9 option forces the process to stop immediately.

  4. Verify the Process is Terminated:

    To ensure the process has been successfully terminated, you can rerun the lsof command:

    sudo lsof -t -i :PORT_NUMBER
    

    If the process was successfully killed, there should be no output.

Killing a Process in One Step

If you want a faster way to stop a process, Linux lets you combine the lsof and kill commands into one simple command. This is great for quickly freeing up a port without doing multiple steps. Here’s how you do it:

sudo kill -9 $(sudo lsof -t -i:8080)

This command first uses lsof to find the process ID (PID) of the process using port 8080, and then it immediately uses kill to stop that process. This makes it easier and quicker to manage processes on your Linux system.

Conclusion

Managing processes on specific ports is an essential skill for Linux users, especially when troubleshooting network issues or freeing up ports for other applications. By following these simple steps, you can quickly identify and kill any process running on a particular port, ensuring your system runs smoothly and efficiently. With this guide, even beginners can confidently manage their Linux system’s ports and processes.