Ubuntu – Error, even with sudo: “dd: failed to open ‘/dev/sda1’: Permission denied” (dd input piped from gzip)

14.04dddevelopmentgzipsudo

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.

Best Answer

You are missing sudo in the other side of the pipeline:

sudo gzip -dc sda1.image.gz | sudo dd of=/dev/sda1

In a <command> | <command> | [...] command format, each command of the pipeline which requires sudo should be run using sudo, not only the first one.

In this case you might not need to use sudo on gzip -dc sda1.image.gz, unless you don't have read permission on the file:

gzip -dc sda1.image.gz | sudo dd of=/dev/sda1

In general, if all the commands to be run in a pipeline require sudo, one way around having to write sudo in each command is to run the whole command in a subshell invoked using sudo:

sudo bash -c '<command> | <command> | [...]'
Related Question