Problem solveGet help with specific problems with your technologies, process and projects.

Linux cloning over the network using netcat

Use netcat to clone Linux images over the network onto multiple servers with a series of simple Linux commands including tar, tee, and dd. This alternative to tools like Clonezilla does not require setting up a separate server.

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 10.0.0.10 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 10.0.0.10, 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 10.0.0.10 there is a bunch of iso file that you want to distribute over the network to the computers 10.0.0.11 to 10.0.0.20. 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 10.0.0.11 1968

That would send out the tar files to computer 10.0.0.11. 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 10.0.0.12 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 10.0.0.12, 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 10.0.0.13 1968

This process is repeated through to the last computer in the chain, the one that has IP address 10.0.0.20. On that computer you would just have netcat listening for incoming data and send that data directly through to the tar process. So on 10.0.0.20, 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 10.0.0.20, after that you enter the command on 10.0.0.19, all the way up to the netcat sender that is started in 10.0.0.10. 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 10.0.0.10 to the /dev/sda on 10.0.0.11, 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 10.0.0.11:

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

and on 10.0.0.10 start the sending process:

dd if=/dev/sdb | netcat 10.0.0.11 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 (10.0.0.20):

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

Next, on 10.0.0.19, start the following command:

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

and on 10.0.0.18 it would look like:

netcat -l -p 1968 | tee > (dd of=/dev/sda) | netcat 10.0.0.29 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 10.0.0.11 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.

ABOUT THE AUTHOR:Sander van Vugt is an author and independent technical trainer, specializing in Linux since 1994. Vugt is also a technical consultant for high-availability (HA) clustering and performance optimization, as well as an expert on SLED 10 administration.

Start the conversation

0 comments

Register

I agree to TechTarget’s Terms of Use, Privacy Policy, and the transfer of my information to the United States for processing to provide me with relevant information as described in our Privacy Policy.

Please check the box if you want to proceed.

I agree to my information being processed by TechTarget and its Partners to contact me via phone, email, or other means regarding information relevant to my professional interests. I may unsubscribe at any time.