Network

The nc command

Opens TCP or UDP connections from the terminal. Use it to check whether a port answers, to listen on a port, or to send data between two machines.

Syntax

nc [opciones] host puerto (cliente) o nc -l [opciones] puerto (escucha)Try it in the terminal →

Most useful options

-zOnly checks whether the port accepts connections. Sends no data. Not used with -l
-vShows connection details: success or refusal
-lListens on the given port instead of connecting
-uUses UDP instead of TCP
-w <segundos>Maximum time to wait on idle connections. Has no effect with -l
-p <puerto>Sets the source port. Cannot be combined with -l
-kWith -l, keeps listening after each connection
-NCloses the connection when the input reaches end of file (EOF)
-nSkips DNS lookups; use IP addresses
-4 / -6Forces IPv4 or IPv6

Copy-ready examples

$ nc -zv localhost 22is the SSH port answering on this machine? If nothing listens, the message is connect ... failed: Connection refused.

Example output

Connection to localhost 22 port [tcp/ssh] succeeded!
$ nc -zv -w 3 miguelacm.es 443checks the HTTPS port of a server with a 3-second timeout. Only use it on hosts you own or have permission to test.
$ nc -l 4444in one terminal, nc -l 4444. In another, nc localhost 4444. What you type in one appears in the other: a 30-second chat that shows how it works.
$ echo "hola" | nc -N localhost 4444sends one line and closes. With nc -l 4444 running on the other side, it prints hola.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
connect to localhost port 22 (tcp) failed: Connection refusedNothing is listening on that port, or the service is down.Check listeners with ss -tln.
nc: Address already in useWhen using -l, another program already listens on that port (the exact text varies by variant).Pick another port, or find the process with ss -tlnp.
nc: getaddrinfo for host "servidor" port 80: Name or service not knownThe host name does not resolve (DNS or a typo).Use the IP address directly.
No error messageThere is no reply, but it waits until the timeout. A firewall drops packets instead of rejecting them.Try -w 3 and check the firewall on the destination.

Typical mistakes

  • Without -z, nc waits for data: it looks like it hangs.

Differences from similar commands

  • netcat: same name, different implementation. netcat-traditional includes -e (run a program when connected) and other options that OpenBSD nc does not have.
  • ncat: the Nmap version, with TLS (--ssl) and access control. It is not always installed.
  • ss: lists the ports already listening on your machine. nc checks ports on other machines.

Security

An nc -l exposes a port to anyone who can reach your machine, and the traffic is not encrypted. Do not use it to send passwords, and stop it with Ctrl+C when you are done.

Related commands

FAQ

What is nc really for?

Speaking TCP/UDP by hand: checking ports, moving data between machines, or starting a quick server for a test.

Does it need sudo?

Not to reach a remote port; yes to listen on ports below 1024.

Why do I see no data with -z?

Because -z only tests the connection. To see a web server's response: printf 'HEAD / HTTP/1.0\r\n\r\n' | nc miguelacm.es 80

Can -z be used with UDP?

Not reliably: a UDP port with no reply can look open.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course