I am running the following command on an ubuntu system:
dd if=/dev/random of=rand bs=1K count=2
However, every time I run it, I end up with a file of a different size. Why is this? How can I generate a file of a given size filled with random data?
Best Answer
You're observing a combination of the peculiar behavior of
dd
with the peculiar behavior of Linux's/dev/random
. Both, by the way, are rarely the right tool for the job.Linux's
/dev/random
returns data sparingly. It is based on the assumption that the entropy in the pseudorandom number generator is extinguished at a very fast rate. Since gathering new entropy is slow,/dev/random
typically relinquishes only a few bytes at a time.dd
is an old, cranky program initially intended to operate on tape devices. When you tell it to read one block of 1kB, it attempts to read one block. If the read returns less than 1024 bytes, tough, that's all you get. Sodd if=/dev/random bs=1K count=2
makes tworead(2)
calls. Since it's reading from/dev/random
, the tworead
calls typically return only a few bytes, in varying number depending on the available entropy. See also When is dd suitable for copying data? (or, when are read() and write() partial)Unless you're designing an OS installer or cloner, you should never use
/dev/random
under Linux, always/dev/urandom
. Theurandom
man page is somewhat misleading;/dev/urandom
is in fact suitable for cryptography, even to generate long-lived keys. The only restriction with/dev/urandom
is that it must be supplied with sufficient entropy; Linux distributions normally save the entropy between reboots, so the only time you might not have enough entropy is on a fresh installation. Entropy does not wear off in practical terms. For more information, read Is a rand from /dev/urandom secure for a login key? and Feeding /dev/random entropy pool?.Most uses of
dd
are better expressed with tools such ashead
ortail
. If you want 2kB of random bytes, runWith older Linux kernels, you could get away with
because
/dev/urandom
happily returned as many bytes as requested. But this is no longer true since kernel 3.16, it's now limited to 32MB.In general, when you need to use
dd
to extract a fixed number of bytes and its input is not coming from a regular file or block device, you need to read byte by byte:dd bs=1 count=2048
.