Copy a file from one machine to another using Netcat
Suppose that you wish to copy the file
bar.example.com. Neither machine is running an SSH server and you would prefer not to install one, but Netcat is already present.
The method described here uses two instances of Netcat to transfer the file:
- one running as a TCP server to receive the file, and
- one running as a TCP client to send the file.
When run as a TCP server Netcat listens for an inbound connection on a given port number. Once a connection has been established, it forwards any data that it receives to
stdout. In this example the data should be written to the file
stdout will be redirected to that pathname.
The port number should be one that is not already open, preferably from the range reserved for ephemeral and private use (officially 49152 to 65535, but typically 32768 to 61000 on Linux-based systems). In this example port 60000 will be used.
There are several variants of Netcat in circulation, and the command needed to invoke it as a server depends on which one is installed. Typically the required command will be:
nc -l 60000 > qux.txt
-l option indicates that Netcat should listen for inbound connections. The only other argument needed is the local port number, which is 60000 in this instance.
When using the variant of Netcat that is traditionally installed on Debian-based systems, the local port number must instead be specified using the
nc -l -p 60000 > qux.txt
(Unnecessarily adding the
-p option will result in an immediate error. Incorrectly omitting it will result in Netcat choosing an arbitrary ephemeral port number instead of the one specified, in which case it will not able able to receive the file.)
When run as a TCP client Netcat establishes an outbound connection to a given IP address and port number. It then reads data from
stdin and forwards it through the connection. In this example the data should be taken from the file
stdin will be redirected to read from that pathname.
As previously, the required command depends on the variant of Netcat that is installed. Typically it will be:
nc bar.example.com 60000 < qux.txt
An exception is the variant traditionally installed on Debian-based systems, which by default does not terminate when the end of the input is reached. The required behaviour can then be obtained by adding the option
nc -q 0 bar.example.com 60000 < qux.txt
(Unnecessarily adding the
-q option could either have no effect or result in an immediate error, depending on whether or not it is recognised. Incorrectly omitting it will result in Netcat failing to terminate after the file has transferred.)
If there are several files to be copied then the
tar command can be used to combine them into a single stream of data prior to transmission:
tar zc *.txt | nc -l 60000
and then to reconstruct the individual files on reception:
nc bar.example.com 60000 | tar zx
In the method described above the file is sent from the client to the server, but there is no reason why it cannot be transferred in the opposite direction. This could be useful if (for example) one of the machines is located behind a NAT or a firewall. It can be achieved by reversing the redirection operators for both the server:
nc -l 60000 < qux.txt
and the client:
nc bar.example.com 60000 > qux.txt
-q option is needed then it should be applied to the server command in this case, since it is the server that reads the file from
stdinand can detect when the end has been reached.