Netcat: using nc -l port_number instead of nc -l -p port_number
Asked Answered
H

2

5

This question is following this one: Sockets working in openSUSE do not work in Debian?

When working with sockets on my Debian system, I have to use nc -l -p port_number to simulate the server I want to talk with. If I'm using nc -l port_number, it will fail when using the socket connect function and strerror(errno) will say "Connection refused".

Netcat without -p option is working great on other Linux distributions, what should I change on my configuration?

Hoban answered 10/3, 2013 at 4:2 Comment(0)
P
4

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).

Pion answered 10/3, 2013 at 5:44 Comment(2)
Great answer! I will use the -p option but I've installed netcat-openbsd package to have a fix for netcat-traditional problems. I've tried netcat6, it should just add IPv6 protocol and enhance UDP according to the man page. The -p option is still required with nc6 but explicitly says it: bapt@debian:~$ nc6 -l 4242 nc6: in listen mode you must specify a port with the -p switchHoban
@baptx, thanks for the info about netcat6. I added it to my answer.Pion
R
6

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.

Rebuff answered 10/3, 2013 at 4:10 Comment(2)
Thanks for explaining it is due to my system implementation. I will use -p option to specify port. I still want to have these problems fixed so I can be sure there won't be any surprise in the future. That's why I've installed netcat-openbsd and there was no need to adjust my set, update-alternatives has switched automatically to nc.openbsd ;) By the way, it seems that netcat-traditional doesn't support IPv6Hoban
cygwin has a BSD nc implementation (not sure the version), that says not to use -p with -l.. So I guess maybe you can't always do -p to "be safe" in the sense of correct usage.. " -p source_port Specifies the source port nc should use,.... It is an error to use this option in conjunction with the -l option. "Sudoriferous
P
4

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).

Pion answered 10/3, 2013 at 5:44 Comment(2)
Great answer! I will use the -p option but I've installed netcat-openbsd package to have a fix for netcat-traditional problems. I've tried netcat6, it should just add IPv6 protocol and enhance UDP according to the man page. The -p option is still required with nc6 but explicitly says it: bapt@debian:~$ nc6 -l 4242 nc6: in listen mode you must specify a port with the -p switchHoban
@baptx, thanks for the info about netcat6. I added it to my answer.Pion

© 2022 - 2024 — McMap. All rights reserved.