LinuxCommandLibrary

rand.1s

Generate cryptographically secure random bytes

TLDR

Generate 32 random bytes in hexadecimal

$ openssl rand -hex 32
copy
Generate 32 random bytes in base64 encoding
$ openssl rand -base64 32
copy
Generate raw random bytes and write to a file
$ openssl rand -out [random.bin] 256
copy
Generate a random password (24 base64 characters)
$ openssl rand -base64 18
copy
Generate 1 kilobyte of random data
$ openssl rand -out [random.bin] 1K
copy

SYNOPSIS

openssl rand [-hex] [-base64] [-out file] num

DESCRIPTION

openssl rand generates cryptographically secure pseudo-random bytes using OpenSSL's CSPRNG (Cryptographically Secure Pseudo-Random Number Generator). The output provides 256 bits of security when properly seeded from the operating system's entropy source.
The command is commonly used to generate random passwords, encryption keys, initialization vectors, and other security tokens. Without encoding options, raw binary bytes are output, which may not display properly in terminals.
On modern operating systems, OpenSSL automatically seeds from trusted system entropy sources (/dev/urandom on Unix-like systems). The command fails if sufficient entropy is unavailable.

PARAMETERS

-hex

Output random bytes as hexadecimal string (2 characters per byte)
-base64
Output random bytes encoded in base64
-out file
Write output to file instead of standard output
-rand file
Use specified file(s) as additional random seed source
-help
Display usage information
num
Number of random bytes to generate (supports K/M/G/T suffixes)

CAVEATS

Raw output (without -hex or -base64) contains binary data that may include non-printable characters. Pipe through encoding for terminal display or shell usage.
The number of output characters differs from the number of bytes: hex output is 2x the byte count, base64 output is approximately 4/3x the byte count (plus padding).
For password generation, base64 encoding includes characters (+, /, =) that may need escaping in some contexts.

SEE ALSO

openssl(1), dd(1), head(1), urandom(4)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community