How to Securely Generate a Random Password in the Linux Terminal Using OpenSSL or /dev/urandom

When deploying a new database, creating a new user account, or setting up a secure SSH key passphrase on a Linux server, you need a password that is completely immune to dictionary attacks. Using a password you can memorize (like Summer2025!) is a catastrophic security vulnerability.

You need a complex, high-entropy string of random characters. While you could use an online password generator, pasting secure credentials into a third-party website is terrible security hygiene. Instead, you can generate cryptographically secure passwords instantly, directly within your Linux terminal, using built-in system tools.

Method 1: The OpenSSL Command (Best for Readability)

OpenSSL is a robust cryptographic toolkit pre-installed on almost every Linux distribution. It includes a built-in pseudo-random number generator that outputs incredibly strong Base64 encoded strings.

  1. Open your terminal.
  2. Type the following command:
    openssl rand -base64 16
  3. Press Enter.

The Result: You will immediately receive a 24-character password that looks something like this: b3G9vX2kM1pL8qZ5wR0tYx==

How it works: The number 16 represents the number of random bytes generated. Because Base64 encoding expands the byte size by about 33%, requesting 16 bytes yields a highly secure ~24-character string composed of uppercase letters, lowercase letters, numbers, and symbols.

Method 2: Reading from /dev/urandom (Total Chaos)

If you don’t have OpenSSL installed, or if you want absolute, unadulterated chaos generated directly by the Linux kernel’s entropy pool, you can read from the /dev/urandom file and filter the output.

  1. In the terminal, paste the following command exactly:
    tr -dc 'A-Za-z0-9!?%=' < /dev/urandom | head -c 20; echo
  2. Press Enter.

The Result: You will get a 20-character password consisting solely of the characters you explicitly permitted, looking like this: A9%bZ!?c2Q=x7mP!L0a3

How it works:

  • /dev/urandom spits out an infinite stream of random binary gibberish.
  • tr -dc 'A-Za-z0-9!?%=' acts as a filter, deleting every character from the stream that is not an alphanumeric letter, number, or one of the four specified symbols.
  • head -c 20 cuts the infinite stream off the exact millisecond it reaches 20 valid characters.
  • echo simply adds a line break at the end so the password doesn’t awkwardly overlap with your bash prompt.

Bonus: Create a Permanent Password Alias

Typing out that urandom command every time you need a password is tedious. You can save it as a permanent genpass command by adding an alias to your bash configuration.

  1. Open your bashrc file: nano ~/.bashrc
  2. Paste this line at the bottom:
    alias genpass="openssl rand -base64 16"
  3. Save and exit (Ctrl+O, Enter, Ctrl+X).
  4. Reload your configuration: source ~/.bashrc

From now on, anytime you type genpass into your terminal and hit Enter, the system will instantly hand you a secure, randomized password.

RELATED POSTS

  • How to Install and Configure the Keepalived Daemon for High Availability on Ubuntu
  • How to Find Files by Size in Linux using the find Command
  • How to Use the Linux hostnamectl Command to Set the System Hostname
  • How to Display the First 10 Lines of a File in Linux Using the head Command
  • How to Use the diff Command to Compare Two Text Files Line by Line in Linux
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.