Generate random password in Linux Command Line

Password is widely used in a production environment, for example, when we created a system account, we need to set an initial password for it, and When email / database / Subversion / LDAP (..etc.) accounts created, random passwords are needed as well.
As the random password is common, there are many ways to generate random passwords in Linux/UNIX. We’ll show some examples here on how to generate a strong password on Linux command line.

Please note that this post does not cover the topics on password strength. you can use some online tools to check password strength.

Gmail passWord strength checkGMail Password Strength Check

You can use the following four commands directly in most of Linux distributions with default install.

joseph@test:~$ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8

I personally prefer to use this command. The other commands work great too, check below:

cat /dev/urandom | tr -cd 'a-f0-9' | head -c 16
dd if=/dev/random count=10 bs=1 | hexdump  | cut -d   -f 2-| head -n 1 | tr -d " "

uuencode which is supplied by sharutils is needed by this command (Sharutils is not installed by default):

head -c 12 /dev/random | uuencode -m - | tail -n 2 | head -n 1

By using the following command, you need to get mkpasswd installed. It’s man page is available here. It can be run like this:

mkpasswd -l 16 -s 0 -C 0|sed 's/[0ol1]/f/g'

Finally, if you have pwgen installed, you can also generate a random password in linux command line like this:

pwgen -ycn 16 1

PWGen supports Windows platform as well, you just need to install pwgen-win instead, it has a nice GUI.

One thought on “Generate random password in Linux Command Line

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.