
In Unix-like operating systems, including Ubuntu, /dev/random
and /dev/urandom
are special files that serve as cryptographically secure random number generators. They allow you to generate random data, which can be used for various purposes, including creating passwords, cryptographic keys, and more. This article will guide you on how to use these files effectively.
To use /dev/random
and /dev/urandom
on Ubuntu and other Unix-like systems, you can read from these special files like any other file. Use commands like dd
or head
to read a specific number of bytes from /dev/urandom
and save them to a file. There is no need to run /dev/(u)random
as a command, and normal users can read from these files without root privileges. /dev/random
is suitable for high-quality randomness, while /dev/urandom
is more efficient for most applications. Alternatives to these files include the RANDOM
environment variable and the shuf
command.
Understanding /dev/random and /dev/urandom
/dev/random
and /dev/urandom
are both random number generators, but they function slightly differently:
/dev/random
: It generates random numbers from the environmental noise collected from device drivers and other sources. However, it can block the operation if it determines that there is not enough entropy (randomness) to generate high-quality random numbers./dev/urandom
: It also generates random numbers from environmental noise, but it does not block operations. Instead, it reuses the internal pool to produce more pseudo-random numbers. This makes it more suitable for most applications.
Reading from /dev/(u)random
You can read from /dev/random
or /dev/urandom
like you would from any other file. Here are some examples:
- To read 1024 bytes from
/dev/urandom
and save it to a file, you can use thedd
command:
Here,dd if=/dev/urandom of=~/urandom_test count=4 bs=1024
if
stands for “input file”,of
stands for “output file”,count
is the number of blocks to be copied, andbs
is the block size. In this case, we are copying 4 blocks of 1024 bytes each. - To read a specific number of bytes, you can use the
head
command:
Here,head -c 30 /dev/urandom > random.bytes
-c
specifies the number of bytes. This command reads 30 bytes from/dev/urandom
and redirects the output to a file namedrandom.bytes
.
Permissions and Running as Root
There is no need to execute /dev/(u)random
as a command. Instead, you should read from it as a file. Also, you don’t need root privileges to read from /dev/(u)random
. Normal users can read from it without any issues.
When to Use /dev/(u)random and /dev/urandom
/dev/random
is suitable when you need very high-quality randomness, such as for generating long-term cryptographic keys. However, for most other purposes, /dev/urandom
is sufficient and more efficient, as it does not block operations waiting for more entropy.
Alternatives to /dev/(u)random
For generating random numbers, you can also use the shell environment variable RANDOM
or the shuf
command. For example, echo $RANDOM
generates a random positive integer, and shuf -i 1-100 -n 1
generates a random number between 1 and 100.
Conclusion
/dev/random
and /dev/urandom
are powerful tools for generating random data on Unix-like systems, including Ubuntu. By understanding how to use these files and when to use each one, you can ensure that your applications have access to high-quality random numbers whenever they need them.
/dev/random
blocks the operation if there is not enough entropy to generate high-quality random numbers, while /dev/urandom
does not block and reuses the internal pool to produce more pseudo-random numbers.
You can read from /dev/(u)random
like you would from any other file. For example, you can use the dd
command or the head
command to read a specific number of bytes.
No, you do not need root privileges to read from /dev/(u)random
. Normal users can read from it without any issues.
/dev/random
is suitable for generating very high-quality randomness, such as for long-term cryptographic keys. However, for most other purposes, /dev/urandom
is sufficient and more efficient as it does not block operations waiting for more entropy.
Yes, you can use the shell environment variable RANDOM
or the shuf
command to generate random numbers. For example, echo $RANDOM
generates a random positive integer, and shuf -i 1-100 -n 1
generates a random number between 1 and 100.