Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Generate Random Passwords from Linux Command Line

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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: openssl or gpg if you prefer those tools (most systems have at least one).

Step-by-Step

Method 1: Using /dev/urandom (Always Available)

  1. Open a terminal.
  2. Generate a 16-character password using /dev/urandom and filter out unwanted characters:
    tr -dc 'A-Za-z0-9!@#$%^&*()_+-=' < /dev/urandom | head -c 16; echo
  3. To change the length, replace 16 with your desired number (e.g., 32).
  4. To include only alphanumeric characters, use 'A-Za-z0-9' instead.
  5. 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)

  1. Check if openssl is available by running which openssl. If it returns a path, proceed.
  2. Generate a 16-character password with:
    openssl rand -base64 12
    This outputs 16 characters (12 bytes encoded as base64). Adjust the byte count: -base64 16 gives 22 characters, -base64 24 gives 32.
  3. To exclude characters like +/, you can pipe through tr:
    openssl rand -base64 12 | tr -d '+/='

Method 3: Using gpg (If Installed)

  1. Check for gpg with which gpg.
  2. Generate a random password with:
    gpg --gen-random --armor 1 12
    This outputs 12 random bytes in base64. Change 12 to your desired byte length.

Method 4: Using apg (If Installed)

  1. Install apg if not present (e.g., sudo apt install apg on Debian/Ubuntu).
  2. Run apg -a 1 -M NCl -n 5 to generate 5 passwords with mixed case, numbers, and special characters.
  3. Options: -a 1 uses random algorithm, -M NCl ensures at least one number, capital, and lowercase letter.

Creating a Reusable Function

  1. Add a function to your shell profile (e.g., ~/.bashrc) for quick access:
  2. Open the file with nano ~/.bashrc.
  3. Append:
    genpass() { tr -dc 'A-Za-z0-9!@#$%^&*()_+-=' < /dev/urandom | head -c ${1:-16}; echo; }
    This creates a genpass command that defaults to 16 characters, or you can pass a length: genpass 24.
  4. Save and reload with source ~/.bashrc.

Common Mistakes

  • Using head without tr: head -c 16 /dev/urandom outputs raw binary, which may contain non-printable characters or newlines. Always filter with tr.
  • Not including enough character classes: A password with only lowercase letters is weak. Use uppercase, numbers, and symbols.
  • Using date or $$ as a seed: These are predictable and not cryptographically secure. Stick to /dev/urandom or openssl.
  • Forgetting to strip newlines: The echo at the end adds a newline for readability, but if you pipe the password directly into a command, you might inadvertently include a newline. Use tr -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/O or 1/l/I), filter them out with tr -d '0O1lI'.
  • Store generated passwords in a password manager rather than in plain text files.
  • Use openssl rand -hex 16 for a hex string (32 characters) if you need a format that is easy to type.
  • Combine methods: generate a long passphrase using apg or pwgen (if installed) for easier memorization.

Troubleshooting

  • Command not found: tr or head? These are coreutils and should exist on any Linux system. If missing, install coreutils via your package manager.
  • openssl rand fails with “unable to load config”? This usually means OpenSSL is not properly configured. Try openssl rand -base64 12 with -rand /dev/urandom or use /dev/urandom method instead.
  • Password contains spaces or special characters that break scripts? Wrap the password in quotes when using it, or use openssl rand -hex to get only hexadecimal characters.
  • Generating a very long password takes time? /dev/urandom is fast, but if you use tr with a large head -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. Use LC_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).