1

I've got the same problem as mentioned here

nc: bind failed: Address already in use

i.e.

$ nc -l 3206
Ncat: bind to :::3206: Address already in use. QUITTING.

Can someone give a 1 line command without pages of explanations, references and alternative methods, on how you free up this port...

Snowcrash
  • 679

2 Answers2

5

There's a process that uses that port, and you will not be able to bind to that port until that process has closed its connection.

To find out what process is holding the port:

sudo lsof -i :3206

If it's a process that you think you may be able to terminate without causing any loss of data, then just use kill pid (possibly with sudo if it's root's process), where pid is the process ID reported by lsof.

Kusalananda
  • 333,661
  • 2
    Not necessarily. It may also be an earlier instance of any command including the nc -l itself, where the port is in a TIME_WAIT state. If this is the case you have to wait a short while until the port is released. Check with netstat -nap | grep :3206 – Chris Davies Feb 11 '17 at 00:01
  • ... unless nc sets SO_REUSEADDR ... – thrig Feb 11 '17 at 00:24
  • @thrig nc on OpenBSD sets SO_REUSEPORT. This implementation is available on Linux too I think (netcat-openbsd in Ubuntu). – Kusalananda Feb 11 '17 at 08:01
1

You can see what processes are on port.

netstat -tulpn
  • You might want to explain that a bit. The -p flag requires a protocol name as argument on OpenBSD, and the command, as it stands, lists no processes on Ubuntu 16.04 LTS. Did you forget sudo? – Kusalananda Feb 11 '17 at 07:28