Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Duplicate a Drive in Linux

BASH, Linux, Sysadmin
David Egan

The dd command allows you to duplicate or clone drives exactly - the target will have the same permissions, formatting and media name.

If the source drive is LUKS encrypted, the target drive will have exactly the same encryption state, and will be unlocked by the same passphrase as the original.

I find this useful for making cloned copies of thumbdrives containing password databases.

Determine Paths

This can be achieved with the lsblk command:


# Typical output
NAME                                          MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda                                             8:0    0 465.8G  0 disk  
├─sda1                                          8:1    0   512M  0 part  /boot/efi
├─sda2                                          8:2    0   488M  0 part  /boot
└─sda3                                          8:3    0 464.8G  0 part  
  └─sda3_crypt                                252:0    0 464.8G  0 crypt
    ├─ubuntu--vg-root                         252:1    0 448.8G  0 lvm   /
    └─ubuntu--vg-swap_1                       252:2    0    16G  0 lvm   
      └─cryptswap1                            252:3    0    16G  0 crypt [SWAP]
sdb                                             8:16   0   1.8T  0 disk  
└─sdb1                                          8:17   0   1.8T  0 part  /media/datadrive
sdc                                             8:32   1   7.5G  0 disk  
└─sdc1                                          8:33   1   7.5G  0 part  
  └─luks-04235321-8ad9-4631-934c-2c09cfa700e7 252:4    0   7.5G  0 crypt /media/david/secure-data
sdd                                             8:48   1   7.5G  0 disk  
└─sdd1                                          8:49   1   7.5G  0 part  /media/david/Thumbdrive
loop0                                           7:0    0  80.5M  1 loop  /snap/core/2462
loop1                                           7:1    0  80.5M  1 loop  /snap/core/2381
loop2                                           7:2    0  79.5M  1 loop  /snap/core/2312
loop3                                           7:3    0 182.6M  1 loop  /snap/atom/9
loop4                                           7:4    0 182.6M  1 loop  /snap/atom/8

In this case, /dev/sdc is the source (or ‘input file’ in dd parlance) and /dev/sdd is the target (or ‘output file’).

Clone Drives

Make sure you correctly identify the drives - especially the output/target, since dd will overwrite all data. In this case, we can easily identify the pair of thumbdrives based on their size.

## Clone sdc to sdd
sudo dd if=/dev/sdc of=/dev/sdd

When finished, the new drive will be an exact copy - with the same identifier. As such, it won’t be possible to mount the original and the clone at the same time.

Because of this, if you want to repeat the process you may need to temporarily change the passphrase on the target drive, or wipe it altogether. To wipe the drive:

sudo dd if=/dev/zero of=/dev/sdd


comments powered by Disqus