My command which doesn't work:
sudo gzip -dc sda1.image.gz | dd of=/dev/sda1
returns the following error even before I've had a chance to enter my password:
dd: failed to open ‘/dev/sda1’: Permission denied
[sudo] password for ken:
I've also tried without the "-dc" options and get the same error.
However, the dd command without gzip, using an uncompressed file, does work:
sudo dd if=sda1.image of=/dev/sda1
It seems like the sudo is only applying to the first command and not the entire sequence of commands. If I remain in the same terminal session and repeat the command, I don't get the password prompt (my authentication seems to persist) and yet I still get the same error (as if my authentication is not applying to the /dev write operation). The same error occurs when executed from a /bin/sh script.
How should I construct my command(s) to uncompress my image to the device?
I'm using Ubuntu 14.04 LTS in a terminal window.
You are missing
sudo
in the other side of the pipeline:In a
<command> | <command> | [...]
command format, each command of the pipeline which requiressudo
should be run usingsudo
, not only the first one.In this case you might not need to use
sudo
ongzip -dc sda1.image.gz
, unless you don't have read permission on the file:In general, if all the commands to be run in a pipeline require
sudo
, one way around having to writesudo
in each command is to run the whole command in a subshell invoked usingsudo
:Found this question and answer a few years later and it helped me resolve the same issue.
For future situations, it might be useful to save the command used in creating the image to a text file, along with the command needed to move the image back to another partition.
For example:
sudo dd if=/dev/mmcblk0 | gzip > YYYYMMDD_Rasp4_Backup.img.gz
to move to a new partition:
sudo cat YYYYMMDD_Rasp4_Backup.img.gz | sudo gunzip | sudo dd of=/dev/sdb
Save those commands in a text file alongside the .img.gz file as a reminder.