Linux nc Command with Examples
Linux nc Command with Examples
The nc command, commonly called Netcat, is a versatile Linux networking utility used to read from and write to network connections. It can communicate using TCP or UDP and is often described as a “networking Swiss Army knife.” University students can use nc to understand sockets, ports, client-server communication, troubleshooting, and basic network testing.
The nc command establishes network connections between two endpoints. It can operate as either a client or a listener. Its basic syntax is:
$ nc [options] host port
For example, the following command attempts to connect to port 80 on a web server:
$ nc example.com 80
Check Whether a Port Is Open
One of the most useful applications of Netcat is testing network ports. Use the -z option to scan without sending application data and -v for verbose output.
$ nc -zv example.com 80
You can test a range of ports as well:
$ nc -zv 192.168.1.10 20-100
This can help students understand whether a service is accepting connections on a particular port.
Create a Simple TCP Server
Netcat can listen for incoming connections. The -l option puts it into listening mode.
$ nc -l 5000
The machine now waits for a TCP connection on port 5000. Another computer can connect using:
$ nc 192.168.1.10 5000
Text entered at one endpoint can be received by the other, demonstrating basic client-server communication.
Send a File Using nc
Netcat can also demonstrate simple data transfer in a controlled environment. On the receiving machine:
$ nc -l 5000 > received.txt
On the sending machine:
$ nc 192.168.1.10 5000 < sample.txt
The contents of sample.txt are transmitted to the listener and saved as received.txt. For real-world file transfers, authenticated and encrypted tools such as scp or sftp are generally preferable.
Use UDP with nc
Netcat can communicate using UDP with the -u option. Start a listener:
$ nc -u -l 5000
Then send a UDP message from another terminal:
$ echo "Hello UDP" | nc -u 192.168.1.10 5000
Important Options
-l– listen for incoming connections-v– display detailed connection information-z– scan for listening ports without sending data-u– use UDP instead of TCP-w– specify a connection timeout
The nc command provides a practical way to explore networking concepts such as IP addresses, ports, TCP, UDP, sockets, listeners, and client-server communication. It is especially useful in networking laboratories and troubleshooting exercises. However, students should use port scanning and connection-testing commands only on systems they own or have explicit permission to test.
See also:
curl Command
https://www.testingdocs.com/linux-curl-command-with-examples/