Linux command
random 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Read 16 random bytes
head -c 16 /dev/urandom
Generate a random hexadecimal string
head -c 16 /dev/urandom | xxd -p
Generate a random base64 string
head -c 32 /dev/urandom | base64
Read from /dev/random
dd if=/dev/random bs=1 count=32
Check available entropy
cat /proc/sys/kernel/random/entropy_avail
说明
/dev/random and /dev/urandom are special character device files that provide access to the Linux kernel's cryptographically secure random number generator. They gather environmental noise from device drivers, hardware interrupts, and other sources into an entropy pool. /dev/urandom (unlimited random) is the preferred source for most applications. It returns random bytes immediately without blocking, using a CSPRNG that reseeds from the entropy pool. It is suitable for encryption keys, session tokens, and general randomness needs. /dev/random blocks when the entropy pool is estimated to be insufficient for the requested amount of randomness. This was historically considered more secure but is rarely necessary on modern systems. It may cause applications to hang waiting for entropy. Both devices provide output suitable for cryptographic purposes on properly functioning systems.
FAQ
What is the random command used for?
/dev/random and /dev/urandom are special character device files that provide access to the Linux kernel's cryptographically secure random number generator. They gather environmental noise from device drivers, hardware interrupts, and other sources into an entropy pool. /dev/urandom (unlimited random) is the preferred source for most applications. It returns random bytes immediately without blocking, using a CSPRNG that reseeds from the entropy pool. It is suitable for encryption keys, session tokens, and general randomness needs. /dev/random blocks when the entropy pool is estimated to be insufficient for the requested amount of randomness. This was historically considered more secure but is rarely necessary on modern systems. It may cause applications to hang waiting for entropy. Both devices provide output suitable for cryptographic purposes on properly functioning systems.
How do I run a basic random example?
Run `head -c 16 /dev/urandom` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
Where can I find more random examples?
This page includes 5 examples for random, plus related commands for nearby Linux tasks.