Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

That "sudo head" only works for reading, not writing though.

I use dd when redirect doesn't have permission.



sort by: page size:

I almost always use ‘dd’ even though there’s better options because it plays friendly with sudo, unlike pipes and io redirects.

Can you even use ‘>’ io redirects with sudo?


Redirect to a file is done by the shell, not by sudo process.

It even tells you that:

    > -bash: /dev/fb0: Permission denied
      ^^^^^

"Well why does the entire internet say to use dd then?"

Because you usually need root to write to drives, and you can't `sudo` redirect. `sudo dd if=whatever.img of=/dev/whatever` is nicer and easier than `sudo sh -c "cat whatever.img >/dev/whatever"`.


You can use head as stated in the article:

sudo dd if=/dev/sda of=/tmp/boot.1 bs=512 count=1

sudo head -c 512 /dev/sda > /tmp/boot.2

md5sum /tmp/boot.1

06d6f2aa3e7c33ad06282f70aa4e133b /tmp/boot.1

md5sum /tmp/boot.2

06d6f2aa3e7c33ad06282f70aa4e133b /tmp/boot.2


The reason why sudo wasn't working is that it runs the command with root privileges, but redirecting to a file with > is done through the shell, which is running under your normal user level privileges. If you want to redirect to a file that only root can write to, you can use the tee command. That command will write anything passed to it via stdin to the file given as an arg.

For example, echo "foo" | sudo tee /path/to/file

or any of the other ways listed here: https://stackoverflow.com/questions/82256/how-do-i-use-sudo-...


you can use: sudo !!

Use sudo or doas.

shell redirection does not work with sudo (at least with bash). You will get a permission denied with this command

Interesting that there is a sudo command. Unfortunately, it's nonfunctional :)

A good use case is to redirect output into a file you only can modify with sudo. Trying to do it like this doesn't work:

  sudo echo "fs.inotify.max_user_watches=1000000" > /etc/sysctl.conf
as the redirection happens within the shell, thus without root permissions. However, the following does work:

  echo "fs.inotify.max_user_watches=1000000" | sudo tee /etc/sysctl.conf
as the writing is done by the elevated tee process.

sudo?

I get really nervous running something with sudo that writes to my hosts file.

This is a really nice idea and I'd love to see it in another implementation.


There's one good (?) reason to use dd with devices: it specifies target in the same command. For devices, writing to them usually requires root privileges, so it's easy to:

    sudo dd .... of=/dev/...
But there's no trivial cat equivalent:

    sudo cat ... > target
Will open target as your current user anyway. You can play around with tee and redirection of course. But that's getting more complicated than the original.

or sudo

If I have to use sudo, I always:

  sudo -i
  # do root work
  exit # or ^D

Sudo.

You can also just use `su`, run your command, then `exit`, but you’re right: `sudo dd` is much easier.

(This obviously ignores that you should almost never use `su`)


This is all pretty basic stuff. One thing that always bites me is trying to use sudo to redirect to a protected file. This doesn't work, because the shell is doing the redirection, not sudo:

    sudo cat ~/something.txt | grep 'hello' > ~someoneelse/result.txt
You can get around this by using sudo to run a new shell that does the redirection:

    sudo sh -c "cat ~/something.txt | grep 'hello' > ~someoneelse/result.txt"

It doesn't prevent you from using sudo if you've set NOPASSWD: for the appropriate commands in your sudoers file.
next

Legal | privacy