Generate Random Passwords from Linux Command Line
In this article
Yes, you can generate strong random passwords directly from the Linux command line using built-in tools like /dev/urandom, openssl, or gpg. No extra software is needed for most distributions. This guide shows you several reliable methods, how to customize length and character sets, and how to avoid common pitfalls.
What You'll Need
- A Linux terminal (any distribution).
- Basic familiarity with running commands.
- Optional:
opensslorgpgif you prefer those tools (most systems have at least one).
Step-by-Step
Method 1: Using /dev/urandom (Always Available)
- Open a terminal.
- Generate a 16-character password using
/dev/urandomand filter out unwanted characters:tr -dc 'A-Za-z0-9!@#$%^&*()_+-=' < /dev/urandom | head -c 16; echo - To change the length, replace
16with your desired number (e.g.,32). - To include only alphanumeric characters, use
'A-Za-z0-9'instead. - To generate multiple passwords at once, wrap the command in a loop:
for i in {1..5}; do tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16; echo; done
Method 2: Using openssl (If Installed)
- Check if
opensslis available by runningwhich openssl. If it returns a path, proceed. - Generate a 16-character password with:
This outputs 16 characters (12 bytes encoded as base64). Adjust the byte count:openssl rand -base64 12-base64 16gives 22 characters,-base64 24gives 32. - To exclude characters like
+/, you can pipe throughtr:openssl rand -base64 12 | tr -d '+/='
Method 3: Using gpg (If Installed)
- Check for
gpgwithwhich gpg. - Generate a random password with:
This outputs 12 random bytes in base64. Changegpg --gen-random --armor 1 1212to your desired byte length.
Method 4: Using apg (If Installed)
- Install
apgif not present (e.g.,sudo apt install apgon Debian/Ubuntu). - Run
apg -a 1 -M NCl -n 5to generate 5 passwords with mixed case, numbers, and special characters. - Options:
-a 1uses random algorithm,-M NClensures at least one number, capital, and lowercase letter.
Creating a Reusable Function
- Add a function to your shell profile (e.g.,
~/.bashrc) for quick access: - Open the file with
nano ~/.bashrc. - Append:
This creates agenpass() { tr -dc 'A-Za-z0-9!@#$%^&*()_+-=' < /dev/urandom | head -c ${1:-16}; echo; }genpasscommand that defaults to 16 characters, or you can pass a length:genpass 24. - Save and reload with
source ~/.bashrc.
Common Mistakes
- Using
headwithouttr:head -c 16 /dev/urandomoutputs raw binary, which may contain non-printable characters or newlines. Always filter withtr. - Not including enough character classes: A password with only lowercase letters is weak. Use uppercase, numbers, and symbols.
- Using
dateor$$as a seed: These are predictable and not cryptographically secure. Stick to/dev/urandomoropenssl. - Forgetting to strip newlines: The
echoat the end adds a newline for readability, but if you pipe the password directly into a command, you might inadvertently include a newline. Usetr -d '\n'if needed.
Tips
- For maximum security, use at least 16 characters; 20+ is better for critical accounts.
- If you need to avoid ambiguous characters (like
0/Oor1/l/I), filter them out withtr -d '0O1lI'. - Store generated passwords in a password manager rather than in plain text files.
- Use
openssl rand -hex 16for a hex string (32 characters) if you need a format that is easy to type. - Combine methods: generate a long passphrase using
apgorpwgen(if installed) for easier memorization.
Troubleshooting
- Command not found:
trorhead? These are coreutils and should exist on any Linux system. If missing, install coreutils via your package manager. openssl randfails with “unable to load config”? This usually means OpenSSL is not properly configured. Tryopenssl rand -base64 12with-rand /dev/urandomor use/dev/urandommethod instead.- Password contains spaces or special characters that break scripts? Wrap the password in quotes when using it, or use
openssl rand -hexto get only hexadecimal characters. - Generating a very long password takes time?
/dev/urandomis fast, but if you usetrwith a largehead -c, it may take a moment. It’s fine for up to a few hundred characters. - I get an error about “invalid byte sequence” when using
tr? This happens if your locale expects UTF-8 and the random bytes are not valid UTF-8. UseLC_ALL=C tr -dc ...to force byte-wise operation.
Frequently Asked Questions
How do I generate a password without special characters?
Use tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16; echo to get only letters and numbers.
Can I generate a password with a specific length?
Yes, change the number after head -c to your desired length. For example, head -c 24 gives a 24-character password.
Is it safe to use /dev/urandom for passwords?
Yes, /dev/urandom is cryptographically secure for password generation. It’s the same source used by many security tools.
How do I generate multiple passwords at once?
Use a loop: for i in {1..10}; do tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16; echo; done generates 10 passwords.
What is the difference between /dev/urandom and /dev/random?
/dev/urandom is faster and never blocks; /dev/random can block if the system entropy pool is low. For password generation, /dev/urandom is recommended.
Can I use these commands in a script?
Yes, but be careful with special characters in the password. Store it in a variable using command substitution: PASS=$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16).