In this short tutorial, we’ll see how to free up a TCP/IP port by killing the process using it, and we’ll also see an introduction to TCP states.
For this purpose, we’ll use fuser as the primary way of doing it, and then we’ll also see ss and lsof along with kill. Those programs are usually installed by default. If they are not, we can use the package manager to get them.
We need to be root or use sudo to access all the information about the system and to kill processes.
2. Using fuser to Free up the Port
We can use fuser to show information and to kill the process using the port. This way, when the process finishes, the port will be free to be used.
The usage is simple, we specify the port in the form of port/tcp, and adding -k will kill the process:
$ fuser -k 8000/tcp 8000/tcp: 1479
fuser shows us the PID, which was using the port. Now that the process was killed, if we run fuser 8000/tcp nothing will show up:
$ fuser -k 8000/tcp
There are more parameters we can use:
- -i: fuser asks us for confirmation before killing the process
- -SIGNAL: it sends SIGNAL to the process, by default fuser uses SIGKILL (-KILL)
- -l: it lists all signals available to use with parameter -SIGNAL
This way, we can be more careful and use SIGTERM while asking for confirmation:
$ fuser -i -TERM -k 8000/tcp 8000/tcp: 1479 Kill process 7818 ? (y/N) y
3. Finding the Process and Then Killing It
To find the process, we can use either lsof or ss:
$ lsof -i :8000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME tracd 1479 trac 3u IPv4 6402151 0t0 TCP *:8000 (LISTEN)
$ ss -apt 'sport = :8000' Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 5 *:8000 *:* users:(("tracd",pid=1479,fd=3))
Now we know PID 1479 is using the port, so we kill it:
$ kill 1479
4. TCP States
When we use TCP, the socket is associated with a port, and it cannot be shared among other processes.
Let’s do a quick overview of a few TCP states:
- LISTEN: a server is listening on the port, accepting new connections
- ESTABLISHED: there is a connection between a client and a server
- TIME-WAIT: the process closed the connection, and the port is waiting for a timeout
If the port is in LISTEN or ESTABLISHED state, we free it up by killing the process using it. We should take into account that we’ll lose ESTABLISHED connections.
Sometimes there isn’t any process using the port and, even so, the system tells us the port is busy or in use. This is usually because of the TIME-WAIT state. If this is the case, we only have to wait until the system frees it up, by default the timeout is 2 minutes.
When a process using a port finishes or when we kill it, the port might go to TIME-WAIT state. This behavior depends on how the software uses the socket.
To see if there are ports in TIME-WAIT state, we can use ss with parameters -apt :
$ ss -apt Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 *:ssh *:* users:(("sshd",pid=1226,fd=3)) tcp LISTEN 0 5 127.0.0.1:ipp *:* users:(("cupsd",pid=1303,fd=9)) tcp LISTEN 0 5 *:8000 *:* users:(("tracd",pid=1479,fd=3)) tcp TIME-WAIT 0 0 192.168.0.4:56886 192.168.0.5:https
We can see that port 56886 is in TIME-WAIT state and without any process using it.
In this tutorial, we saw different ways of freeing up a TCP port. One way was to use fuser to find and kill the process. The other way was to use either ss or lsof to find the process and then using kill.
We also saw that a port could be in a TIME-WAIT state waiting for a timeout without any process using it.