How to Use the Netcat Command in Linux

The fundamental operations that may be used on data in every system are reading and writing. Transmission Control Protocol, sometimes known as TCP, is a connection-oriented communication protocol that facilitates message sending and receiving between computers in a network.

It is the most often used protocol in networks that use the Internet Protocol (IP); when used together, it is sometimes referred to as TCP/IP. User Datagram Protocol, or UDP for short, is a communication protocol used to build loss-tolerant, low-latency connections between internet applications.

In this post, we’ll talk about the Ubuntu 22.04 command “Netcat,” which is used to read and write discs via networks using the TCP and UDP protocols (Linux OS).

Numerous instructions are provided in the Netcat utility program for controlling networks and keeping track of data transfer between systems. The internet and other computer networks are built on top of the TCP and UDP protocols. One of the most useful tools in the arsenal of network and system administrators, it is known as the Swiss Army knife of networking tools.

A cross-platform application called Netcat works with Linux, Windows, Mac OS X, and BSD. Netcat may be used to transmit data, scan for open ports, debug connectivity, evaluate connectivity, scan for open ports, and function as a proxy.

Netcat Port Scanning

Port scanning is one of the most often used Netcat apps. You can choose to scan a specific port or a set of ports.

TCP Port Scanning:

Scan for open ports:

If you are planning to scan ports starting from port 30 and ends at port 60 then you can do that by typing:

$ nc -z -v 30-60


In the above command -z and -v are two different flags where -z is used to scan open ports without sending any data -v is verbose to give you additional details.

Filter the result using grep command:

$ nc -z -v 2>&1 | grep succeeded


UDP Port Scanning:

To scan ports for UDP you need to use the -u flag along with the other two that we have previously use.

$ nc -z -v -u 30-60


Netcat Sending Files

Netcat may be used to transfer data from one host to another by establishing a fundamental client or server paradigm. To do this, set Netcat to listen on a specific port on the receiving host using the -l option, then create a conventional TCP connection between several machines and transmit the file over it.

Run the command shown below to open port 6666 for incoming connections and direct the output result to a file on the receiving end:

$ nc -l 6666 > linux1.txt

This will send the file to receiving host which is connected to the sending host:

$ nc 6666 < linux2.txt


Create Web Server

The first is to create any HTML file and we have used a nano editor to do that as shown below:

$ nano linux.html


Then we have written a basic HTML code in the HTML file as shown and then you can save it using “CTRL + S” and then you can close this file using “CTRL + X”



 <title> Linux<title>




$ printf 'HTTP/1.1 200 OK\n\n%s' "$(cat linux.html)" | netcat -l 8888


Now in browser, you can access the file by below-mentioned link:



The TCP/UDP protocols are used by the fundamental Linux program Netcat to read and write data via network connections. It is designed to be a trustworthy backend tool that can be used either manually or automatically by other programs and apps. This article provides information on using Netcat with the TCP and UDP protocols as well as several other uses, like scanning ports, sending files, and creating web servers using Netcat.

Print Friendly, PDF & Email