How to Generate Secure, Cryptographically Random Passwords Using Command-Line Tools
10-Second Summary
Use openssl rand -base64 32 to generate a cryptographically secure 32-byte password instantly. For a customizable length without symbols, use pwgen -s 40 (40 characters, letters and numbers only). Both tools draw from cryptographically secure random sources, not predictable pseudo-random generators.
Step-by-Step Instructions
- Generate a password with OpenSSL (recommended for maximum portability).
Run the following command in your terminal:openssl rand -base64 32This produces a random sequence of 32 bytes encoded in Base64. The output will look similar toWkY+q7Z5YmiHTrpV0lwtSApbepky1BNWc1s1E7lFcpY=. The-base64flag ensures the output is printable text you can copy and paste. The32specifies the number of random bytes. OpenSSL uses the operating system’s cryptographically secure random number generator. - Generate a password with pwgen (if you need length control without symbols).
Ifpwgenis not installed, install it via your package manager (e.g.,sudo apt install pwgenon Debian/Ubuntu). Then run:pwgen -s 40The-sflag tellspwgento generate a completely random, secure password (letters and numbers only). The40is the desired password length. Sample output:hAZmJQZkAf44F3Zoazl7II8bv07e5ta2COmFQdjK. - Add symbols to pwgen output (optional).
Run:pwgen -s -y 40The-yflag includes symbols in the generated password. Sample output:i^^iAa5o^oc["+43[G]{62&S!&Mt:L]ny7qbR~g0. - Copy the output immediately to a secure location.
Do not leave generated passwords in your terminal history. Pipe the command directly to your clipboard if your system supports it, or paste the result into a password manager right away. For macOS, you can useopenssl rand -base64 32 | pbcopy. For Linux withxclip, useopenssl rand -base64 32 | xclip -selection clipboard.
FAQ / Quick Troubleshooting
- What if I don’t have
opensslorpwgeninstalled?opensslis available by default on most Unix-based systems (Linux, macOS, WSL). Ifpwgenis missing, install it via your package manager. Alternatively, you can usehead -c 32 /dev/urandom | base64as a fallback on Linux and macOS. - Can I use
/dev/urandomdirectly?
Yes. On Linux and macOS,/dev/urandomis a cryptographically secure random source. The commandhead -c 32 /dev/urandom | base64produces a 32-byte Base64-encoded password without requiring OpenSSL. However,openssl randis generally more portable and explicit about its cryptographic guarantees. - Why not use
dateor$RANDOMfor password generation?$RANDOMin Bash and time-based methods are not cryptographically secure. Their output is predictable and unsuitable for passwords. Always use a tool that explicitly draws from a CSPRNG (cryptographically secure pseudo-random number generator), such asopenssl randorpwgen -s.