/dev/urandom
uses a CSPRNG (Chacha20 last time I checked) to generate a stream of random data from an initial seed and allows the user to read an arbitrary number of bits from it. /dev/random
pulls data out of a random pool that needs to be replenished.
It is possible to read from /dev/urandom
before the system has enough entropy to provide a secure stream of data, in which case the kernel log should show a warning about an uninitialized read from it. If this is a concern, for example in embedded systems, then you'll need to use a hardware random number generator to generate additional entropy before you let programs start using it.
Provided the system gathered enough entropy, you'll get cryptographically secure random data from urandom
and information theoretically secure data from random
. If you're using a non-information-theoretically-secure encryption scheme or you're not generating vast quantities of encryption keys, then you might as well just use urandom
.
You shouldn't read from /dev/random
or /dev/urandom
directly since the kernel exposes a nice getrandom
system call that you can use in your programs, and it will help you avoid some pitfalls when trying to read from the character devices directly.
This answer has additional details about the differences between the two RNG devices.