Home / Learning Linux and Unix / Cloning an SD Card on Linux or Mac

Cloning an SD Card on Linux or Mac

You’re in luck! I just added a video on this same topic although a slightly different procedure on Mac. I’ve also got videos to Learn Raspberry Pi, Python, vim and git. Or get to know me and my dog.

Okay, its time to do the SD card cloning. This is full of nuances. First thing, be sure you’re on a Linux machine, because it is equipped with the dd command, which will do a raw copy of media, plowing right through partitions and such. Preferably, you should be on an Ubuntu box, due to its expansive device support and auto-mounting.

Next, make sure you have as much room on your hard drive as the size media you’re using. This is an issue, because of the old recycled boxes and laptops we tend to use for these sorts of spare Linux boxes. The harddrive of the laptop I’m using is only 5 GB, so it is an issue. So if you’re cloning a 2GB SD card, you need AT LEAST 2GB free space available on the hard drive. You can confirm this with the simple “df” command. So just open a terminal window and type df, and you will see how much space you have available per partition on your system.

While you’re at it, promote yourself to root (so long as you don’t plan on walking away from the box).

sudo su

Next, you need to identify the name of your sd card in the /dev directory. Its hard to do, and the little bit of detective work necessary to make it easy is simply to pop your sd card (and card reader) into a port, and then go to a terminal window and type:

dmesg | tail

What this does is it pipes your device messages into the tail command, showing you the last 10 device messages you’ve received. One of them will be the one created by popping the SD card in, and will probably look something like:

[  335.316290]  sd 3:0:0:0: [sdb] Attached SCSI removable disk

This is your smoking gun. It’ll probably be the last entry. And this gives you the device name that you will use in the dd copy command.

Now that your SD card s installed and you know that it is called /dev/sdb (or whatever), you can go ahead and copy the entire raw disk image to your hard drive. If you have 2 SD card readers, you can eliminate this step and go right from card to card. But I don’t, so here’s the copy-to-harddrive command.

dd if=/dev/sdb of=~/sd-card.bin

You might have to be root to do this, so you can sudo su and issue the above command, or optionally:

sudo dd if=/dev/sdb of=/home/mike/sd-card.bin

Just remember where you’re putting the file. It’s possible to open that folder through the GNOME desktop and do refreshes to see the size of the file grow. Mine went up to 1.8 GB before it finished, and took quite a long time.

Put the new media into your SD card reader, and reverse the command. Of course be sure that your source and destination media are identical, right down to the manufacturer and the batch they came from, if at all possible. This is probably best accomplished by buying them all at once, or part of a multi-pack. This is a raw copy akin to a floppy disk or CD-ROM copy, and of course all floppy disks and CD-ROMs are basically laid out the same. But that is not true of SD cards that have many differences, so the more identical the source and destination media you use, the less grief you will cause yourself.

dd if=/home/mike/sd-card.bin of=/dev/sda

If this command seems ridiculously easy, it is. It’s just a long wait. And considering how easy it is, setting yourself up with an Ubuntu box just for this purpose is really the path of least resistance, compared to trying to do it on a PC with Norton Ghost or using some other paid software, or even setting up Cygwin on your PC. Just have a Linux box around for these types of things. It will help you immensely over time with the whole ShankServer thing.

Look, if you made it all the way down to here and have developed the skill to clone SD cards, then you might as well get the most out of it and play with the Raspberry Pi where each SD card can become its own entire system boot disk:

Comments

comments

Previous
Next