Freeing up a TCP/IP Port on Linux


Introduction

TCP/IP ports are used to connect and communicate with different devices and applications on a network. In Linux, sometimes a specific port may be in use by a particular application or process, preventing other applications from using it. In such cases, it is necessary to free up the port by stopping the process or application using it.

In this article, we will discuss how to free up a TCP/IP port on a Linux system. We will also discuss some common tools and commands that can be used to identify the process or application using a particular port and terminate it.

Identifying the Process or Application Using a Port

Before we can free up a port, we need to identify the process or application using it. There are several tools and commands that can be used to do this.

netstat tool

A netstat command is a useful tool for displaying network statistics and the status of network connections. It can be used to list all active TCP/IP connections, including the ports they are using.

To list all active TCP/IP connections, use the following command -

$ netstat -atn

This will display a list of all active TCP connections, along with the local and remote addresses, the state of the connection, and the PID (process ID) of the process using the connection.

To list only the ports being used by a specific process, use the following command -

$ netstat -atn | grep <process name>

For example, to list the ports being used by the Apache web server, use the following command

$ netstat -atn | grep apache

lsof tool

The lsof command (short for "list open files") is another useful tool for identifying the process or application using a particular port. It can be used to list all open files on a system, including network sockets and ports.

To list all open network sockets and their associated PIDs, use the following command -

$ lsof -i

To list only the ports being used by a specific process, use the following command -

$ lsof -i | grep <process name>

For example, to list the ports being used by the Apache web server, use the following command

$ lsof -i | grep apache

Freeing up the port

Once we have identified the process or application using a particular port, we can stop it to free up the port. There are several ways to do this, depending on the process or application.

Terminating the Process

If the process or application using the port is a standalone process, we can simply terminate it to free up the port. To do this, we need to find the PID of the process using the port, and then use the kill command to terminate it.

For example, if the Apache web server is using port 80, we can use the netstat command to find its PID

$ netstat -atn | grep apache
tcp 0 0 :::80 :::* LISTEN 1785/httpd

In this case, the PID of the Apache process is 1485. To terminate the process, use the following command

$ kill 1485

Stopping the Service

If the process or application using the port is a service, we can stop the service to free up the port. To do this, we can use the systemctl command.

For example, to stop the Apache web server service, use the following command

$ systemctl stop httpd

Closing the Connection

In some cases, the process or application using the port may be a network connection, rather than a standalone process or service. In such cases, we can use the netstat command to close the connection and free up the port. For example, to close a connection using port 80, use the following command

For example, to close a connection using port 80, use the following command -

$ netstat -atn | grep :80
tcp 0 0 192.168.0.43:80 192.168.0.44:56632 ESTABLISHED 1485/httpd

In this case, the connection is being used by the Apache web server (PID 1485). To close the connection, use the following command

$ netstat -atn -f inet -w 1 | grep :80 | grep ESTABLISHED | awk '{print $7}' |
awk -F '/' '{print $1}' | xargs kill -9

This command will close all connections using port 80 that are in the ESTABLISHED state.

Conclusion

In this article, we have discussed how to free up a TCP/IP port on a Linux system. We have also discussed some common tools and commands that can be used to identify the process or application using a particular port, and terminate it. By following these steps, you can easily free up a port and allow other applications or processes to use it.

Updated on: 04-Jan-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements