Netcat: using nc -l port_number instead of nc -l -p port_number Netcat: using nc -l port_number instead of nc -l -p port_number unix unix

Netcat: using nc -l port_number instead of nc -l -p port_number


Do not adjust your set. There are multiple implementations of netcat out there; not all of them behave the same.

In particular, the "traditional" version of netcat, which is probably what you have installed on your Debian system, will end up doing something totally unexpected if you omit the -p ("port") flag: it will end up treating the last argument as a hostname, pass it to inet_aton(), which will convert it to a nonsensical IP address (e.g, 1234 will become 0.0.4.210), and will then proceed to ignore that IP address and listen on a socket with an automatically assigned (probably random) port number.

This behavior is obviously silly, so some other implementations of netcat will assume you meant -p. The one you're using doesn't, though, so pass the -p option.


I agree with duskwuff that it is better to just use the -p option everywhere, but to answer your question:

The one thing you have to do is install a netcat that supports the syntax you want. I know the netcat-openbsd package supports it. I know the netcat-traditional package does not. There's also a netcat6 package, which also doesn't. You can then explicitly request the OpenBSD version of netcat like so:

nc.openbsd -l 4242

Optionally you may use the alternatives system to set this version of netcat to run when you issue the nc command:

update-alternatives --set nc /bin/nc.openbsd

This will be done automatically for you if this is the only netcat you've installed.

Finally, you may, again optionally, remove the netcat you don't like (netcat-traditional or netcat6).