How to Generate a Random Password in the Linux Terminal

When creating a new user account, setting up a database, or configuring a Wi-Fi access point on a Linux server, you need a strong, completely random password. Relying on a human to manually type random characters on a keyboard almost always results in predictable patterns.

Instead of relying on online password generators (which requires trusting a third-party website not to log your credentials), you can force your Linux machine to mathematically generate a highly secure password directly in the terminal.

Method 1: Using OpenSSL (Recommended)

The OpenSSL toolkit is pre-installed on virtually every modern Linux distribution, making it the most reliable tool for this job. You can use its built-in pseudo-random byte generator and encode the output in Base64 (which translates the raw bytes into a readable string of letters, numbers, and symbols).

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

The terminal will instantly spit out a string of text similar to v3Q9bZ8xL/K2pM5nQwRtYw==. The number 16 in the command dictates the length; you can change it to 32 or 64 for incredibly long, uncrackable cryptographic keys.

Method 2: Using the /dev/urandom File

If you are working on a severely restricted, minimal server that lacks OpenSSL, you can extract raw entropy directly from the Linux kernel using the /dev/urandom file and filter it through the tr command.

  1. Execute the following command:
    tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c 20; echo

This command pulls raw, chaotic data from the kernel, strips away everything except standard letters, numbers, and a few safe special characters (A-Za-z0-9!@#$%^&*), cuts the output exactly at 20 characters (head -c 20), and prints it neatly on a new line (echo).

This method generates an incredibly strong password without requiring any external software packages to be installed.

Get the best tech tips delivered straight to your inbox.

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