Don't use dd

Thursday, Dec 10, 2020
#unix #tips

Most new (and some old) linux users know dd for one purpose. That is, of course, creating a bootable flashdrive with a linux iso. Many of the most popular guides and tutorials online show something like dd if=file.iso of=/dev/sdx && sync as if that’s the only way you can put an iso on a flashdrive. It technically will work, but there are better ways to copy a file on linux. For example, cp file.iso /dev/sdx will do the same thing and you don’t need to worry about block size or any of that nonsense, since cp is actually meant for copying files.

Basically anything that will read/write a file can be used:

cp file.iso /dev/sdx
cat file.iso > /dev/sdx
tee < file.iso > /dev/sdx
pv file.iso > /dev/sdx

Contrary to popular belief dd doesn’t stand for “disk duplicator” or anything similar. In fact, it doesn’t know anything about disks and the only reason it can interact with block devices is because linux presents drives and partitions as simple files under /dev/. Using dd for this purpose can actually be a bad idea because of its unwieldy options that are easy to typo (i and o are next to each other on the keyboard). When you’re working with sudo and block devices a tiny typo could mean wiping your drive.

dd is meant to be used for more specific read and write operations – like this little command that will generate a random password for you by reading 30 bytes from /dev/urandom and then converting the binary output to base64.

dd if=/dev/urandom count=1 bs=30 | base64

My personal favourite tool is pv, which is fairly standard, but not very well known. It can be used in a pipeline to display progress and can conveniently read a file like in pv file.iso > /dev/sdx.

Update - 2020/02/01

Changed the example dd command to use a byte size of 30 instead of 20. This allows base64 to create a clean password without a padding character at the end. Henry pointed this out in my public mailing list. Thanks!