The following sections describe ways, depending on what tools are available, to generate random primitives on the command line such as numbers or strings.
We define primitives as primitive types that might be one-level up in terms of specialization; for instance, an UUID is just a hexadecimal string, but in this definition it is considered a primitive type. The same applies to MD5.
Command | Description |
---|---|
$((RANDOM)) | a bashism: a random number in the interval ![]() ![]() |
$(( RANDOM % (10 - 5 + 1 ) + 5 )) | a random number in the interval ![]() |
shuf –random-source='/dev/random' -i 1-100 -n 1 | generates one (-n 1 ) random number in the interval ![]() -i 1-100 ) |
jot -r 1 1 100 | generate one (-r 1 ) random number in the interval ![]() |
awk -v min=5 -v max=10 'BEGIN{srand(); print int(min + rand() * (max - min + 1))} ' | generates a random number between ![]() ![]() -v min=5 -v max=10 ) using a random number generator seeded by time (srand() ) and then clamping the random number of a range |
tr -cd '[:digit:]' < /dev/random | fold -w 11 | head -n 1 | one (head -n 1 ) 11 -digit long (fold -w 11 ) number |
/dev/random
or /dev/urandom
has the advantage of using a larger entropy pool provided by Linux, and, where available, a device like onerng can be used as a source of true entropy. Command | Description |
---|---|
cat /proc/sys/kernel/random/uuid | a random UUIDv4 from the kernel |
uuidgen | found in package uuid-runtime on Debian and derivatives |
uuid | another UUID generating package |
Command | Description |
---|---|
dd if=/dev/random status=none count=1 bs=512 | md5sum | generates a random MD5 hash with ![]() bs=512 ) of entropy from /dev/random |
Command | Description |
---|---|
tr -dc 'A-Za-z0-9' < /dev/random | fold -w 5 | head -n 1 | generate one ( head -n 1 ) random string of length 5 (fold -w 5 ) using the character class A-Za-z0-9 (-dc A-Za-z0-9 ) |
tr -dc '[:graph:]' < /dev/random | head -c 10 | generate ![]() -c 10 ) of printable characters except space ([:graph:] ) |
shuf -er -n20 {A..Z} {a..z} {0..9} | paste -d'\0' -s - | using Bourne shell character sequences; generates a 20 character long (-n20 ) mixed-case alpha-numeric ({A..Z} {a..z} {0..9} ) password |
openssl rand 99999 | tr -dc '[:alnum:]' | head -c 5 | generates a 5 -character long alpha-numeric ([:alnum:] ) password (head -c 5 )((openssl is just used to gather entropy and much of that entropy (99999 bytes) will not fall within the alphabet selected by tr such that it is not clear how many bytes to select) |
openssl rand -base64 12 or openssl rand -hex 12 | openssl can generate random strings but they will either be base64 or hexadecimal |