{"id":15754,"date":"2022-02-28T16:18:49","date_gmt":"2022-02-28T16:18:49","guid":{"rendered":"https:\/\/linuxways.net\/?p=15754"},"modified":"2022-02-28T16:18:49","modified_gmt":"2022-02-28T16:18:49","slug":"how-to-use-the-netcat-command-to-read-and-write-data-across-the-network","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-use-the-netcat-command-to-read-and-write-data-across-the-network\/","title":{"rendered":"How to Use the Netcat Command to Read and Write Data Across the Network"},"content":{"rendered":"<p>Reading and writing are the basic operations that can be applied to data in every system. TCP is abbreviated as Transmission Control Protocol and is a connection-oriented communication protocol that makes it easier for computers on a network to send and receive messages. It is the most widely used protocol in networks\u00a0that employ the Internet Protocol (IP); it is sometimes referred to as TCP\/IP when used jointly. UDP, abbreviated as User Datagram Protocol, is a communication protocol used to construct low-latency and loss-tolerant connections between applications on the internet. In this article, we will discuss the command &#8220;Netcat,&#8221; which is used to read and write disks across networks using TCP and UDP protocols on Ubuntu 20.04 (Linux OS).<\/p>\n<p>The Netcat utility application includes several instructions for managing networks and monitoring the level of data between systems. The TCP and UDP protocols are the basis of computer networks, like the internet. It is regarded as the Swiss army knife of networking tools and is among the most effective tools in the armory of network and system administrators. Netcat is a cross-platform program that runs on Linux, Windows, Mac OS X, and BSD. Netcat can be used to debug and analyze connectivity issues as well as scan for open ports, transfer data, and act as a proxy.<\/p>\n<h2><strong>Using Netcat to Read and Write Data Across the Network<\/strong><\/h2>\n<p>On macOS and common Linux distributions such as Ubuntu and Debian, the Netcat package comes pre-installed. Some of the &#8220;Netcat&#8221; utilities are mentioned below.<\/p>\n<ul>\n<li>Perform Port Scanning through Netcat<\/li>\n<li>Sending Files through Netcat<\/li>\n<li>Create a web server through Netcat<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ nc [options] host port<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Either \u201cnc\u201d or \u201cnetcat\u201d is used on Ubuntu systems.<\/p>\n<p>A TCP connection will be established to provide host\/hosts and port\/ports by Netcat as\u00a0default. Use the -u option if you want to create a UDP connection.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>$ nc -u host port<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4><strong>Perform Port Scanning through Netcat<\/strong><\/h4>\n<p>One of the most prevalent Netcat applications is port scanning. You have the option of scanning a single port or a range of ports.<\/p>\n<p><strong>TCP:<\/strong><\/p>\n<h4><strong>Scan for open ports:<\/strong><\/h4>\n<p>To scan the open ports in the range of 30-60 using Netcat, run the command mentioned below:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ nc -z -v 10.0.2.15 30-60<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1194\" height=\"735\" class=\"wp-image-15755\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-9.png\" alt=\"Graphical user interface, text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-9.png 1194w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-9-300x185.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-9-1024x630.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-9-768x473.png 768w\" sizes=\"auto, (max-width: 1194px) 100vw, 1194px\" \/><\/p>\n<p><strong>-z<\/strong> will instruct NC to just scan for open ports and not to send any data to them.<\/p>\n<p><strong>-v<\/strong> tells the info about verbose.<\/p>\n<p>Filter the result using the grep command:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ nc -z -v 10.0.2.15 2&gt;&amp;1 | grep succeeded<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1202\" height=\"224\" class=\"wp-image-15756\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-32.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-32.png 1202w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-32-300x56.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-32-1024x191.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-32-768x143.png 768w\" sizes=\"auto, (max-width: 1202px) 100vw, 1202px\" \/><\/p>\n<p>No connection was successful in this range.<\/p>\n<p><strong>UDP:<\/strong><\/p>\n<p>Simply add the -u parameter to the script to check for UDP ports in the below-mentioned command:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ nc -z -v -u 10.0.2.15 30-60<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1202\" height=\"244\" class=\"wp-image-15757\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-33.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-33.png 1202w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-33-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-33-1024x208.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-33-768x156.png 768w\" sizes=\"auto, (max-width: 1202px) 100vw, 1202px\" \/><\/p>\n<p>There is no UDP port in this range.<\/p>\n<h3><strong>Sending Files through Netcat<\/strong><\/h3>\n<p>By establishing a basic client or server model, Netcat may be used to transport data from one host to another. This is accomplished using the -l option on the receiving host to set Netcat to listen on a certain port, then creating a standard TCP connection from multiple computers and transferring the file across it.<\/p>\n<p>Run the below-mentioned command on the receiving end, which opens port 6666 for incoming connections and diverts the output result to the file:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ nc -l 6666 &gt; linux1.txt<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"962\" height=\"162\" class=\"wp-image-15758\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-website-description-aut-2.png\" alt=\"Graphical user interface, website Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-website-description-aut-2.png 962w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-website-description-aut-2-300x51.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-website-description-aut-2-768x129.png 768w\" sizes=\"auto, (max-width: 962px) 100vw, 962px\" \/><\/p>\n<p>Linux1.txt is the filename to be opened for writing, and you can change the filename according to your requirements.<\/p>\n<p>Now receiving host will be connected to sending host and sends the file:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ nc google.com 6666 &lt; linux2.txt<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1169\" height=\"285\" class=\"wp-image-15759\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-10.png\" alt=\"Graphical user interface, text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-10.png 1169w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-10-300x73.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-10-1024x250.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-10-768x187.png 768w\" sizes=\"auto, (max-width: 1169px) 100vw, 1169px\" \/><\/p>\n<h3><strong>Creating Web Server Through Netcat<\/strong><\/h3>\n<p>Firstly, create a simple HTML &#8220;linux, html&#8221; file by using the nano command:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ nano linux.html<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"922\" height=\"273\" class=\"wp-image-15760\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-34.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-34.png 922w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-34-300x89.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-34-768x227.png 768w\" sizes=\"auto, (max-width: 922px) 100vw, 922px\" \/><\/p>\n<p>Type the below mentioned content or you can add content according to your requirement following the html file rules.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>&lt;html&gt;<\/strong>\r\n\r\n<strong> &lt;head&gt;<\/strong>\r\n\r\n<strong> &lt;title&gt; Linux&lt;title&gt;<\/strong>\r\n\r\n<strong> &lt;head&gt;<\/strong>\r\n\r\n<strong>&lt;html&gt;<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1213\" height=\"388\" class=\"wp-image-15761\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-11.png\" alt=\"Graphical user interface, text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-11.png 1213w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-11-300x96.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-11-1024x328.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/graphical-user-interface-text-description-automa-11-768x246.png 768w\" sizes=\"auto, (max-width: 1213px) 100vw, 1213px\" \/><\/p>\n<p>Save by \u201cCtrl+S\u201d and close the file by \u201cCtrl+X\u201d.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>$ printf 'HTTP\/1.1 200 OK\\n\\n%s' \"$(cat linux.html)\" | netcat -l 8888<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1350\" height=\"334\" class=\"wp-image-15762\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-35.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-35.png 1350w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-35-300x74.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-35-1024x253.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/text-description-automatically-generated-35-768x190.png 768w\" sizes=\"auto, (max-width: 1350px) 100vw, 1350px\" \/><\/p>\n<p>Now in the browser, you can access the file by below-mentioned link:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre><strong>http:\/\/server-ip:8888<\/strong><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><strong>Conclusion:<\/strong><\/h2>\n<p>Netcat is a basic Linux utility that uses the TCP\/UDP protocols for reading and writing data across network connections. It&#8217;s intended to be a dependable backend tool that may be operated directly or simply by other applications and programs. This article gives information about the usage of Netcat with TCP and UDP protocol and some other uses like scanning ports, sending files, and creating a web server.<\/p>","protected":false},"excerpt":{"rendered":"<p>Reading and writing are the basic operations that can be applied to data in every system. TCP is abbreviated as Transmission Control Protocol and is a connection-oriented communication&hellip;<\/p>","protected":false},"author":1,"featured_media":15763,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[921],"class_list":["post-15754","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-netcat-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/15754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=15754"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/15754\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/15763"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=15754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=15754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=15754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}