keepcalculator

06 Numbers and Powers

Generate Random Passwords Command Line

Card No. 06·68428 · Updated · Standard formula, shown below

Yes, you can generate random passwords directly from the command line using built-in tools or small scripts. This article explains the most common methods on Linux, macOS, and Windows, how they work, and how to choose the right one for your needs.

Why Use the Command Line for Passwords

Command-line password generation is fast, scriptable, and often more secure than using a website. It avoids sending your password over the network and lets you integrate generation into scripts or password managers. It also gives you full control over the character set and length, and you can generate many passwords at once.

Built-in Tools: /dev/urandom and openssl

The most reliable source of randomness on Linux and macOS is /dev/urandom. It provides cryptographic-quality random bytes. You can use it with base64 or tr to produce readable passwords.

Example (Linux/macOS):

head -c 16 /dev/urandom | base64 | tr -d '\n'

This outputs 16 random bytes encoded as base64, which gives about 22 characters. The tr -d '\n' removes the newline.

openssl is another common tool available on most systems. It has a rand command that can generate random data:

openssl rand -base64 16

This produces 16 random bytes, base64-encoded, with a trailing newline. You can remove the newline with | tr -d '\n' if needed.

Using /dev/urandom with tr for Custom Character Sets

If you want only alphanumeric characters or a specific set, you can filter the output of /dev/urandom with tr:

tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 20; echo

This reads random bytes, deletes everything not in the set A-Za-z0-9, and takes the first 20 characters. The echo adds a newline. Note that this method can be slow if the character set is small because tr may discard many bytes before finding a match.

Windows: PowerShell and Command Prompt

Windows does not have /dev/urandom, but PowerShell provides a secure random number generator via System.Security.Cryptography.RNGCryptoServiceProvider or the newer Get-Random with -SetSeed. A common one-liner is:

powershell -Command "[System.Text.Encoding]::ASCII.GetString([System.Security.Cryptography.RNGCryptoServiceProvider]::GetBytes(16))"

This produces 16 random ASCII bytes, which may include non-printable characters. To get a clean alphanumeric password, you can use:

powershell -Command "-join ((48..57)+(65..90)+(97..122) | Get-Random -Count 20 | % {[char]$_})"

This generates 20 characters from the ASCII ranges for digits, uppercase, and lowercase letters.

In the classic Command Prompt, there is no built-in random generator, but you can call PowerShell from it as shown above.

Using pwgen and Other Dedicated Tools

If you prefer a purpose-built tool, pwgen is available on Linux and macOS via package managers. It generates pronounceable passwords that are easier to remember:

pwgen -s 20 1

The -s flag makes it fully random (not pronounceable), and 20 is the length. You can also use apg (Automated Password Generator) which offers similar features.

On macOS, pwgen can be installed with brew install pwgen. On Debian/Ubuntu, use sudo apt install pwgen.

How to Choose the Right Method

Consider the following when selecting a method:

  • Security: Always use a cryptographically secure random source. /dev/urandom and openssl rand are secure. Avoid $RANDOM in bash or rand() in C, as they are not suitable for passwords.
  • Character set: Decide whether you need special characters. Some systems restrict them, so you may want alphanumeric only.
  • Length: Longer is generally better. A 16-character password with mixed case, digits, and symbols offers about 100 bits of entropy.
  • Portability: If you work across systems, openssl is the most portable since it is installed by default on most Linux, macOS, and Windows (via Git Bash or WSL).
  • Ease of use: For quick one-off passwords, openssl rand -base64 16 is simple. For a batch of passwords, a loop with tr works well.

Common Mistakes and Pitfalls

One common mistake is using head -c 16 /dev/urandom without encoding, which may produce non-printable characters. Always encode with base64 or filter with tr.

Another mistake is using openssl rand -hex and then converting to base64, which reduces entropy. Stick to -base64 directly.

On Windows, using %RANDOM% in batch files is not secure; it is a simple linear congruential generator. Use PowerShell instead.

Also, be careful with the tr method: if you specify a character set that is too small (e.g., only digits), the command may take a long time because it discards many bytes. For digits only, use tr -dc '0-9' and increase the output length.

Finally, never use the same password across multiple accounts. Even a strong generated password is useless if reused.

Frequently Asked Questions

Is /dev/urandom truly random?

Yes, /dev/urandom uses the kernel’s entropy pool, which is seeded from hardware noise. It is cryptographically secure for password generation. It does not block, unlike /dev/random which can wait for more entropy.

How do I generate a password without special characters?

Use tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 20 on Linux/macOS, or the PowerShell example above that selects from digit and letter ASCII ranges.

Can I generate multiple passwords at once?

Yes, use a loop: for i in {1..5}; do openssl rand -base64 16; done on Linux/macOS. In PowerShell, you can use 1..5 | ForEach-Object { ... }.

What is the best length for a password?

For most purposes, 16 characters is a good balance. If you use a password manager, 20-30 characters is even better. The key is to use a mix of character types and avoid dictionary words.

Is it safe to use these passwords for important accounts?

Yes, as long as you use a cryptographically secure method and keep the password private. Do not store it in plain text; use a password manager.

How do I make sure the password doesn’t contain ambiguous characters like 0/O or 1/l?

You can filter them out by modifying the character set. For example, use tr -dc 'A-HJ-NP-Za-km-z2-9' to exclude 0, O, 1, and l.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →