Sometimes you want to reinstall your Raspberry Pi without switching the SD-card or via remote since it is located on another physical location. Here is a solution to reinstall the operating system remotely.
Installation
1. First you must activate the magic SysRq option:
1 |
echo 1 > /proc/sys/kernel/sysrq |
2a. Transfer the image and write it to SD:
1 |
ssh user@host "dd if=2016-05-27-raspbian-jessie.zip bs=4M" | pv | funzip | dd bs=4M of=/dev/mmcblk0 |
- Login via SSH to the system on which the image is located and read it via dd. The stdout is tunneled through SSH to the local system.
- Using pv you can see how much data is transferred. (You may need to install it with apt-get, but you can also omit this.)
- funzip extracts the first file and sends it to stdout.
- dd writes it to the SD-card.
2b. You can also pull the image direct from the Internet:
1 |
curl -L http://downloads.raspberrypi.org/raspbian_lite_latest | funzip | dd bs=4M of=/dev/mmcblk0 |
3. Reset the Raspberry:
1 |
echo b > /proc/sysrq-trigger |
Important
There are many ways how to transfer an image over the network. The methods described here pull the image from a remote location. After the transfer process has been completed, you are still logged in. With other ways, if you are logged out, you are lost because you cannot log in any more…
Be sure that you have DHCP enabled on the network.
Good luck. ;)
Links
Featured image “raspberry pi” by Clive Darra is licensed under CC BY-SA 2.0.
I know this is an older entry..
But seems the best so far, I am about to start this..
You are installing this over a network because the Pi is in another, remote location..
How do you access it after resetting it?
When I write the image locally I have to “touch ssh” in the boot partition..
I am not seeing that step in your instructions..? Am I missing something?
This article was created before the Raspberry Pi Foundation decided to block SSH on all new installs.. Personally, I strongly feel that -lite- should have SSH enabled.. The average 1D10T user isn’t going to be using -lite- to worry about not changing the default password..
I use (and modified) this a little bit
http://www.aoakley.com/articles/2016-12-05-raspbian-enable-ssh.php
Then I put it on a web server and use 2b.. Zipped or unzipped (remove the funzip if it is already unzipped)
important!!!
don’t forget new releases of raspbian come with ssh blocked; if you’re doing this in headless mode active ssh before dd
I like to create a new image of my RPi after I’ve configured it to my needs, i.e. enable ssh, setup samba, setup dhcp, dns, etc.
Now, if you need to re-image the SD card, it’ll be exactly how you want it.
Example to create image:
sudo dd bs=4M if=/dev/sdb of=raspbian.img
Keep in mind that once re-installed, ssh is by default disabled!
curl -L http://downloads.raspberrypi.org/raspbian_lite_latest | funzip | dd bs=4M of=/dev/mmcblk0
Worst method ever :( My image was downloaded 75% only, I didn’t see that and rebooted the raspberry pi. Guess what happen after? ;)
Jenifer,
By doing just that, you won’t have remote access after it reboots anyway… See the other comments about enabling SSH on a new image before installing.
Not the worst method ever.. This is my primary way of re-installing a Pi board.. I typically host the image file myself or save it to the Pi board if it has a big enough SD card, so one isn’t dependant on the internet/external servers for the image.
There are more complicated ways of checking the image download before it gets written too.
There is an error in the guide
This does not work:
curl -L http://downloads.raspberrypi.org/raspbian_lite_latest | funzip | dd bs=4M of=/dev/mmcblk0
funzip error: invalid compressed data–length error
and the card will be unusable
Does this tutorial still work or not?
it was working 1 or 2 times for me. I did not changed anything, but since than I was not able to get it working again. And I always end up with a system not able to boot (kernel panic)
So this is not working
Just try it out. Maybe I’m doing something wrong. Would be good to know
No,
The 2020 images changed the ‘boot’ directory size, so the offsets need to be adjusted.
Personally, it is on my todo list, but still further down the list.
Hopefully it gets up your list :-)
I tried many tricks, but it didn’t worked.
Many thanks for this guide. It’s working quite well. Just one question. I know it’s require a ZIP file but would it be possible to use similar with a 7z file?
Thanks for the guide!
This actually still works for me with the latest raspbian lite.
Just make sure you put the ssh file in the img and run the command without funzip if you use a .img directly like i did
Interesting.. I’ll have to try this again..
I wonder what changed (again) that it works now.
The partition size of the -boot- image changed which messed up the script, so I am curious how/why it worked for you.
Never mind…
This article has been edited..
It used to have instructions to add the ‘ssh’ file in the boot partition.
As it stand now, reinstalling over the network is a lot less useful because you can’t regain access to your newly imaged Pi.
Hi Kevin,
I reinstalled over the network and regained access over ssh.
i used an unzipped version of the raspbian image, opened the image by mounting it, then added an empty “ssh” file in the root directory of that image. Then i uploaded that changed image to a webserver and used method 2b from there.
Regards,
Lars
I confirm this working again. Tested on Rpi3B+ with 2020-02-13-raspbian-buster-lite. And if you like it extremely lightweight, DietPi_RPi-ARMv6-Bullseye is working as well.
Hi Joulinar,
I’m following the same steps to re-flash my Raspberry with Raspberry pi desktop, but after completion when I’m resetting it, then on boot it’s kernel is getting panicked.
directions seem to be missing Step 0:
sudo su root
And it doesn’t boot up. Kernel panic.
Your blog post seems to be the only place on the internet that seriously tries to tell people how to reinstall the OS without pulling the SD card out. Too bad it currently isn’t working.
A little more explanation about unpacking the archives would also be nice since funzip doesn’t work with “Raspberry Pi OS (Legacy)” (it’s in .xz format). Maybe just an entire step where you get the image file copied to the local drive and all unpacked instead of trying to send it directly to /dev/mmcblk0 in one step?
It seems I will be digging the SD card out now and installing it the stupid way, but you should totally update and expand on this topic. This blog post is an island of really useful information in a sea of low-quality posts about how to install the OS exactly the way the official directions from Raspberry Pi Foundation clearly tell you to: by pulling the SD card out like a freaking neanderthal.
thanks a lot for trying. please try again. the internet needs you.
I experimented with this tonight..
mount -o loop,offset=4194304 2020-02-13-raspbian-buster-lite.img ~/mnt
cd /mnt
touch ssh
vi config.txt
cd ..
umount ~/mnt
dd if=/root/2020-02-13-raspbian-buster-lite.img of=/dev/mmcblk0 bs=4M status=progress
In a separate SSH session, before starting dd
echo 1 > /proc/sys/kernel/sysrq
After dd was finished,
echo b > /proc/sysrq-trigger
It closed my SSH session the moment dd was finished, I was glad to have a second terminal running.
I don’t know which newer versions would work, but downloading and using the latest image does kernel panic.
Using 2020-02-13 worked.
In the script above, just after “vi config.txt” , don’t forget to also modify the file “wpa_supplicant.conf” with your wifi credentials (or copy the existing one), if the Pi is not connected by cable.
Don’t ask me how I figured out.
;)
thanks for figuring that out, bro!