Chris Dzombak

sharing preview • dzombak.com

Cloning Raspberry Pi SD cards

Cloning Raspberry Pi SD cards

Occasionally I need to set up a new Raspberry Pi that’s extremely similar to an existing Pi, typically for a monitoring application like collecting UPS statistics.

The hard way to do this is to set up the new Pi from scratch and install and configure your application.

The easy way is by cloning the existing Pi’s microSD card to a new card, booting it, and making a few small changes. All you need is a second microSD card of equal or greater capacity than the source Pi’s card.

As with all my Raspberry Pi posts, myriad things can go wrong in this process, and you should proceed only if you fully understand each step.

1. Clone the SD card

The first step is actually cloning the microSD card. With two microSD card readers attached to your machine, balenaEtcher makes this very easy. Or, on macOS or Linux, experienced users can use the dd tool to clone the card.

Of course, be certain you’re cloning to the correct drive. The last thing you want to do is overwrite the source Pi’s microSD card, or worse, accidentally destroy one of your desktop’s drives!

The interesting part — the reason I’m writing this post — is what you need to do after you install the cloned card in a new Pi and boot it.

2. SSH in

Boot the new Pi and SSH into it.

Be sure you’re SSHing into the new Pi; you may want to use your network router’s tooling to watch for new devices connected to the network to discover the new Pi’s IP. It may be a good idea to keep the source Pi offline while you do this to avoid confusion.

2a. If the Pi is configured to use a static IP instead of DHCP, you’ll need to keep the source Pi offline and adjust the network configuration on the new Pi. I use DHCP for absolutely everything, so I can’t provide specific instructions here.

When you SSH into the new Pi, you will probably get a warning about the SSH host key being reused. In this case, you can safely ignore the warning; we’ll solve this problem later.

3. Change the hostname

Setting the new Pi’s hostname takes two straightforward commands:

sudo hostnamectl set-hostname mynewpi.lan
sudo hostnamectl --pretty set-hostname mynewpi

4. Change the SSH host key

We need to give the new Pi its own SSH host key. To do that:

sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-server

5. Address potential conflicts with the source Pi (e.g. backups)

We’ve changed network settings, the hostname, and the SSH host key already. At this point you need to consider: have I installed anything else on this Pi that needs to be reconfigured?

As an example from my personal use case, I need to set up automatic backups on this new Pi to write to a new repository to make sure they don’t conflict/aren’t confused with backups from the original source Pi.

6. Reconfigure your application

Finally, it’s time to reconfigure your intended application for this new Pi.

In this use case, this means I need to reconfigure NUT for the UPS the new Pi is connected to and reconfigure nut_influx_connector to write to Influx with the correct metadata.