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 uname Command: Check Kernel and System Architecture

Linux uname Command: Check Kernel and System Architecture

Learn how to check Kernel and System Architecture with the Linux uname command, including practical examples, key options, and important precautions.

Linux rmdir Command: Remove Empty Directories

Linux rmdir Command: Remove Empty Directories

Learn how to remove empty directories with Linux rmdir, delete empty parent paths, diagnose failures, and understand when rm -r is different.

Linux split Command: Divide Large Files into Smaller Parts

Linux split Command: Divide Large Files into Smaller Parts

Learn how to divide Large Files into Smaller Parts with the Linux split command, including practical examples, key options, and important precautions.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Practice searching, sorting, grouping, extracting, and counting text with grep, sort, uniq, cut, and wc, including reproducible output and key caveats.

Linux info Command: Browse GNU Documentation

Linux info Command: Browse GNU Documentation

Learn how to use the Linux info command to browse hierarchical GNU manuals and navigate Info nodes, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux find Command: Search Files by Name, Type, Size, and Time

Linux find Command: Search Files by Name, Type, Size, and Time

Learn how to search with Linux find by name, file type, size, and modification time, combine expressions, run commands safely, and verify targets before deletion.