4 ms·
> I wish that all of those projects, tutorials etc. that explain how to write their image to a block device, like an sdcard, would start advise using cat, becau
by RJIb8RBYxzAMX9u 10y ago
> I wish that all of those projects, tutorials etc. that explain how to write their image to a block device, like an sdcard, would start advise using cat, because there is no reason to use dd, it's just something that people stick with because others do it too.
I'd wondered whether dd or cat were faster, and indeed cat is faster, but not by much. Also, for some embedded devices, you have to write to specific offsets, so dd is more convenient and explicit. Lastly, cat composes poorly with sudo.
$ sudo dd if=foobar.img of=/dev/sdi # works
$ sudo cat foobar.img > /dev/sdi # fails unless root b/c redirection is done by shell
> I only use dd for specific blocks, like writing back a backup of the mbr, or as a rudimentary hex editor.
xxd / xxd -r is much nicer, but I suppose sometimes vim is not available...
- greggyb 10y agosudo (cat foobar.img > /dev/sdi) No?
- nerdponx 10y agoWould sudo (cat foobar.img > /dev/sdi) Work?
- RJIb8RBYxzAMX9u 10y agoNo, unless you have a shell where sudo is a built-in, and I don't know of any.
- c0l0 10y agoThis is actually impossible (at least with UNIX shells), as shell builtins cannot have the SUID bit set - and your shellitself also shouldn't ;)
- JoshTriplett 10y agoYou could have a shell with a "sudo" builtin that knew how to invoke a separate "sudo" program with the right shell syntax and quoting, such that "sudo somecommand > /path/to/root-writable-file" did the right thing.
- abofh 10y agoNot really - to sudo, your shell would have to be setuid - and constantly fork stuff as you to get user permissions. Alternatively your shell could maintain a separate process for privileged access, but that puts a whole lot of your security on the assumption that your shell has no bugs that might allow escalation. In short, you could do it, but it'd be ripped out of every server that's been hardened, and for users that don't want to care - they're just running 'sudo su' anyhow. Speaking only for myself, the thought of my shell having a magical escalation process would scare the bejeezus out of me - and I'm supposed to have root on our boxes!
- TheCoelacanth 10y agoThe shell wouldn't need to be setuid if it just performed a syntax transformation and then called the existing sudo binary.
- JoshTriplett 10y agoExactly. The shell would just treat "sudo" as a builtin prefix similar to "time", but would then run the real "sudo" with an appropriate shell invocation and proper quoting.
- deleted 10y ago[deleted]
- sply 10y agoNo, you should use sudo sh -c "cat foobar.img > /dev/sdi" or sudo -s "cat foobar.img > /dev/sdi"
- RJIb8RBYxzAMX9u 10y agoI was originally going to put that in my examples, but opted to leave it out, because with sh -c you have to think about escaping special characters. Most of the time it doesn't matter, but when running commands as root you ought to be absolutely sure.
- nerdponx 10y agoWhat about using a here-doc (as in http://stackoverflow.com/a/16514624/2954547 http://stackoverflow.com/a/16514624/2954547)? sudo sh <<EOF cat foobar.img > /dev/sdi EOF
- deleted 10y ago[deleted]
- mercora 10y agoone could use: cat foobar.img | sudo tee /dev/sdi > /dev/null
- JoshTriplett 10y agoI've used that to write short files (such as settings in /sys or /proc), but for large files, tee has the disadvantage of writing everything twice, and the pipe adds another write and read of every byte.
- mercora 10y agoI just realized this too and tried to close stdout instead. tee complains about it but goes on with its business: cat foobar.img | sudo tee /dev/sdi >&-
- RJIb8RBYxzAMX9u 10y agoWhy's everyone so against dd? :-) If you're going to use tee, might as well not bother with cat at all[0]: $ sudo tee /dev/sdi >&- < foobar.img Or better yet, pv[1]; you even get a progress bar that way! [0] https://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat https://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_... [1] http://www.ivarch.com/programs/pv.shtml http://www.ivarch.com/programs/pv.shtml
- mercora 10y agoThanks for the reminder :) I do fall quite often for the useless use of cat. But most of the time i also do not really care about it much. I did omit pv in believe tee will always be available but it is great and absolutely preferred when available.
- JoshTriplett 10y ago> I'd wondered whether dd or cat were faster, and indeed cat is faster, but not by much. That performance difference often comes from block size; "dd bs=1M" typically runs much faster than the default block size of 512 bytes.
- carussell 10y ago> xxd / xxd -r is much nicer, but I suppose sometimes vim is not available `od` is ubiquitous--it's POSIX and a requirement of the Single UNIX Specification
- na85 10y agoSudo is anachronistic anyways. Laptops don't need to follow multiuser best practices, and frankly a complex root password offers little.
- deleted 10y ago[deleted]