Skip to main content

Linux nc (Netcat) Command Explained: Practical Networking Examples

·496 words·3 mins
Linux Networking Netcat
Table of Contents

🌐 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 output

This 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 terminal

Connect to a TCP Service
#

nc <hostname> <port>

nc localhost 1234

# Connects to a TCP service running on port 1234

Send a File to a Remote Host
#

nc <receiver_ip> <port> < file_to_send

nc 192.168.1.10 1234 < file.txt

This 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.txt

The 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 25

While 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> 1234

Both 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 80

This is useful for inspecting HTTP responses and debugging web servers.

Connect to a UDP Service
#

nc -u <hostname> <port>

nc -u localhost 1234

The -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
done

This 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.log

This 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.

Related

Linux NAT and Port Forwarding Explained
·615 words·3 mins
Linux Networking Security System Administration
How to Configure Static IP Addresses on Linux Distributions
·475 words·3 mins
Linux Networking IP Configuration
Linux System Security: sudo Authorization and Best Practices
·724 words·4 mins
Linux Sudo Security