0

I have a script to call syscall Kill the process that holds a certain TCP port in Linux (ubuntu). But even if I do that, sometimes the port still remains in /proc/[0-9]/net directory binding the port.

Is there any easy way to force to free the tcp port? I know which proc/X directory holds the port that I want to kill. Can I just delete the /proc/XXX/ directory and consider the port is free to use after this deletion?

Some postings say I can use fuser, but I want to know what's going on in OS level.

Thanks!

1 Answers1

2

The port remains bound because the calling application that is binding to the port does not use the socket option SO_REUSEADDR. You should fix the calling application to do this. In C:

int yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));

This clears itself in the killed application after net.ipv4.tcp_fin_timeout seconds. The best thing to do is fix your calling program to do the right thing though.

Matthew Ife
  • 23,357
  • 3
  • 55
  • 72