Netcat is a console command used for diagnostics, debugging, creating, and listening to network connections. Here's a brief and clear guide on how to use this utility.
This command is popular for its functionality and versatility — Netcat (nc) can do almost everything you might need when working with networks. Specifically, it can:
Listen to ports — Netcat allows you to create a server that will listen on a specific port and accept incoming connections.
Connect to remote hosts — The utility can be used to connect to remote services, such as web servers, using SSH or FTP protocols.
Send data — Netcat can send data to a remote host over TCP or UDP.
Check service availability, test network connections, and troubleshoot network issues.
The syntax of the command is quite simple — it's easy to understand:
nc [options] <host> <port>
Where <host> is the IP address or domain name of the remote host, and <port> is the port number on the remote host to which the connection will be established.
Let's look at a few common use cases.
For example, if you need to listen on port 42, you would run the following command in the terminal:
nc -l 12345
This will make Netcat wait for incoming connections on port 12345. All data received through this port will be displayed in the terminal.
To connect to the server site.com on port 80, use the following command:
nc example.com 80
To create a UDP connection, you need to use the -u flag, as Netcat defaults to TCP:
nc -u <host> <port>
With Netcat, you can also create a proxy server or redirect traffic from one port to another. For example: nc -l 12345 | nc <another_host> 80
For simple port scanning, you only need the following command:
nc -zv <host> 1-1000
When working with Netcat, you’ll need to know what the flags mean — here’s a simple explanation:
-l — listening mode (used to create a server).
-u — use UDP instead of TCP.
-z — port scanning (without sending data).
-v — verbose output.
-w <time> — wait for connection in seconds.
-n — disable DNS resolution.
-p <port> — use a specific port for listening (for older versions).
-e <program> — execute a command on the server side when the client connects.
Note that Netcat does not provide encryption by default, so for secure connections, it’s recommended to use other tools such as SSH. Also, Netcat can be used for unauthorized access, so its usage may be restricted in some networks or organizations.
Netcat is a powerful tool for system administrators and developers, allowing them to quickly diagnose network issues and test various services.