🌐 Introduction #
nc, short for netcat, is a lightweight yet powerful networking utility available on most Linux systems. It allows users to read from and write to network connections using TCP or UDP, and can operate as either a client or a server.
Because of its flexibility and minimal overhead, nc is widely used for network debugging, service testing, port scanning, and ad-hoc data transfer. It is often referred to as the “Swiss Army knife” of networking tools.
🔧 Common Examples #
Check if a Port Is Open #
nc -zv <hostname> <port>
nc -zv example.com 80
# -z: Scan for listening daemons without sending data
# -v: Verbose outputThis is commonly used to verify service availability or firewall rules.
Start a Simple TCP Server #
nc -l <port>
nc -l 1234
# Listens on port 1234 and prints any received data to the terminalConnect to a TCP Service #
nc <hostname> <port>
nc localhost 1234
# Connects to a TCP service running on port 1234Send a File to a Remote Host #
nc <receiver_ip> <port> < file_to_send
nc 192.168.1.10 1234 < file.txtThis method is often used for quick file transfers without additional tooling.
Receive a File on a Specific Port #
nc -l <port> > received_file
nc -l 1234 > file.txtThe receiver must start listening before the sender transmits the file.
Port Scanning #
nc -zv <hostname> <start_port>-<end_port>
nc -zv 192.168.1.1 20-25
# Scans ports 20 through 25While not a full replacement for tools like nmap, this is useful for quick checks.
Establish a Simple Chat Session #
# Server side
nc -l 1234
# Client side
nc <server_ip> 1234Both sides can type messages interactively once connected.
Send a Raw HTTP Request #
echo -e "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" | nc example.com 80This is useful for inspecting HTTP responses and debugging web servers.
Connect to a UDP Service #
nc -u <hostname> <port>
nc -u localhost 1234The -u flag switches nc to UDP mode.
Send Arbitrary Data to a Host and Port #
echo "Hello, World!" | nc <hostname> <port>This pattern is frequently used for testing custom protocols or services.
🚀 Advanced Usage #
Create a Minimal Web Server #
while true; do
echo -e "HTTP/1.1 200 OK\r\n\r\nHello, World!" | nc -l 8080
doneThis loop creates a basic HTTP service listening on port 8080.
Monitor and Log Network Traffic #
nc -l 1234 | tee output.log
# Displays incoming data and writes it to output.logThis approach is useful for debugging protocols or capturing raw traffic.
✅ Summary #
The nc command remains one of the most versatile tools in the Linux networking toolkit. Its ability to act as both client and server, support multiple protocols, and integrate easily with standard Unix pipelines makes it invaluable for developers, system administrators, and network engineers.
Whether you are debugging services, testing connectivity, or transferring data, nc provides a fast and effective solution with minimal complexity.