Linux copy and clone USB stick including partitions command

I need to copy and clone existing data from a USB stick. How do I clone a bootable USB key/pen drive on Linux? How do I clone a USB stick including partitions on Linux operating system?

You can easily clone USB flash drives on Linux. It is useful for backups and other purposes such as cloning USB for installation. You need to use the dd command. It will clone a bootable USB (or USB hard disk) for backup. This page shows how to copy and clone USB stick on Linux operating systems.

ADVERTISEMENTS

Linux copy and clone USB stick command

dd command used for copy a file, converting and formatting according to the operands. The procedure clone a USB stick including partitions is as follows on Linux:

  1. Insert USB disk/stick or pen drive
  2. Open the terminal application
  3. Find out your USB disk/stick name using the lsblk command
  4. Run dd command as: dd if=/dev/usb/disk/sdX of=/path/to/backup.img bs=4M

Let us see all commands in details.

Copy and clone a USB stick including partitions on Linux

Naturally, the first step is to find out your USB stick name on Linux. Selecting the wrong device name can result in data loss.

Find USB disk name on Linux

Simply run the dmesg command after inserting the USB stick or key:
$ dmesg
Filter out info using the grep command:
$ dmesg | grep -i usb
$ dmesg | grep -i 'attached'

Sample outputs:

[    5.793647] sd 0:0:0:0: Attached scsi generic sg0 type 0
[    5.795335] sd 0:0:0:0: [sda] Attached SCSI disk
[    7.421446] sd 6:0:0:0: Attached scsi generic sg1 type 0
[    7.438791] sd 6:0:0:0: [sdb] Attached SCSI removable disk
[  334.349540] sd 6:0:0:0: Attached scsi generic sg1 type 0
[  334.365272] sd 6:0:0:0: [sdb] Attached SCSI removable disk

/dev/sdb is my usb stick. Additionally, one can run the following lsblk command lists information about all available or the specified block devices:
$ lsblk
OR use the fdisk command to list the Linux disk partition tables for the specified devices and its size:
$ sudo fdisk /dev/sdb
Linux find USB disk or pen key name commands

Use dd command to copy and clone a usb stick on Linux

The syntax is as follows:
dd if=/dev/sdX of=/path/to/file.img bs=SIZE
To clone a usb stick named /dev/sdb to ~/usb-opensuse-current.img, run:
$ sudo dd if=/dev/sdb of=~/usb-opensuse-current.img bs=4M
You can show progress copy bar with status option for the dd command:
$ sudo dd if=/dev/sdb of=~/usb-opensuse-current.img bs=4M status=progress
Linux copy and clone USB stick including partitions
Verify new file with the ls command:
$ ls -i ~/*.img
You may want to change file permission too as the sudo command created the image with root:root. Use the chown command as follows:
$ sudo chown vivek:users ~/usb-opensuse-current.img
$ ls -l ~/usb-opensuse-current.img

Optional: Copy file usb-opensuse-current.img to NAS server or external media

Be sure that you copy ~/usb-opensuse-current.img somewhere safe. I usually like to keep on a FreeNAS or Linux NAS server using the scp command:
$ scp ~/usb-opensuse-current.img user@IP:/path/to/safe/
OR
$ scp ~/usb-opensuse-current.img vivek@server1.cyberciti.biz:~/backups/x230/usb/

Say hello to ddresuce

First, you need to install ddrescue using the dnf command/yum command/apt command/apt-get command as per your Linux distro:
sudo dnf install ddrescue ## fedora ##
sudo yum install ddrescue ## centos/rhel from EPEL repo ##
sudo apt install gddrescue ## debian/ubuntu and friends ##

Now you can clone the disk easily:
ddrescue /dev/INPUT /dev/OUTPUT
sudo ddrescue /dev/sdb ~/my-usb.img

See “Linux: Save and Recover Data From Crashed Disks With ddrescue Command Like a Pro” for more information.

Conclusion

You just learned how to clone a USB stick including partitions on Linux operating system using the dd command. For more info see man pages by typing the following man command:
man dd
man lsblk
man dmesg
man fdisk

See also

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • Kitt Phi Sep 21, 2020 @ 20:46

    Hello Vivek,
    Thank you for the article. I am not getting the same result when running the command
    $ sudo dd if=/dev/sdc of=~/usb-opensuse-current.img bs=4M status=progress

    I am booted into a SSD Ubuntu18. I then attach a second SSD, which I am trying to clone, it contains two partitions, which contains Ubuntu18. When I run the above command it does not perform the clone. Any advice would be appreaciated.
    Thanks so much,
    Kitt

    ecg@ecg-motion-01:/$ dmesg | grep -i 'attached'
    [    2.231282] sd 4:0:0:0: Attached scsi generic sg0 type 0
    [    2.231355] scsi 4:0:0:1: Attached scsi generic sg1 type 13
    [    2.296033] sd 4:0:0:0: [sda] Attached SCSI disk
    [    2.296081] ses 4:0:0:1: Attached Enclosure device
    [    2.653993] sr 1:0:0:0: Attached scsi CD-ROM sr0
    [    2.654085] sr 1:0:0:0: Attached scsi generic sg2 type 5
    [   27.684786] sd 5:0:0:0: Attached scsi generic sg3 type 0
    [   27.684943] ses 5:0:0:1: Attached Enclosure device
    [   27.685046] ses 5:0:0:1: Attached scsi generic sg4 type 13
    [   29.754651] sd 5:0:0:0: [sdb] Attached SCSI disk
    [  696.320882] scsi 5:0:0:0: Attached scsi generic sg3 type 0
    [  696.321163] ses 5:0:0:1: Attached Enclosure device
    [  696.321459] ses 5:0:0:1: Attached scsi generic sg4 type 13
    [  697.536750] sd 5:0:0:0: [sdb] Attached SCSI disk
    [ 1893.895080] sd 5:0:0:0: Attached scsi generic sg3 type 0
    [ 1893.895321] ses 5:0:0:1: Attached Enclosure device
    [ 1893.895500] ses 5:0:0:1: Attached scsi generic sg4 type 13
    [ 1895.003765] sd 5:0:0:0: [sdb] Attached SCSI disk
    [ 2940.272466] sd 6:0:0:0: Attached scsi generic sg3 type 0
    [ 2940.272688] ses 6:0:0:1: Attached Enclosure device
    [ 2940.273040] ses 6:0:0:1: Attached scsi generic sg4 type 13
    [ 2941.448718] sd 6:0:0:0: [sdc] Attached SCSI disk
    ecg@ecg-motion-01:/$ lsblk
    NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    loop0    7:0    0   956K  1 loop /snap/gnome-logs/100
    loop1    7:1    0 161.4M  1 loop /snap/gnome-3-28-1804/128
    loop2    7:2    0 255.6M  1 loop /snap/gnome-3-34-1804/36
    loop3    7:3    0   2.2M  1 loop /snap/gnome-system-monitor/148
    loop4    7:4    0    97M  1 loop /snap/core/9665
    loop5    7:5    0   276K  1 loop /snap/gnome-characters/539
    loop6    7:6    0 255.6M  1 loop /snap/gnome-3-34-1804/33
    loop7    7:7    0  62.1M  1 loop /snap/gtk-common-themes/1506
    loop8    7:8    0   4.2M  1 loop /snap/gnome-calculator/544
    loop9    7:9    0   276K  1 loop /snap/gnome-characters/550
    loop10   7:10   0 354.9M  1 loop /snap/pycharm-community/209
    loop11   7:11   0  44.9M  1 loop /snap/gtk-common-themes/1440
    loop12   7:12   0  65.9M  1 loop /snap/snapcraft/5308
    loop13   7:13   0    55M  1 loop /snap/core18/1880
    loop14   7:14   0   956K  1 loop /snap/gnome-logs/81
    loop15   7:15   0  67.5M  1 loop /snap/snapcraft/5407
    loop16   7:16   0  55.3M  1 loop /snap/core18/1885
    loop17   7:17   0 350.2M  1 loop /snap/pycharm-community/207
    loop18   7:18   0 160.2M  1 loop /snap/gnome-3-28-1804/116
    loop19   7:19   0   3.7M  1 loop /snap/gnome-system-monitor/127
    loop20   7:20   0   2.4M  1 loop /snap/gnome-calculator/748
    loop21   7:21   0  96.6M  1 loop /snap/core/9804
    sda      8:0    0 238.5G  0 disk 
    ├─sda1   8:1    0   512M  0 part /boot/efi
    └─sda2   8:2    0   238G  0 part /
    sdc      8:32   0 238.5G  0 disk 
    ├─sdc1   8:33   0   512M  0 part 
    └─sdc2   8:34   0   238G  0 part /media/ecg/d8fe946b-4c4d-40a2-93ac-e3654af0b67c
    sr0     11:0    1  1024M  0 rom  
    ecg@ecg-motion-01:/$ sudo fdisk /media/sdc
    
    Welcome to fdisk (util-linux 2.31.1).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    fdisk: cannot open /media/sdc: No such file or directory
    ecg@ecg-motion-01:/$ sudo fdisk /dev/sdc
    
    Welcome to fdisk (util-linux 2.31.1).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): sudo dd if=/dev/sdc of=~/usb-opensuse-current.img bs=4M status=progress
    Created a new partition 1 of type 'Linux native' and of size 238.4 GiB.
    Created a new partition 2 of type 'Linux swap' and of size 47.1 MiB.
    Created a new partition 3 of type 'Whole disk' and of size 238.4 GiB.
    Created a new Sun disklabel.
    The old gpt signature will be removed by a write command.
    Command (m for help):
    • 🐧 Vivek Gite Sep 22, 2020 @ 9:37

      Type the following at shell prompt and not with fdisk session:
      $ sudo dd if=/dev/sdc of=~/usb-opensuse-current.img bs=4M status=progress

  • Michael Sep 28, 2020 @ 14:18

    Do the methods you describe skip over unused space so as to make the image as small as possible?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.