Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to understand the output of the dd command. I tried

dd if=/dev/zero of=/dev/null bs=512 count=1

and got (as expected):

 1+0 records in
 1+0 records out
 512 bytes (512 B) copied, 2e-05 seconds, 26 MB/s

However when I tried

dd if=/dev/random of=/dev/null bs=512 count=1

I got

 0+1 records in
 0+1 records out
 128 bytes (128 B) copied, 0.00012 seconds, 1.1 MB/s

Why is it only copying 128 bytes?

share|improve this question
    
Ah thank you! Edited –  fo_x86 Dec 17 '12 at 19:16
    
See superuser.com/questions/359599/… for a more complete discussion of /dev/random and urandom –  BobT Dec 17 '12 at 19:29
add comment

2 Answers

up vote 8 down vote accepted

You need to use /dev/urandom, or the "unblocking" random source.

/dev/random uses a kind of entropy pool to increase the randomness of the bit source. This method will only return as many random bits/bytes as can be returned based on the entropy pool's state at the time, so if a hardware random number generator is used, this can sometimes be a constant. From the Linux manpage:

The generator also keeps an estimate of the number of bits of noise in the entropy pool. From this entropy pool random numbers are created.

The /dev/urandom file keeps reusing the internal pool as-is to generate a number as long as you need. The side-effect of this is: do not use /dev/urandom for cryptographic purposes, as it is less random than the bits produced by /dev/random. See the manpage link above for details.

share|improve this answer
add comment

Since reading /dev/random returns only the amount of bytes that is available, you have to specify block size 1. In your example, you set block size to 512 which fails after the first read.

Therefore, the correct arguments that reads exactly 512 bytes is:

dd if=/dev/random of=filename bs=1 count=512

Note the command will block until there's enough entropy in the system to generate all the data.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.