Why does reading /dev/random byte by byte block so often?

linux, random, shell

Solution

The answer lies in your question:

49 bytes (49 B) copied, 0.000134028 s, 366 kB/s

So it didn't copy 1024 bytes like told it but only a few and then stops. I quess this is the same amount that you would have gotten in the loop before it blocks.

/dev/random is slow because it needs to collect the randomness from different sources and as long as none is available it doesn't output anything.

Use /dev/urandom if you need faster numbers.

Problem

The following call returns fast: ``` time dd if=/dev/random bs=1024 count=1 .... 0+1 records in 0+1 records out 49 bytes (49 B) copied, 0.000134028 s, 366 kB/s real 0m0.004s user 0m0.001s sys 0m0.002s ``` However, if `/dev/random` is read one byte after another: ``` for i in {1..500}; do dd if=/dev/random bs=1 count=1 status=none; done ``` The loop reads several bytes, then block for several seconds, and then read another several bytes. Typing random characters on keyboard speeds up the process a lot, like if there was not enough entropy in the random pool. After all the loop takes many minutes to complete. What makes reading `/dev/random` byte by byte a lot slower than reading a block from it? Uname -a: ``` Linux ... 2.6.32-431.11.2.el6.centos.plus.x86_64 ```

Original source