The nc
( netcat ) command can be used to transfer arbitrary data over the network. It represents a quick way for Linux administrators to transfer data without the need for an additional data transfer services such as FTP, HTTP, SCP etc. This config will show you an example on how to transfer data between to network hosts. We will be transferring data myfile.txt
file from a localhost to a destination host with an IP address 10.1.1.2
.
Destination host
The nc
command first needs to be started on the network host to which we need to transfer data. We will instruct nc
to listen for an incoming request on user defined port number and once the client request comes receive the desired data. Pick the port number of your choice to ensure that it is not blocked by firewall and it is accessible from the source host.
$ nc -l -p 7555 > myfile.txt
The above command instructs nc
to keep listening on port 7555 until it receives a request. Once the request is obtained it will receive a transfer of myfile.txt
file.
Source host
To receive the myfile.txt
file we need to initiate data transfer request using the IP address ( eg. 10.1.1.2 ) or hostname of the destination host and given port:
$ nc 10.1.1.2 7555 < myfile.txt
Troubleshooting
Symptom:
Ncat: Connection refused.
Check whether you have specified a correct port number on the source host. Furthermore, make sure that the port is accessible and is not blocked by the firewall:
$ nmap -p 7555 10.1.1.2 Starting Nmap 6.45 ( http://nmap.org ) at 2015-03-28 09:25 AEDT Nmap scan report for thebeast (10.1.1.2) Host is up (0.00097s latency). PORT STATE SERVICE 7555/tcp open unknown
Lastly, make sure that nc
is listening on destination host:
# netstat -ant | grep 7555 tcp 0 0 0.0.0.0:7555 0.0.0.0:* LISTEN