Linux nc Command: Test TCP and UDP Connections

nc (Netcat) is a versatile tool that opens or listens to TCP/UDP connections to send and receive data. It is also used to quickly check if a port on an authorized host is accessible.

What is the nc command?

Linux distributions may have different implementations, such as OpenBSD netcat or Nmap Ncat, installed under the name nc. The -z option checks whether a connection is possible without sending data, and -v displays diagnostic messages. Some options and listening mode syntax differ between implementations.

Basic syntax

nc [options] host port

The installed implementation and options may vary depending on the distribution. Check the description for your current system with man nc.

Examples

Checking local SSH port

Check if your computer's TCP port 22 is accepting connections.

nc -vz 127.0.0.1 22

Connection timeout

Limit so as not to wait too long on an unresponsive local port.

nc -w 3 -vz 127.0.0.1 80

Direct connection

Observe the data sent by services on your own computer. Exit with Ctrl+C.

nc -v 127.0.0.1 22

Interpreting Port Connection Test Results

nc -vz allows you to simply check whether a TCP port can be connected without sending or receiving data. Supported options and output messages vary depending on the netcat implementation.

nc -vz example.com 443

Example output when the connection is successful:

Connection to example.com 443 port [tcp/https] succeeded!

A successful connection only means that the port accepted a TCP connection; it does not mean that the TLS certificate or HTTP response is valid. If there is a timeout, check the firewall, route, or server waiting status; if it is immediately refused, check whether the target is not accepting connections. Only send test packets to authorized targets.

Main Options and Format

Options/Format Description
-z Check connectivity without sending data.
-v Display diagnostic output in detail.
-w seconds Set connection and I/O timeouts. The meaning may vary depending on the implementation.
-u Use in UDP mode.
-l Receive mode that waits for a connection. Check the syntax according to the implementation.

Precautions when using

A port being open does not guarantee that the application is working correctly or that authentication is secure. UDP -z results are especially difficult to interpret due to the lack of response. Only check external hosts with the owner's permission. Use nc -h to check the options available in the current implementation.

Frequently Asked Questions

Are nc and ncat the same tool?

Both are Netcat derivatives, but their implementations may differ. Check the installed package and the output of nc -h to distinguish the options.

Official Documentation

You can check the exact behavior of the options and the differences between implementations in the official documentation for nc.

More in This Category
Linux df Command: Check Filesystem Free Space

Linux df Command: Check Filesystem Free Space

Learn how to check Filesystem Free Space with the Linux df command, including practical examples, key options, and important precautions.

Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.

Linux printf Command: Print Values with a Defined Format

Linux printf Command: Print Values with a Defined Format

Learn how to print Values with a Defined Format with the Linux printf command, including practical examples, key options, and important precautions.

Linux ssh Command: Connect to a Remote Host Securely

Linux ssh Command: Connect to a Remote Host Securely

Learn how to connect to a Remote Host Securely with the Linux ssh command, including practical examples, key options, and important precautions.

Linux fmt Command: Reformat Plain-Text Paragraphs

Linux fmt Command: Reformat Plain-Text Paragraphs

Learn how to reformat Plain-Text Paragraphs with the Linux fmt command, including practical examples, key options, and important precautions.

Linux realpath Command: Resolve Absolute and Canonical Paths

Linux realpath Command: Resolve Absolute and Canonical Paths

Learn how to use the Linux realpath command to resolve relative paths, symbolic links, and redundant path components, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux id Command: Show User and Group IDs

Linux id Command: Show User and Group IDs

Learn how to show User and Group IDs with the Linux id command, including practical examples, key options, and important precautions.

Linux sed Command: Search and Replace Text Streams

Linux sed Command: Search and Replace Text Streams

Learn how to search and Replace Text Streams with the Linux sed command, including practical examples, key options, and important precautions.

Linux column Command: Format Text as a Table

Linux column Command: Format Text as a Table

Learn how to format Text as a Table with the Linux column command, including practical examples, key options, and important precautions.