nc

networkingLinux/Unix
The nc command is one of the most frequently used commands in Linux/Unix-like operating systems. nc Netcat utility for reading from and writing to network connections

Quick Reference

Command Name:

nc

Category:

networking

Platform:

Linux/Unix

Basic Usage:

nc [options] [arguments]

Common Use Cases

    Syntax

    nc [options] [hostname] [port]

    Options

    Option Description
    -4 Use IPv4 addresses only
    -6 Use IPv6 addresses only
    -b Allow broadcast
    -C Send CRLF as line-ending
    -D Enable debugging on the socket
    -d Detach from stdin
    -h Display help
    -i interval Delay interval for lines sent, ports scanned
    -k Keep inbound sockets open for multiple connects
    -l Listen mode, for inbound connects
    -n Numeric-only IP addresses, no DNS
    -o file Output hexdump traffic to file
    -p port Local port number
    -q seconds Quit after EOF on stdin and delay of seconds
    -r Randomize local and remote ports
    -s source Local source address
    -T type Set IP Type of Service
    -t Answer TELNET negotiation
    -U Use UNIX domain socket
    -u UDP mode
    -v Verbose output
    -w timeout Timeout for connects and final net reads
    -X proxy Proxy protocol: "4", "5" (SOCKS), or "connect"
    -x addr[:port] Specify proxy address and port
    -z Zero-I/O mode (used for scanning)

    Note: Some options may vary between different versions of nc. This table covers the most common options available in standard implementations.

    Examples

    How to Use These Examples

    The examples below show common ways to use the nc command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    nc -l 1234
    Listen for incoming connections on port 1234.
    nc 192.168.1.100 80
    Connect to a server at 192.168.1.100 on port 80.
    # Advanced Examples Advanced
    nc -v example.com 80 Connect to example.com on port 80 with verbose output. nc -l -p 1234 > received_file.txt Listen on port 1234 and save received data to a file.
    cat file.txt | nc example.com 1234
    Send the contents of file.txt to example.com on port 1234. nc -l -p 1234 < file.txt Listen on port 1234 and send file.txt to any client that connects. nc -z -v example.com 20-30 Scan ports 20 through 30 on example.com with verbose output. nc -u 192.168.1.100 53 Connect to a UDP service (DNS) at 192.168.1.100 on port 53. nc -l -p 1234 | nc example.com 80 Create a simple TCP proxy between local port 1234 and example.com:80. nc -w 5 example.com 80 Connect with a timeout of 5 seconds. nc -l -k -p 1234 Keep listening for connections after client disconnects.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Netcat (nc) is often referred to as the "Swiss Army knife" of networking utilities due to its versatility and wide range of capabilities. First released in 1995, it has become an essential tool for network administrators, security professionals, and system engineers. At its core, nc creates TCP or UDP connections between hosts, making it useful for everything from simple client-server interactions to complex network debugging and security testing. Key features of the nc command: 1. Connection Establishment: nc can act as either a client (initiating connections) or a server (listening for connections), supporting both TCP and UDP protocols. This flexibility allows for testing virtually any network service. 2. Port Scanning: With the -z option, nc becomes a simple yet effective port scanner, allowing users to check which ports are open on a target system. This is valuable for both network troubleshooting and security auditing. 3. File Transfer: nc can transfer files between systems without requiring file transfer protocols like FTP or SCP. By redirecting input and output, users can send and receive files across network connections. 4. Network Debugging: The verbose mode (-v) provides detailed information about connections, making it an excellent tool for diagnosing network issues, validating firewall rules, or troubleshooting service problems. 5. Proxy Connections: nc can create simple proxy servers or relay traffic between different network segments, useful for bypassing network restrictions or creating basic load balancing solutions. 6. Banner Grabbing: Security professionals use nc to retrieve service banners, which often reveal software versions and configurations that may have known vulnerabilities. 7. Simple Chat Server: By establishing bidirectional connections, nc can be used to create a basic chat system between two or more hosts. 8. Network Service Testing: nc can send custom data to network services, allowing developers and administrators to test how services respond to various inputs. Common use cases for nc include: - Testing if specific ports are open and accessible on remote servers - Creating simple TCP/UDP servers for testing client applications - Transferring files between systems without setting up dedicated file sharing services - Performing basic network diagnostics and troubleshooting - Setting up simple network proxies or relays - Creating backdoor shells for security testing (or unfortunately, for malicious purposes) - Streaming data between network endpoints - Testing firewall rules and network segmentation It's worth noting that while nc is an extremely powerful and flexible tool, its simplicity and capabilities have also made it popular for malicious purposes. Many system administrators restrict its use in production environments, and some Linux distributions don't install it by default due to security concerns. There are several variants of nc, including the original implementation, GNU Netcat, Ncat (part of the Nmap project), and others. These variants may offer different features or command-line options, but they all share the same basic functionality of creating network connections. Because of its ubiquity and versatility, proficiency with nc is considered a fundamental skill for anyone working with networks, especially in system administration, cybersecurity, or network engineering roles.

    Related Commands

    These commands are frequently used alongside nc or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the nc command works in different scenarios.

    $ nc
    View All Commands