Skip to content

Instantly share code, notes, and snippets.

@SecretX33
Last active April 26, 2025 16:32
Show Gist options
  • Select an option

  • Save SecretX33/baf782706aeb51471f9c9b81da4650e4 to your computer and use it in GitHub Desktop.

Select an option

Save SecretX33/baf782706aeb51471f9c9b81da4650e4 to your computer and use it in GitHub Desktop.
HD cloning guide (using DD)

Disk Cloning and File Transfer Using DD and Netcat

1. Clone an Entire Disk via Network Using DD

Example scenario:

You want to copy your entire 1TB NMVE SSD into a new, 2TB NMVE SSD you bought, you have two computers, but both of them only have one NMVE slot each, so a direct copy from one SSD into the other using only one computer isn't needed.

Steps:

  1. Install the source disk 1TB SSD in the source computer and the 2TB SSD in the target computer.
  2. Boot both computers into a Linux Live environment (e.g., Ubuntu, Debian, or any distro with SSH and dd).
  3. Use dd over SSH to clone the data.

Commands:

On the Target Computer (with 2TB SSD):

Start a netcat (nc) listener to receive data:

nc -l -p 1234 | dd of=/dev/nvme0n1 bs=64K

On the Source Computer (with 1TB SSD):

Send the disk data over the network:

dd if=/dev/nvme0n1 bs=64K | nc <target_ip> 1234

Notes:

  • Replace <target_ip> with the target computer's IP address.
  • bs=64K sets the block size to 64KB for efficient transfer.
  • This method copies everything, including partition tables and boot sectors.

2. Test Network Transfer Method by First Sending a File via DD

Purpose:

Before cloning an entire disk, verify that the network connection is stable by transferring a test file.

Steps:

  1. Create a sample image file on PC1 (Source Computer).
  2. Transfer the file from PC1 to PC2 (Target Computer).
  3. Verify file integrity using checksums.

Commands:

Step 1: On PC1 (Source Computer)

Create a test image file (100MB):

dd if=/dev/zero of=~/test.img bs=1M count=100
  • if=/dev/zero: Uses zero bytes as dummy data.
  • of=~/test.img: Saves the test file in the home directory.
  • bs=1M count=100: Creates a 100MB file.

Or use an existing image file.

Start the file transfer:

cat ~/test.img | nc -q 0 <PC2_IP> 1234
  • Replace <PC2_IP> with the IP of PC2.
  • -q 0: Ensures nc exits immediately after the transfer.

Step 2: On PC2 (Target Computer)

Start a listener to receive the file:

nc -l -p 1234 > ~/received_test.img
  • -l: Listens for incoming data.
  • -p 1234: Uses port 1234.
  • > ~/received_test.img: Saves the received file.

Step 3: Verify File Integrity

Check the SHA-256 checksum on both computers:

sha256sum ~/test.img  # On PC1
sha256sum ~/received_test.img  # On PC2

If both checksums match, the transfer was successful.


3. Clone an Entire Disk by Saving It to a File on Another Disk Using DD

Purpose:

Instead of transferring the disk directly over the network, you can save it as an image file on another disk. This is useful for backups or staged transfers.

Steps:

  1. Mount the destination disk (where the image will be saved).
  2. Use dd to create an image of the source disk.
  3. Restore the disk from the saved image if needed.

Commands:

Step 1: On the Source Computer

Save the entire disk (/dev/nvme0n1) to an image file on another mounted disk:

dd if=/dev/nvme0n1 bs=64K | pv > /mnt/backup/disk_image.img
  • /mnt/backup/: Should be the mount point of the target disk (e.g., an external drive or a second internal disk).
  • pv: Shows progress (install it via sudo apt install pv if needed).

Step 2: Verify the Image File

Check the image file size:

ls -lh /mnt/backup/disk_image.img

To verify the integrity, compare checksums:

sha256sum /dev/nvme0n1
sha256sum /mnt/backup/disk_image.img

Step 3: Restore the Disk from the Image

If you need to restore the image to a new disk:

dd if=/mnt/backup/disk_image.img bs=64K | pv | dd of=/dev/nvme0n1
  • This will completely overwrite the target disk with the saved image.

Notes:

  • Ensure the target disk is at least as large as the source disk.
  • You can compress the image to save space:
dd if=/dev/nvme0n1 bs=64K | gzip > /mnt/backup/disk_image.img.gz
  • To restore a compressed image:
gzip -d -c /mnt/backup/disk_image.img.gz | dd of=/dev/nvme0n1 bs=64K
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment