First find the proces ID (pid) of the process that uses the port.
For example, to free the port '51000', first find the pid of the process that uses the port '51000'. Use the command 'netstat -tulpn' for the same.
$netstat -tulpn
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:33660 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:24800 0.0.0.0:* LISTEN 1640/synergys
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:51000 0.0.0.0:* LISTEN 11056/sim
tcp6 0 0 :::445 :::* LISTEN -
tcp6 0 0 :::139 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
Using the command 'netstat -tulpn', we found that the 'pid' of the process that uses the port '51000' is '11056'. Now we have to kill the process that binds to the port '51000'. Issue the following command for the same.$sudo kill -9 11056
On doing the above, the port would be freed.