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

35,587

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> | [...]'
Share:
35,587

Related videos on Youtube

Ken Conrad
Author by

Ken Conrad

Updated on September 18, 2022

Comments

  • Ken Conrad
    Ken Conrad over 1 year

    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.

  • kos
    kos about 9 years
    @KenConrad If this answer solved your problem, please mark it as the accepted answer by clicking on the mark under the upvotes count, so that other users may know that this answer worked for you and may possibly profit from it as well.
  • Marc Vanhoomissen
    Marc Vanhoomissen about 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review