0

I tried running nc -lnvp 1234 > filename.log on my macOS today and it returned this error saying I need to specify a port number, while I specified the port number on my command.

This command always works on my Debian and Ubuntu, is there a reason why its different on macOS?

enter image description here

I tried the same command in my Debian based machine and it worked.

What should I do?

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
PsOom
  • 1
  • 2
  • 1
    I can't figure out why it's complaining (your syntax looks right according to the usage text there), but do keep in mind that just on Linux there are 5 or 6 different programs all named "nc" with similar but not identical usages – even Debian has two – so it's not that surprising that Apple wrote their own for macOS. – u1686_grawity Dec 03 '22 at 10:42
  • i did `man nc` and it says i am running netcat, however i cant seem to get the version no matter what command i use, and its not specified in the manual what option returns the version. – PsOom Dec 03 '22 at 10:48
  • 1
    Does this answer your question? [Netcat on Mac OS X](https://superuser.com/questions/115553/netcat-on-mac-os-x) – Giacomo1968 Dec 03 '22 at 11:07
  • Broader picture: "everything is a file" in \*nix unfortunately does not apply to network sockets. See [this answer](https://unix.stackexchange.com/a/141020/108618): "if Unix had integrated TCP/IP I/O into the filesystem from the start, we wouldn't have the `netcat` vs `socat` vs `Ncat` vs `nc` mess, the cause of which was the same design weakness that lead to the disk imaging and wiping tool proliferation on Windows: lack of an acceptable OS facility". Plan 9 fixed this design misstep and I wish all Unix-like systems work out a single universal interface some day. (Silly me?) – Kamil Maciorowski Dec 03 '22 at 11:19

2 Answers2

2

From ss64.com man nc/netcat

-l  Listen for an incoming connection rather than initiate a connection
    to a remote host.  It is an error to use this option in conjunction 
    with the -p, -s, or -z options.
Tetsujin
  • 47,296
  • 8
  • 108
  • 135
  • why not?, if im waiting for a connection does netcat use a random port for that connection and why is it different on linux where i have to specify a port number? – PsOom Dec 03 '22 at 10:59
  • I have no idea. You'd have to ask Apple. – Tetsujin Dec 03 '22 at 10:59
0

I am using macOS 13 (Ventura) and have tried your command:

nc -lnvp 1234 > filename.log

And it fails with an error that states:

nc: missing port with option -l

Trying the same command without the -p option like this works fine:

nc -lnv 1234 > filename.log

Checking the man page for nc (aka: man nc) in macOS shows this:

 -l      Used to specify that nc should listen for an incoming connection
         rather than initiate a connection to a remote host.  It is an
         error to use this option in conjunction with the -p, -s, or -z
         options.  Additionally, any timeouts specified with the -w option
         are ignored.

Note where it clearly states the following:

“It is an error to use this option in conjunction with the -p, -s, or -z options.”

So that’s the issue; don’t set the -p option and your command will work fine and port 1234 will be used.

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
  • thank you! but im wondering why is the command different on linux where i have to specify a port number? does netcat choose a random port for when that connection is initiated? – PsOom Dec 03 '22 at 11:01
  • 1
    @MaverickS. Ask Apple why they did this. I don’t know. As far as the port number goes, it will use `1234`; it is just that you don’t need to set `-p` to indicate a port is being set. – Giacomo1968 Dec 03 '22 at 11:05