netcat utility (nc command) considered as TCP/IP swiss army knife. It reads and writes data across network connections, using TCP or UDP protocol. It is designed to be a reliable "back-end" tool that can be used directly or easily driven by other programs and scripts. At the same time, it is a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities.
I also install the netcat package for administering a network and you'd like to use its debugging and network exploration capabilities.
One my favorite usage is to migrating data between two server hard drives using netcat over a network. It is very easy to copy complete drive image from one server to another.
You can also use ssh for the same purpose, but encryption adds its own overheads. This is tried and trusted method (hat tip to karl) .
Make sure you have backup of all important data.
It is possible that nc may not be installed by default under Redhat / CentOS / Debian Linux.
Insall nc under Redhat / CentOS / Fedora Linux
Use yum command as follows:
# yum install nc
Loading "installonlyn" plugin Loading "rhnplugin" plugin Setting up Install Process Setting up repositories rhel-x86_64-server-vt-5 100% |=========================| 1.2 kB 00:00 rhel-x86_64-server-5 100% |=========================| 1.2 kB 00:00 Reading repository metadata in from local files Parsing package install arguments Resolving Dependencies --> Populating transaction set with selected packages. Please wait. ---> Downloading header for nc to pack into transaction set. nc-1.84-10.fc6.x86_64.rpm 100% |=========================| 6.9 kB 00:00 ---> Package nc.x86_64 0:1.84-10.fc6 set to be updated --> Running transaction check Dependencies Resolved ============================================================================= Package Arch Version Repository Size ============================================================================= Installing: nc x86_64 1.84-10.fc6 rhel-x86_64-server-5 56 k Transaction Summary ============================================================================= Install 1 Package(s) Update 0 Package(s) Remove 0 Package(s) Total download size: 56 k Is this ok [y/N]: y Downloading Packages: (1/1): nc-1.84-10.fc6.x86 100% |=========================| 56 kB 00:00 Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing: nc ######################### [1/1] Installed: nc.x86_64 0:1.84-10.fc6 Complete!
Debian / Ubuntu Linux netcat installation
Simply use apt-get command:
$ sudo apt-get install netcat
How do I use netcat to copy hard disk image?
Our sample setup
----------------------- HostA // 192.168.1.1 ------------------------ sda NETWORK sdb ------------------------ HostB // 192.168.1.2 -------------------------
Your task is copy HostA /dev/sda to HostB's /dev/sdb using netcat command. First login as root user
Command to type on hostB (receiving end ~ write image mode)
You need to open port on hostB using netcat, enter :
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb
- -p 2222 : Specifies the source port nc should use, subject to privilege restrictions and availability. Make sure port 2222 is not used by another process.
- -l : Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host.
- bzip2 -d : Compresses image using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. This will speed up network transfer ( -d : force decompression mode)
- dd of=/dev/sda : /dev/sda is your hard disk. You can also specify partition such as /dev/sda1
Command to type on hostA (send data over a network ~ read image mode)
Now all you have to do is start copying image. Again login as root and enter:
# bzip2 -c /dev/sda | netcat hostA 2222
OR use IP address:
# bzip2 -c /dev/sda | netcat 192.168.1.1 2222
This process takes its own time.
A note about latest netcat version 1.84-10 and above
If you are using latest nc / netcat version above syntax will generate an error. It is an error to use -l option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored. So use nc command as follows.
On hostA, enter:
# nc -l 2222 > /dev/sdb
On hostB, enter:
# nc hostA 2222< /dev/sda
# nc 192.168.1.1 2222< /dev/sda
Using a second machine (hostB), connect to the listening nc process at 2222 (hostA), feeding it the file (/dev/sda)which is to be transferred. You can use bzip2 as follows.
On hostA, enter:
# nc -l 2222 | bzip2 -d > /dev/sdb
On hostB, enter:
# bzip2 -c /dev/sda | nc 192.168.1.1 2222
- netcat command other usage - Linux port scanning with netcat (nc) command (you can also use nmap command for port scanning)
- nc and bzip2 command man page
- A good collection of netcat command usage
- Another good netcat disk image howto
How do I improve performance?
As suggested by anonymous user:
You should definitely use bs=16M or something like that. Otherwise, the copy will take forever. Copying a 300 GB hard drive over a 1 Gbps cross-over cable took about 1 1/2 hours or so using bs=16M Without this option, the same thing would have taken about 7 hours.
In short use command as follows:
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb bs=16M
Updated for accuracy.TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop