Advertisement

How to Clone Raspberry Pi SD Cards Using the Command Line in OS X

by
Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

The Raspberry Pi runs from an operating system stored on a Secure Digital (SD) card and many different operating systems may be employed. Storage is relatively inexpensive, can be created (flashed), recreated, written to and overwritten with ease.

On the one hand, this is an advantage of the Pi. On the other, the experimental nature of the Pi means more time flashing SD cards. This tutorial shows you how to use a Mac to clone any Raspberry Pi SD card which is particularly useful when you have your OS set up just as you want it.


Raspberry Pi

The Raspberry Pi is somewhat different to the computing, to which most of us have been accustomed, of the last two decades. It’s a bare-bones board of components and connectors that requires you to source a power supply, keyboard, monitor and mouse to get it up and running.

With almost two and a half million units sold–in little over a year since its launch–and with its focus on getting kids (adults?) coding and experimenting, it might not be an exaggeration to consider the Raspberry Pi to be a revolutionary device.

Being able to experiment, play and break things is all part of the fun. By cloning your SD cards, you can be up and running again with the minimum of downtime.


Set Up Your SD Card

Before embarking on cloning your SD card, install NOOBS or the OS of your choice, customise it and get it set up exactly how you like to use it.

Tip: To set up your SD card, read our tutorials How to Install NOOBS on a Raspberry Pi With a Mac and How to Install NOOBS on a Raspberry Pi With a Mac.


Cloning the SD Card With Mac OS X

Cloning is the process of making an exact copy. The steps shown, below, will take you through the process of creating a disk image of your existing Raspberry Pi SD card, regardless of the exact operating system you have on it or how it has been set up.

The benefit of having a disk image of your SD card is that you have a backup of your data from which you can easily write a new SD card


Cloning Using the Command Line (CLI)

Step 1. Insert Your SD Card

Ensure that you have inserted the SD Card, that you wish to clone, into the SD Card reader of your Mac. If your Mac does not have an internal SD Card reader, you will need to plug in an external SD Card reader via a USB socket.

Step 2. Locate Your SD Card

Open Terminal and enter the following command to locate your SD Card:

diskutil list

The response will be something similar to that shown in my screenshot of Terminal. Identify your SD Card (look under NAME and SIZE to verify correct volume). In my example, the SD Card is /dev/disk2.

Locating the SD Card using a Terminal Command
Locating the SD Card using a Terminal Command

Step 3. Cloning Your SD Card

Whether your RPi SD Card is all set up as you want it, or you just want to make an incremental clone before installing some software, backing up your SD Card could save you time in the long run.

In my example, my SD Card is /dev/disk2. Amend this as necessary for yours. In Terminal, enter the following command to create a disc image (.dmg) of your SD Card on the Desktop.

sudo dd if=/dev/disk2 of=~/Desktop/raspberrypi.dmg
Cloning the SD Card with a Terminal command
Cloning the SD Card with a Terminal command

Tip: You may be asked to enter your administrator password in order to proceed with this command. Be aware that no characters will be displayed as you type your password, so be aware of the keys you are pressing and be careful.

The process to clone the SD Card may take some time and you will not be shown any progress, in Terminal, whilst the SD Card is being copied. You will, however, be shown a message when the process is complete.

The copying process may take some time and will take longer for larger capacity SD Cards. To give you an idea, on my Mac mini Core i5, the process to backup an 8GB SD Card took 23 minutes.

Confirmation of completion
Confirmation of completion

Restoring Using the Command Line (CLI)

It is useful to create a master image of your SD Card from which you can replicate onto new SD Cards as and when required.

Or it can be used to restore an existing SD Card if it became corrupted or you wanted to revert to a previous state, for example.

Step 1. Insert the SD Card to be Restored

Insert a blank (or used and nuked) SD card into the SD card reader on your Mac.

Step 2. Locate the SD Card to be Restored

Open Terminal and locate your SD Card, noting that the number may be different to that previously used, using the command:

diskutil list
Locating the SD Card using a Terminal Command
Locating the SD Card using a Terminal Command

Step 3. Unmount the SD Card

In Terminal, enter the following command:

diskutil unmountDisk /dev/disk2
Unmounting the SD Card with a Terminal command
Unmounting the SD Card with a Terminal command

Step 3. Format the SD Card

When you have identified your SD Card, enter the following command to format it as FAT16, in my case it’s /dev/disk2. Amend this as required for your circumstances:

sudo newfs_msdos -F 16 /dev/disk2
Formatting the SD Card from the Terminal
Formatting the SD Card from the Terminal

Step 4. Restore from a Cloned Disc Image

Locate the disc image, dmg, that you previously cloned. My example assumes that the dmg is on the Desktop. In Terminal, enter the following command ensuring that you identify the correct destination disc, in my example it’s /dev/disk2.

sudo dd if=~/Desktop/raspberrypi.dmg of=/dev/disk2
Restoring a disc image to an SD Card
Restoring a disc image to an SD Card

Tip: Be aware that restoring the disc image to the SD Card can take some time. Probably a lot longer than you think. My Mac mini Core i5 took 3 hours 27 minutes to restore on an 8GB SDXC card.


What to do Before Deploying Your Clone

At any time you need to revert to a cloned SD card, don’t be hasty. Instead of swapping out the existing card with the clone, ensure that you re-clone the clone before using it.

This way, you will always have a version of your OS ready to go, just as you like it and without the need for a lengthy workflow in flashing, installing, upgrading and updating software.


Conclusion

In this tutorial I have shown you how to clone your Raspberry Pi SD Card using nothing more than the command line interface (CLI) in Terminal on OS X.

Cloning your SD cards can save a lot of time meaning that you can spend more time enjoying and experimenting with your Raspberry Pi.

Advertisement