How to Generate a Secure Random Password Using the Linux Terminal

When you are setting up a new user account, a database server, or a Wi-Fi access point in Linux, you need a strong, cryptographically secure password. While you could use a web-based password generator, doing so involves trusting a third-party website with your security credentials. Instead, you can instantly generate a highly secure, random password directly from the Linux command line using built-in tools.

Method 1: Using the ‘openssl’ Command

The OpenSSL utility is pre-installed on almost every Linux distribution (including Ubuntu, Debian, and CentOS) and is perfect for generating random strings of characters.

  1. Open your terminal application.
  2. Run the following command:
    openssl rand -base64 16

This command tells OpenSSL to generate 16 random bytes of data and output it in base64 format. The result will be a highly complex, 24-character password containing uppercase letters, lowercase letters, numbers, and symbols (like aB3+k9/zXp2=).

Method 2: Using ‘/dev/urandom’

If you need a simpler password without symbols (for example, if a specific database software rejects special characters), you can pull random data directly from the Linux kernel and filter it to only show letters and numbers.

  1. Run the following command:
    tr -cd 'A-Za-z0-9' < /dev/urandom | head -c 20

This command reads raw, random noise from /dev/urandom, filters out everything except standard alphanumeric characters, and spits out a clean, 20-character password.

Get the best tech tips delivered straight to your inbox.

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