How to generate a random password in Linux shell

There are several ways to generate random passwords for any character you want in Linux.

  1. PWGen

PWGen is a Linux command line utility that generates secure and human-memorable passwords for any desired character of any number in one go. To generate 8 passwords with 10 digits, you can do

$ pwgen 10 8

Output example

ouQ1e2hooB jee1o2jiPe iiF63g5ree phoo3O40ah k5e2reo8Is aiZ6iZ3kel ieb7ov6Dai si48ie5eiT

To make it even stronger, the ‘-s’ flag can be used, which generates a random and hard-to-remember password.

$ pwgen 10 8

Output example

4RP3k9g1KG W2cWav15mF eH3hS8KD49 bH10Zg4VtB k5Zu2m1Wea Hx5Dv5d6Gh Nfn7r7FxYH x6YfClj8W6

Additionally

You can randomly generate username using command line utility gpw… To generate 3 random 6 letter usernames, you can do

$ gpw 3 6</pair>
<p>Пример вывода</p>
<pre>
ttuons
ionwes
ferall

2. OpenSSL

OpenSSL is called the Swiss Army of Crypto. You can use OpenSSL to generate random passwords like so

$ openssl rand -base64 10 | cut -c -10

Output example

nday7dwFIQ

3. $ RANDOM

$ RANDOM is a built-in variable in BASH. You can generate a random password like this

$ echo $RANDOM$RANDOM$RANDOM | tr '[0-9]' '[a-zA-Z]' | cut -c -10

Output example

gabsbbwhbc

In case you only want to create a digital password

$ echo $RANDOM$RANDOM$RANDOM |cut -c3-12

Output example

1227344409

4.bash.generate.random.alphanumeric.string.sh

The script below is written ‘earthgecko’. You can get the latest version from here

#!/bin/bash
# bash generate random alphanumeric string
#
 
# bash generate random 32 character alphanumeric string (upper and lowercase) and
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
 
# bash generate random 32 character alphanumeric string (lowercase only)
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
 
# Random numbers in a range, more randomly distributed than $RANDOM which is not
# very random in terms of distribution of numbers.
 
# bash generate random number between 0 and 9
cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 1
 
# bash generate random number between 0 and 99
NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 2)
if [ "$NUMBER" == "" ]; then
  NUMBER=0
fi
 
# bash generate random number between 0 and 999
NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 3)
if [ "$NUMBER" == "" ]; then
  NUMBER=0
fi

Output example

fEFWqyvlAbOSpp7qKjCmMfqziRuHdho6O

This script generates a random number of 32 characters. To generate a random 10-digit password from this script, you can do 2 things.

    • Modify the script and replace 32 with 10. So the script now looks something like this
#!/bin/bash
# bash generate random alphanumeric string
#
 
# bash generate random 10 character alphanumeric string (upper and lowercase) and
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
 
# bash generate random 10 character alphanumeric string (lowercase only)
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1
 
# Random numbers in a range, more randomly distributed than $RANDOM which is not
# very random in terms of distribution of numbers.
 
# bash generate random number between 0 and 9
cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 1
 
# bash generate random number between 0 and 99
NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 2)
if [ "$NUMBER" == "" ]; then
  NUMBER=0
fi
 
# bash generate random number between 0 and 999
NUMBER=$(cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | sed -e 's/^0*//' | head --bytes 3)
if [ "$NUMBER" == "" ]; then
  NUMBER=0
fi

Output example

9HZhpqWcyJ

    • If you don’t change the script, you can cut 32 characters using the Linux ‘cut’ command to get 10 characters, something like this
$ ./bash.generate.random.alphanumeric.string.sh  | cut -c -10

Output example

OC9dFqwwbU

Sidebar