Home –  Tips – Your own command line password generator!

Your own command line password generator!

random password from command line

When you need to create a random password, stop brain storming over random characters as you have Linux command line at your disposal. It can accomplish things perfectly and, there are multiple ways to do it. Today we are creating our own command line password generator. We have assembled some ways to create a random password. We will start off with an easy one, its easy to remember:

Using md5sum

It doesn’t give you enough options but its random enough.

date +”%N” | md5sum If you want some more control, keep reading.

For rest of the following random password commands, you are allowed to modify them according to your requirement like restricting the number of characters returned.

Using /dev/urandom

It is a built-in feature. Following command return only those characters which you need to use in a password.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;

Another way to do it but in reverse order:

tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1

Using SHA

Date is hashed via SHA, passed on to base64 and 32 characters appear in output.

date +%s | sha256sum | base64 | head -c 32 ; echo

Using Strings

String command filters and returns printable straings:

strings /dev/urandom | grep -o ':alnum:' | head -n 30 | tr -d '\n'; echo

Using urandom (again!)

This one is less complicated:

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6 ; echo ;

Using dd command

Ever thought dd is capable of doing this?

dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

Using openssl’s rand

It may not be available on your system:

openssl rand -base64 32

If you feel like using some method again and again, you can save yourself the trouble of executing the command by putting it into a function and placing that function in ~/.bashrc. For an instance, if you want to create a function for SHA one here is the code:

genpass() { date +%s | sha256sum | base64 | head -c 32 ; echo } We are calling our random password generator as “genpass”.

All the commands are tested on our own system, screenshot:
random password from command line

So whats your favorite method? Post it in comments.

4 thoughts on “Your own command line password generator!

  1. lightonflux

    Ohh, dd that’s really strange!

    You forgot the simple tool `pwgen`, but that would be to simple :)

  2. VulcanRidr

    In Debian and it’s progeny, there is also a package called apg that is an apt-get away. Once installed, you can do

    $ apg -MSNCL (where SNCL is “s”pecial character, “n”umeric, “c”apital, and “l”owercase. When lowercase (-Msncl), it instructs apg that it -should- use these char sets, whereas uppercase -MSNCL means it must use them.

  3. Shaun

    None of the ones based on a date only are “random enough”. Spinning through all possible dates and hashing them would take only a few minutes (or less if someone knew roughly when you created the password). Much better to use /dev/(u)random.

  4. capecape

    All are very good methods, but openssl rand y very easy to use, i love it. Thanks!

Leave a Reply

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