How to generate random passwords from the Linux command line


ZDNET

I often need random passwords. Mostly, those passwords are used for the creation of user accounts on apps and services, at which point I’ll use my password manager that includes a very good random password generator.

But other times, I might need a temporary password that doesn’t require saving in a password manager. Or maybe it’s a permanent password, but I’m logged into my desktop from a remote machine, and the command line is the only tool I have.

Also: 5 Linux commands I use to keep my device running smoothly

Regardless of why, there’s a simple Linux command that makes it very easy to generate random passwords. That tool is called pwgen, and here’s how you install and use it.

How to install pwgen

What you’ll need: The only things you’ll need for this are a Linux distribution and a user with sudo privileges.

To install pwgen on Debian or Ubuntu-based distributions, open a terminal window and issue the command:


Show more

sudo apt-get install pwgen -y

To install pwgen on Fedora-based distributions, open a terminal window and issue the command:


Show more

sudo dnf install pwgen

To install pwgen on Arch-based distributions, open a terminal window and issue the command:


Show more

sudo pacman -S pwgen

To install pwgen on SUSE-based distributions, open a terminal window and issue the command:


Show more

sudo zypper install pwgen

At this point, the app should be ready to go.

How to use pwgen

Also: 5 best Linux commands for troubleshooting problems

1. Generate a single password

Let’s say you want to generate a single password that is 16 characters long. For that, the command would be:

pwgen 16 1

2. Generate three passwords

You might need to create 3 random passwords, each of which is 16 characters long. For that, the command would be:

pwgen 16 3

3. Generate a list of passwords

You might want a list of passwords, each of which is 16 characters long. To do that, issue the command:

pwgen 16

As you can see, the first option tells pwgen how long the password should be, and the second option tells pwgen how many passwords to generate.

Also: 5 Linux commands you need to have ready – just in case

You can also use a number of options with pwgen. Those options are:

  • -c or –capitalize – Include at least one capital letter.
  • -A or –no-capitalize – Don’t include capital letters.
  • -n or –numerals – Include at least one number.
  • -0 or –no-numerals – Don’t include numbers.
  • -y or –symbols – Include at least one special symbol.
  • -r or –remove-chars= – Remove characters from the set of characters
  • -s or –secure – Generate completely random passwords
  • -B or –ambiguous – Don’t include ambiguous characters.
  • -H or –sha1=path/to/file[#seed] – Use sha1 hash of given file as a (not so) random generator
  • -C – Print the generated passwords in columns
  • -1 – Don’t print the generated passwords in columns
  • -v or –no-vowels

One thing to keep in mind is that pwgen used to have a –no-space option, which made it possible to use regular expressions in scripts. That option has since been deprecated, so writing scripts with the pwgen command is a bit more complex than it once was. What once could be done with a few lines now requires far longer scripts and would look something like this:

Also: The 6 Linux commands you need to know for user management

#!/bin/bash

# Password length
PASSWORD_LENGTH=16

# Number of passwords to generate
NUM_PASSWORDS=5

# Characters to include (lowercase, uppercase, digits, symbols)
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

# Example: Store passwords in an array (optional)
password_array=()
for i in $(seq 1 $NUM_PASSWORDS); do
password=$(generate_password) # Regenerate passwords. If you want to use the first
password_array+=(“$password”)
done

# Print any errors while generating and storing passwords
if [ $? -ne 0 ]; then
echo “An error occurred”
fi

# (Extra) Example of working with the saved passwords in the array
echo “Stored in password_array:”
for password in “${password_array[@]}”; do
echo “$password”
done

Enjoy those random passwords.





Source link

Leave a Comment