You might be worried that an application you’re running is misbehaving or hogging up your network resources or local CPU power. Such an application could even be a security threat you want to eliminate.
Whatever the reason is, you can use the fuser, lsof, and netstat commands from the terminal (along with kill) to help you find and kill any Linux process using a port number. These commands will work on most Linux operating systems, including Ubuntu.
How to Kill a Linux Process Using the fuser Command
One of the easiest ways to kill a process using a port number is to use the fuser command. This Linux terminal command can list or kill processes accessing files or sockets. If an app runs with network activity (and thus uses an open port), it’s probably doing one of these two things. Before you proceed, you’ll need to make sure you’re comfortable using a command line terminal on your Linux PC. To list the processes that are using a port number using fuser, you can use the following syntax: fuser -n protocol port In this example, protocol can be tcp or udp, while port is the port number you want to check. For example, to see which processes are using TCP port 80, you can run: fuser -n tcp 80 This will print the process IDs (PIDs) of the processes using that port. To kill those processes, you can add the -k option to fuser. This will send a SIGTERM signal to each process, asking them to terminate gracefully. For example: fuser -k -n tcp 80 This will kill all processes using TCP port 80. If some processes don’t respond to SIGTERM, you can use -KILL instead of -k. This will send a kill signal (SIGKILL), which can help to force the currently running processes you’ve identified to terminate immediately. However, this may cause data loss or corruption, so use it cautiously. For example: fuser -KILL -n tcp 80 This will kill all processes using TCP port 80 forcefully. If you run into any issues, run these commands as the super user (using the sudo command) or by using the root user instead.
How to Kill a Linux Process Using the lsof Command
Another way to kill a process using a port number on a Linux PC is to use the lsof command. This command can list open files and sockets on your system. To list the processes that are using a specific port number, you can use the following syntax: lsof -i protocol:port As before, protocol can be tcp or udp, while port is the port number you want to check. For example, to see which processes are using TCP port 53 (typically used for DNS requests), you can run this command: lsof -i tcp:53 This will print some information about each process using that port, including its PID. To kill those processes, you can use the -t option with lsof. This will only print the PIDs of the processes without any other information. You can then pipe this output to the kill command with any signal option. For example: kill $(lsof -t -i tcp:53) This will send SIGTERM signals (the default) to all processes using TCP port 53. If some processes don’t respond to SIGTERM signals as before, you can use -9 instead of nothing after kill. This will send SIGKILL signals as before, forcing them to terminate immediately but may also cause data loss or corruption. For example: kill -9 $(lsof -t -i tcp:53) This will forcefully send SIGKILL signals (the default) to all processes using TCP port 53.
How to Kill a Linux Process Using the netstat Command
You can also use the netstat command to track down running processes on your Linux PC using active and open network ports. netstat lets you view network connections and statistics on your system, allowing you to pinpoint problematic processes.
To list the processes using a port number along with their PIDs, you need to add two options: -p, which shows PIDs, and -l, which shows listening sockets only.
You also need to specify the protocol (tcp, udp, etc.) and optionally filter by state (LISTEN, etc.). For example, to see which TCP processes are listening on any ports, you can run:
netstat -p tcp -l
This will print information about each TCP socket listening on any ports, including its PID.
To filter by specific ports, you must add another option: -n, which shows numerical addresses instead of names. You also need to specify the exact address format: [protocol][@hostname|hostaddr][:service|port].
For example, to see which TCP processes are listening on port 80, you can run:
netstat -p tcp -l -n 80
This will print information about each TCP socket listening on port 8080, including its PID.
To kill those processes, you can use the kill command with any signal option you want and the PIDs you obtained from netstat. For example:
kill 1234 5678
This will send SIGTERM signals (the default) to processes with PIDs 1234 and 5678.
If some processes don’t respond to SIGTERM signals as before, you can use -9 instead of nothing after kill. This will send SIGKILL signals as before, forcing them to terminate immediately but may cause data loss or corruption as before. For example:
kill -9 1234 5678
This will send SIGKILL signals to processes with PIDs 1234 and 5678 forcefully, for instance. Replace 1234 with the correct PID for your running process.
Control Your Linux Applications
Thanks to the steps outlined above, you can quickly kill a running Linux process using a port number using the Linux command line. Remember to use caution when killing processes, especially with SIGKILL signals, as they may cause unwanted side effects—you don’t want to cause system instability, after all. Want to look more closely at your system performance on Linux? You may want to check your memory usage on Linux next. If you’re worried about a potential security risk, you may want to change your password on Linux, too. Need to switch to a new Linux distribution? If you’re a beginner, consider Linux Mint a safe and stable alternative.