LightAtom

nc (netcat) Command: Examples

nc, the network Swiss Army knife, is built into macOS. Test ports, listen on one, send data between machines and debug protocols.

nc -vz -w 2 host 443

Check whether a TCP port is open.

intermediate10 minNeeds: Terminal

When to use it

Use nc to check reachability of a port, to stand in for a server or client while debugging, or to move data between two machines on a trusted network without setting up anything else.

Steps

  1. Test a port

    The most common use.
    nc -vz -w 2 192.168.1.10 22
  2. Scan a small range

    Ranges work in macOS nc.
    nc -vz -w 1 192.168.1.10 20-25
  3. Listen on a port

    On one Mac, listen; on another, connect to it. Whatever you type appears on the other side.
    nc -l 9000
  4. Transfer a file on a trusted network

    Receiver: nc -l 9000 > file.zip. Sender: nc receiver-ip 9000 < file.zip. Data isn't encrypted, so use scp outside your own network.
  5. Check a UDP service

    UDP has no handshake, so results are less certain.
    nc -vzu 1.1.1.1 53

Output explained

Connection to 127.0.0.1 port 4321 [tcp/rwhois] succeeded!
nc: connectx to 127.0.0.1 port 4322 (tcp) failed: Connection refused

The name in brackets is the port's registered service, not necessarily what's running: here a web dev server happens to use 4321.

Options and variations

FlagMeaningExample
-zScan without sending datanc -z host 80
-vVerbosenc -vz host 80
-w NTimeoutnc -w 2 host 80
-lListen modenc -l 9000
-uUDPnc -u host 53
-kKeep listening after a client disconnectsnc -lk 9000
-G NTCP connection timeout (macOS)nc -G 3 -vz host 443

Common errors

ErrorCauseFix
nc: Address already in useAnother process holds the portPick another port, or find the process with lsof.
Listener unreachable from another machinemacOS firewall blocks ncAllow it when prompted, or test on loopback first.

Do it in the app

While nc listens, LightAtom's Ports view shows it as an open port, owned by nc, reachable from the network.

Frequently asked questions

Is netcat installed on a Mac?

Yes, as nc. macOS's version is based on OpenBSD netcat with Apple additions such as -G for the connection timeout.

How do I check if a port is open with nc?

nc -vz -w 2 host port. It prints “succeeded” for open ports and “Connection refused” for closed ones.

Can nc transfer files securely?

No, nc sends data unencrypted. It's fine on your own network for testing; use scp or rsync over SSH otherwise.

LightAtom team

The people who build LightAtom, a network monitor for macOS.