Netcat (nc) Command Examples - Network Utility

Explore Netcat (nc) command examples for TCP/UDP connections, port scanning, proxy usage, and more. Master network utility for developers.

Netcat (nc) Command Examples

Netcat, often abbreviated as nc, is a versatile networking utility that reads and writes data across network connections using the TCP/IP protocol. It's often referred to as the "Swiss army knife" for network administrators and developers due to its wide range of applications, from simple port scanning to complex debugging scenarios.

Establish TCP Connections

Use Netcat to establish a TCP connection from a specific source port to a destination host and port, with an optional timeout.

# To open a TCP connection from <src-port> to <dest-port> of <dest-host>, with a timeout of <seconds>
nc -p <src-port> -w <seconds> <dest-host> <dest-port>

Initiate UDP Connections

Netcat can also be used to open UDP connections to a specified destination host and port.

# To open a UDP connection to <dest-port> of <dest-host>:
nc -u <dest-host> <dest-port>

Specify Source IP Address

When testing network connectivity, you can choose the source IP address for the connection using the -s option.

# To open a TCP connection to port 42 of <host> using <source-host> as the IP for the local end of the connection:
nc -s <source-host> <dest-host> <port>

# To choose the source IP for the testing using the -s option
nc -zv -s source_IP target_IP Port

Listen on UNIX Domain Sockets

Netcat supports creating and listening on UNIX-domain stream sockets, useful for inter-process communication on the same machine.

# To create and listen on a UNIX-domain stream socket:
nc -lU /var/tmp/dsocket

Connect via HTTP Proxy

This utility allows you to connect to a destination through an HTTP proxy, which is particularly useful for network debugging and testing proxy configurations.

# To connect to <dest-port> of <dest-host> via an HTTP proxy at <proxy-host>,
# <proxy-port>. This example could also be used by ssh(1); see the ProxyCommand
# directive in ssh_config(5) for more information.
nc -x<proxy-host>:<proxy-port> -Xconnect <dest-host> <dest-port>

# The same example again, this time enabling proxy authentication with username "ruser" if the proxy requires it:
nc -x<proxy-host>:<proxy-port> -Xconnect -Pruser <host> <port>

Further Learning on Networking

Understanding Netcat is crucial for network troubleshooting. For more in-depth knowledge on network protocols and security, consult resources like: