How to generate random passwords in Linux


  • One of the most common ways to generate a random password in Linux is by using the /dev/urandom PRNG

head /dev/urandom | tr -dc A-Za-z0-9 | head -c 20

Will generate something like:

[dpopescu@localhost prod]$ head /dev/urandom | tr -dc A-Za-z0-9 | head -c 20
eLoVg0tnKeLTibMw60Lo

  • We can also use the openssl rand function to generate a random string:

[dpopescu@localhost prod]$ openssl rand -base64 20
ZpnMt4RGm38smTx7vUbezu/nyEE=

  • We can get the number of seconds since 1970 using the date +%s command then hash it and encode:

[dpopescu@localhost prod]$ date +%s | sha256sum | base64 | head -c 20
ODk0OWM1M2RmYzlkOWQy

  • Or simply use the date command:

[dpopescu@localhost prod]$ date | sha512sum | head -c 20
58c61a26e8f83a15dbae

  • We can generate passwords using the $RANDOM function then hash it:

[root@localhost /root]# echo $RANDOM | sha256sum |head -c 20
360c5aefb017fbf0deff

  • Use the GNU shuf command to create permutations then hash it and encode:

[root@localhost /root]# shuf -i 1-100 | sha256sum | base64 | head -c 20
b574b57e221e4dc7270b

There are other password generators like makepassword or pwgen but I’ve tried to used only the in-built tools that ship with CentOS. Please post a comment if you know other methods to generate random passwords in Linux

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s