LINUX

How to Generate Random Passwords from the Linux Command Line

In today’s digital world, strong passwords are essential for securing user accounts, sensitive data, and system access. While password managers can generate and store passwords for you, sometimes you may need a quick, temporary password or want to create passwords directly from the Linux terminal—especially when working remotely.

Luckily, Linux provides a powerful and efficient tool called pwgen that simplifies random password generation. This guide will walk you through installing pwgen on various Linux distributions and demonstrate how to use it effectively to generate secure passwords.

Installing pwgen on Linux

To start using pwgen, you first need to install it on your Linux system. The installation process varies based on your Linux distribution. Below are the commands to install pwgen on different distributions:

For Debian or Ubuntu-based systems:

sudo apt-get install pwgen -y

For Fedora-based systems:

sudo dnf install pwgen

For Arch-based systems:

sudo pacman -S pwgen

For openSUSE-based systems:

sudo zypper install pwgen

After installation, pwgen is ready to use for generating strong and random passwords directly from the command line.

How to Generate Passwords with pwgen

The pwgen command allows you to create random passwords of various lengths, strengths, and formats. Here are some common use cases and command examples:

1. Generate a Single Random Password (16 Characters Long)

pwgen 16 1

This will generate one random password that is 16 characters long.

2. Generate Multiple Random Passwords (Three Passwords, 16 Characters Each)

pwgen 16 3

This command will generate three passwords, each 16 characters long.

3. Generate a List of Random Passwords (Default Behavior)

pwgen 16

Without specifying the number of passwords, pwgen will generate a list of random passwords, all 16 characters long.

Customizing Password Generation with pwgen Options

One of the best features of pwgen is its flexibility. It allows you to customize the generated passwords with additional parameters.

Here are some useful pwgen options and their descriptions:

OptionDescription
-c or –capitalizeInclude at least one uppercase letter
-A or –no-capitalizeExclude uppercase letters
-n or –numeralsInclude at least one number
-0 or –no-numeralsExclude numbers
-y or –symbolsInclude at least one special symbol
-r or –remove-chars=Exclude specific characters from passwords
-s or –secureGenerate completely random passwords
-B or –ambiguousAvoid ambiguous characters (like O and 0)
-CPrint generated passwords in columns
-1Print passwords in a single column
-v or –no-vowelsRemove vowels from passwords (for added security)

Examples of Custom Password Generation

Generate a Secure 20-Character Password (Fully Randomized)

pwgen -s 20 1

This generates a completely random, 20-character password with no patterns or predictability.

Generate a Password with Symbols and Numbers

pwgen -y -n 16 1

This creates a 16-character password that includes symbols and numbers.

Generate 5 Passwords Without Uppercase Letters

pwgen -A 16 5

This generates five passwords, each 16 characters long, without uppercase letters.

Generating Passwords Without pwgen (Using Bash Scripting)

If pwgen is not installed or unavailable, you can still generate secure random passwords using Bash scripts. Here’s a custom script to generate random passwords:

#!/bin/bash

# Password length
PASSWORD_LENGTH=16

# Number of passwords to generate
NUM_PASSWORDS=5

# Characters to include
USE_LOWERCASE=true
USE_UPPERCASE=true
USE_DIGITS=true
USE_SYMBOLS=true

# Function to generate a password
generate_password() {
  local characters=""

  if $USE_LOWERCASE; then characters+="abcdefghijklmnopqrstuvwxyz"; fi
  if $USE_UPPERCASE; then characters+="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; fi
  if $USE_DIGITS; then characters+="0123456789"; fi
  if $USE_SYMBOLS; then characters+="!@#$%^&*()_+=-`~[]{}|;':\",./<>?"; fi

  local password=""
  for i in $(seq 1 $PASSWORD_LENGTH); do
    local rand=$(( RANDOM % ${#characters} )) # Get random index
    local char="${characters:$rand:1}" # Get character at that index
    password+="$char" # Append to password
  done
  echo "$password"
}

# Generate passwords and print them
echo "Generated Passwords:"
for i in $(seq 1 $NUM_PASSWORDS); do
  password=$(generate_password)
  echo "$password"
done

How to Run the Script

1. Copy and paste the script into a file, for example:

nano generate_passwords.sh

2. Save and exit the file.

3. Make the script executable:

chmod +x generate_passwords.sh

4. Run the script:

./generate_passwords.sh

This script will generate five random passwords, each 16 characters long, with uppercase, lowercase, numbers, and special characters.

Why Use pwgen for Random Passwords?

1. Quick and Efficient

With one simple command, pwgen can generate strong, random passwords instantly, making it ideal for system administrators, developers, and security-conscious users.

2. Highly Customizable

The wide range of options allows you to customize password generation to meet different security requirements.

3. Secure and Reliable

Using completely random character generation (with -s mode), pwgen ensures passwords are unpredictable and strong.

4. Ideal for Scripting

System administrators can integrate pwgen into Bash scripts to automate password creation for user accounts, databases, and secure logins.

Final Thoughts

Generating secure passwords is crucial in today’s cyber-threat landscape, and pwgen provides a simple, fast, and efficient way to create strong passwords directly from the Linux command line. Whether you need a single password, multiple passwords, or a highly customized password with specific characters, pwgen has you covered.

For those who prefer scripting, a Bash-based random password generator is another effective alternative.

So, the next time you need a strong, random password, try using pwgen or a Bash script—and keep your accounts secure from cyber threats!

What’s your favorite Linux command for security tasks? Let us know in the comments!

You may also like

Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments