{"id":8209,"date":"2019-01-16T06:51:15","date_gmt":"2019-01-16T06:51:15","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=8209"},"modified":"2019-03-07T22:35:36","modified_gmt":"2019-03-07T22:35:36","slug":"how-to-use-netcat-to-quickly-transfer-files-between-linux-computers-linux-com","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/16\/how-to-use-netcat-to-quickly-transfer-files-between-linux-computers-linux-com\/","title":{"rendered":"How to Use Netcat to Quickly Transfer Files Between Linux Computers | Linux.com"},"content":{"rendered":"<p>There\u2019s no shortage of software solutions that can help you transfer files between computers. However, if you do this very rarely, the typical solutions such as NFS and SFTP (through OpenSSH) might be overkill. Furthermore, these services are permanently open to receiving and handling incoming connections. Configured incorrectly, this might make your device vulnerable to certain attacks.<\/p>\n<p><code>netcat<\/code>, the so-called \u201cTCP\/IP swiss army knife,\u201d can be used as an ad-hoc solution for transferring files through local networks or the Internet. It\u2019s also useful for transferring data to\/from your virtual machines or containers when they don\u2019t include the feature out of the box. You can even use it as a copy-paste mechanism between two devices.<\/p>\n<h2>How to Install netcat on Various Linux Distributions<\/h2>\n<p>Most Linux-based operating systems come with this pre-installed. Open a terminal and type:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>netcat<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-287768 aligncenter\" title=\"netcat-command-not-found\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-command-not-found.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-command-not-found.png\" alt=\"netcat-command-not-found\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-command-not-found.png\" \/><\/p>\n<p>If the command is not found, install the package that contains netcat, a BSD variant. There is also GNU\u2019s version of netcat which contains fewer features. You need netcat on both the computer receiving the file and the one sending it.<\/p>\n<p>On Debian-based distributions such as Ubuntu or Linux Mint, install the utility with:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>sudo apt install netcat-openbsd<\/pre>\n<\/div>\n<\/div>\n<p>With openSUSE,\u00a0<a href=\"https:\/\/software.opensuse.org\/package\/netcat-openbsd\">follow the instructions on this page<\/a>, specific to your exact distribution.<\/p>\n<p>On Arch Linux enter the following command:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>sudo pacman -S openbsd-netcat<\/pre>\n<\/div>\n<\/div>\n<p>Unfortunately, the RedHat family doesn\u2019t include the BSD or GNU variants of netcat. For some odd reason, they decided to go with nmap-ncat. While similar, some command line options are not available, for example\u00a0<code>-N<\/code>. This means you will have to replace a line such as\u00a0<code>nc -vlN 1234 &gt; nc<\/code>\u00a0with\u00a0<code>nc -vl 1234 &gt; nc<\/code>\u00a0so that it works on RedHat\/Fedora.<\/p>\n<p>To install ncat on RedHat:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>sudo yum install nmap-ncat<\/pre>\n<\/div>\n<\/div>\n<p>And on Fedora:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>sudo dnf install nmap-ncat<\/pre>\n<\/div>\n<\/div>\n<h2>How to Use netcat to Transfer Files Between Linux Computers<\/h2>\n<p>On the computer that will receive the file, find the IP address used on your internal network.<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>ip route get 8.8.8.8<\/pre>\n<\/div>\n<\/div>\n<p>After \u201csrc\u201d you will see the internal network IP address of the device. If, for some reason, results are irrelevant, you can also try:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>ip address<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-287769 aligncenter\" title=\"netcat-find-ip-address\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-find-ip-address.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-find-ip-address.png\" alt=\"netcat-find-ip-address\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-find-ip-address.png\" \/><\/p>\n<p>In the screenshot offered as an example, the IP is\u00a010.11.12.10.<\/p>\n<p>On the same computer, the one that will receive the file, enter this command:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>nc -vl 44444 &gt; pick_desired_name_for_received_file<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-287770 aligncenter\" title=\"netcat-receiving-file\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-receiving-file.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-receiving-file.png\" alt=\"netcat-receiving-file\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-receiving-file.png\" \/><\/p>\n<p>And on the computer which will send the file, type this, replacing 10.11.12.10 with the IP you discovered earlier:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>nc -N 10.11.12.10 44444 &lt; \/path\/to\/file\/you\/want\/to\/send<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-287772 aligncenter\" title=\"netcat-sending-file\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-sending-file.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-sending-file.png\" alt=\"netcat-sending-file\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-sending-file.png\" \/><\/p>\n<p>Directory and file paths can be absolute or relative. An absolute path is \u201c\/home\/user\/Pictures\/file.png.\u201d But if you already are in \u201c\/home\/user,\u201d you can use the relative path, \u201cPictures\/file.png,\u201d as seen in the screenshot above.<\/p>\n<p>In the first command two parameters were used:\u00a0<code>-v<\/code>\u00a0and\u00a0<code>-l<\/code>.\u00a0<code>-v<\/code>\u00a0makes the output verbose, printing more details, so you can see what is going on.\u00a0<code>-l<\/code>\u00a0makes the utility \u201clisten\u201d on port 44444, essentially opening a communication channel on the receiving device. If you have firewall rules active, make sure they are not blocking the connection.<\/p>\n<p>In the second command,\u00a0<code>-N<\/code>\u00a0makes netcat close when the transfer is done.<\/p>\n<p>Normally, netcat would output in the terminal everything it receives.\u00a0<code>&gt;<\/code>\u00a0creates a redirect for this output. Instead of printing it on the screen, it sends all output to the file specified after\u00a0<code>&gt;<\/code>.\u00a0<code>&lt;<\/code>\u00a0works in reverse, taking input from the file specified instead of waiting for input from the keyboard.<\/p>\n<p>If you use the above commands without redirections, e.g.,\u00a0<code>nc -vl 44444<\/code>\u00a0and\u00a0<code>nc -N 10.11.12.10 44444<\/code>, you create a rudimentary \u201cchat\u201d between the two devices. If you write something in one terminal and press Enter, it will appear on the other computer. This is how you can copy and paste text from one device to the other. Press\u00a0<kbd class=\"key\">Ctrl<\/kbd>\u00a0+\u00a0<kbd class=\"key\">D<\/kbd>(on the sender) or\u00a0<kbd class=\"key\">Ctrl<\/kbd>\u00a0+\u00a0<kbd class=\"key\">C<\/kbd>\u00a0(anywhere) to close the connection.<\/p>\n<h2>Optimize File Transfers<\/h2>\n<p>When you send large files, you can compress them on the fly to speed up the transfer.<\/p>\n<p>On the receiving end enter:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>nc -vl 44444 | gunzip &gt; pick_desired_name_for_file<\/pre>\n<\/div>\n<\/div>\n<p>And on the sender, enter the following, replacing 10.11.12.10 with the IP address of your receiving device:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>gzip -c \/path\/to\/file\/you\/want\/to\/send | nc -N 10.11.12.10 44444<\/pre>\n<\/div>\n<\/div>\n<h2>Send and Receive Directories<\/h2>\n<p>Obviously, sometimes you may want to send multiple files at once, for example, an entire directory. The following will also compress them before sending through the network.<\/p>\n<p>On the receiving end, use this command:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>nc -vl 44444 | tar zxv<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-287771 aligncenter\" title=\"netcat-receiving-tar-gzipped-directory\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-receiving-tar-gzipped-directory.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-receiving-tar-gzipped-directory.png\" alt=\"netcat-receiving-tar-gzipped-directory\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-receiving-tar-gzipped-directory.png\" \/><\/p>\n<p>On the sending device, use:<\/p>\n<div class=\"holsh-div nolinks\">\n<div class=\"bash\">\n<pre>tar czp \/path\/to\/directory\/to\/send | nc -N 10.11.12.10 44444<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" class=\"responsively-lazy size-full wp-image-287773 aligncenter\" title=\"netcat-sending-tar-gzipped-directory\" src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-sending-tar-gzipped-directory.png\" srcset=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-sending-tar-gzipped-directory.png\" alt=\"netcat-sending-tar-gzipped-directory\" data-sizes=\"auto\" data-src=\"https:\/\/www.maketecheasier.com\/assets\/uploads\/2019\/01\/netcat-sending-tar-gzipped-directory.png\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>Preferably, you would only use this on your local area network. The primary reason is that the network traffic is unencrypted. If you would send this to a server, through the Internet, your data packets could be intercepted along the network path. But if the files you transfer do not contain sensitive data, it\u2019s not a real issue. However, servers usually have SSH preconfigured to accept secure FTP connections, and you can use SFTP instead for file transfers.<\/p>\n<p><a href=\"https:\/\/www.linux.com\/learn\/how-use-netcat-quickly-transfer-files-between-linux-computers\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There\u2019s no shortage of software solutions that can help you transfer files between computers. However, if you do this very rarely, the typical solutions such as NFS and SFTP (through OpenSSH) might be overkill. Furthermore, these services are permanently open to receiving and handling incoming connections. Configured incorrectly, this might make your device vulnerable to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/16\/how-to-use-netcat-to-quickly-transfer-files-between-linux-computers-linux-com\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Use Netcat to Quickly Transfer Files Between Linux Computers | Linux.com&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8209","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=8209"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8209\/revisions"}],"predecessor-version":[{"id":10618,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8209\/revisions\/10618"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=8209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=8209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=8209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}