Wednesday, February 17, 2010

Linux cloning over the network using netcat

If you find yourself in a situation where you need to set up a series of Linux computers that use the same configuration, using dd and netcat is one solution to clone servers over the network.

Using netcat with tar
Netcat is known as the Linux Swiss Army knife, meaning that you can do lots of things with it. You can use netcat to open a port on one computer and use that port to pipe data through it from another computer.

For instance, you can use it to easily copy the contents of a directory, as shown in the sample command below where netcat and tar are combined.

On the receiving computer, you can start a netcat listener process. The following command tells netcat to listen on port 1968 and all it receives through that port, is piper to the tar x command, which will extract the tar file that comes through the pipe.

# netcat -l -p 1968 | tar x

At the other end of the connection is the netcat receiver. In this example, that would be the command that creates the tar file and pipes it through netcat to the destination host. This command looks like:

# tar c . | netcat 1968

The first part of the command starts a tar job on the contents of the current directory, the second part sends the result to netcat which listens on host, port 1968.

Netcat and multicast using tee
As you have read, netcat is an easy way to get a file from one computer to another. There is a disadvantage though, the command does not support multicast.

That means that you can't start netcat as a listener on multiple computers and have one computer send data to the multicast port.

But, you can use a workaround and connect multiple computers in a netcat chain. Let's imagine that you have ten computers.

On there is a bunch of iso file that you want to distribute over the network to the computers to

You first have to prepare a netcat session on all of the computers, then on the computer that has the iso files, you would type the following command:

# tar c . | netcat 1968

That would send out the tar files to computer That computer needs a netcat process waiting for incoming data, and then it can extract that data through a tar pipe.

At the same time, you need to send the data through to another computer, you can use the tee command. With tee, you can execute two commands on the output that comes in through a pipe. An example of this would look as in the following line:

# netcat -l -p 1968 | tee > (tar x) | netcat 1968

As you can see, with tee and the output redirector, the data is sent to the tar x command to extract the data. At the same time, the data is send to the computer with IP address, where a netcat process has to be listening on port 1968.

So on that computer, you would also have a netcast process waiting for incoming data:

# netcat -l -p 1968 | tee (tar x) | netcat 1968

This process is repeated through to the last computer in the chain, the one that has IP address

On that computer you would just have netcat listening for incoming data and send that data directly through to the tar process.

So on, you would have the following command waiting:

# netcat -l -p 1968 | tar x

To start this multicast alike sequence, you have to start with the listener on, after that you enter the command on, all the way up to the netcat sender that is started in

You will see the files being copied to all machines in the chain very quickly. But, this is just a test drive. Once you have confirmed that it works on your Linux system, you can get to the serious work, and use this method to distribute an image to multiple computers.

Distributing a Linux server image with netcat multicast
You just did a test drive distributing some files with tar. You can do the same with dd, which you can use to clone complete hard drives.

First, consider this command:

# dd if=/dev/sda of=/dev/sdb bs=4096

Using this command, you would copy block by block the entire /dev/sda disk to /dev/sdb.

If /dev/sdb for instance is a USB hard drive connected to your computer, once this command is complete, you would have a one-on-one copy of the original hard drive.

Make sure that you have tried this and understand it completely before proceeding to the next step.

What you can do with local hard drives, you can do over the network as well. That means that to clone a hard drive /dev/sdb that is connected to computer to the /dev/sda on, you can use a combination of dd and netcat.

But: to make sure it works, you have to boot both of the computers from a live CD, so that there are no files on the local hard disk in use. If both the computers are booted from a live CD, just start the listener process on

# netcat -l -p 1968 | dd of=/dev/sda

and on start the sending process:

# dd if=/dev/sdb | netcat 1968

After you have verified that this works, you can create the netcat-dd daisy chain, by starting on the last computer in the range (

# netcat -l -p 1968 | dd of=/dev/sda

Next, on, start the following command:

# netcat -l -p 1968 | tee > (dd of=/dev/sda) | netcat 1968

and on it would look like:

# netcat -l -p 1968 | tee > (dd of=/dev/sda) | netcat 1968

Next you continue up the chain until you are at the first computer, where you have started the initiating netcat process:

# dd if=/dev/sdb | netcat 1968

Once the work has been done, you have cloned a hard drive to multiple computers on the network.

This is a nice method to clone a Linux hard drive over the network to multiple computers using netcat.

However, if you have to do this type of work often, there are other solutions that you should consider, such as Clonezilla.

But, that tool does require you to set up a server, which is not the case for the netcat solution.


  1. Thanks for the great info. Last day I did a full backup clone of my 500Gb hard-drive. Thanks again!

  2. Thanks, now I have a good solution to create a backup copy from an old live zero downtime server since it has no available usb ports or equal. So mounting the same kind of HDD on another machine and dd with netcat to that drive directly will work like a charm. When the day comes and the hdd crashes. the drives can just be switched fast. =)

  3. I have been some problem with my connection and I thought could be my network card, how ever i wasn´t sure. So, I decided to looking for information by internet and try to understand the network problem. I am happy because all the solution which advice this blog are very useful and interesting.
    costa rica investment opportunities gave me another alternatives to do the best investment, but of course i always will need a good network.

  4. should be:
    tar c - . | netcat 1968