9 Examples To Generate Random Password In Linux

You may have noted that often when you create an account on some website, you are provided with a random password. How can we generate this random data (or passwords in our case) on Linux? This can be done in a number of ways. Let's see how.

Generating Random Passwords

Linux has a powerful way of chaining commands together that we will use here to generate random data. Date command prints out date and time of the day. As we know that time keeps on changing, so we use this changing data to create random passwords. So, first of all, let's have a look at the output of the 'date' command:

raghu@raghu-Inspiron-1440:~$ date
Thu Jun 28 15:56:02 IST 2012

with +%s, it displays total number of seconds passed since January 01, 1970:

raghu@raghu-Inspiron-1440:~$ date +%s

Example 1

Both of these commands can be used as input to 'md5sum' command, which creates 128 bit checksum(and we know that this checksum looks quite random).

raghu@raghu-Inspiron-1440:~$ date | md5sum
746b59da100daa4137d91de95c052793 -

Example 2

The pipe symbol, “|” is used to chain the commands, i.e. the output of the left command becomes the input of the right command. And the output with “date +%s”,

raghu@raghu-Inspiron-1440:~$ date +%s | md5sum
f751a65f32a1e0cf52c513a7cf23a451 -

Example 3

Here, instead of using “md5sum”, “sha256sum” or “sha512sum” can also be used to generate 256 or 512 bit output.

raghu@raghu-Inspiron-1440:~$ date | sha256sum
3a96b9feb1e7d869ed98b101a2e5cc5169e9f8d348712c1fb56196f877e23b8e -

raghu@raghu-Inspiron-1440:~$ date +%s | sha512sum
4f2f1e1863c243fea1dd7082006256211b16d0cb747968cd03ab77abc8f2a01b5c0cac7d9ca078e73d2dadf41959ff9cd2b1b8373dec803ab5967f007b19a5aa -

As you have seen that these commands create quite a large output, however, sometimes fixed length output (for example 8 character long) might be needed. The head command prints out first few lines (10 by default) of a file. But with -c option, it can be used to display first few characters.

Example 4

So, in the above commands, if we pipe the output further into “head -c 10” command, then it will generate only 10 characters of output.

raghu@raghu-Inspiron-1440:~$ date +%s | md5sum | head -c 10 ; echo

raghu@raghu-Inspiron-1440:~$ date | sha256sum | head -c 8 ; echo

Here, echo command is used just for a return after the output. Otherwise, output will attach itself to the prompt.

There is a file /dev/urandom that provides an interface to kernel's random number generator, meaning it can be used for random number generation. This file can produce a lot of random characters. Before we see how to use this file, we will have a look at a text processing tool, “tr”. “tr” command which can be used to translate the characters from one range to another (for example, from lowercase to uppercase). We won't go into much details of this command, but will confine ourselves with only what is needed for random numbers generation. “echo 'Hello World' | tr a-z A-Z” will convert all small letters in the message into capitals.

raghu@raghu-Inspiron-1440:~$ echo 'Hello World' | tr a-z A-Z

“tr -cd a-z” will truncate (-d option, delete) everything except (-c option, compliment) the letters in the given range (a-z here):

raghu@raghu-Inspiron-1440:~$ echo 'Hello World' | tr -cd a-z; echo

Here, H and W are removed because they do not belong to range a-z.

Example 5

Now let's get back to generating random numbers. If you need the random numbers in alphabets only, use “tr -cd [:alpha:] < /dev/urandom”. But be careful, it will fill the screen with random alphabets until you press ctrl+c. (The input redirection symbol, < in above command means that the file /dev/random is the input file of the command on its left side, tr here.) So we will use head command for first few characters.

raghu@raghu-Inspiron-1440:~$ tr -cd [:alpha:] < /dev/urandom | head -c 15 ; echo

Example 6

Here, [:alpha:] is a predefined character range for alphabets. “tr -cd A-Za-z “ could also be used for the same effect. If all letters and numbers are required, [:alnum:] (or alternatively A-Za-z0-9 or [alpha]0-9) is used.

raghu@raghu-Inspiron-1440:~$ tr -cd [:alnum:] < /dev/urandom | head -c 10 ; echo

Example 7.

To include all characters, such as punctuation marks etc, use:

raghu@raghu-Inspiron-1440:~$ tr -cd [:alnum:][:punct:] < /dev/urandom | head -c 20 ; echo

Example 8.

This output can be further given to md5sum or sha256sum command:

raghu@raghu-Inspiron-1440:~$ tr -cd [:alnum:] < /dev/urandom | head -c 10 | md5sum | head -c 10; echo

Example 9.

If you want more randomness in password, or if want to complicate it some more, you can use random number of bits as input to md5sum command (instead of fixed 10).

raghu@raghu-Inspiron-1440:~$ tr -cd [:alnum:] < /dev/urandom | head -c $(tr -cd 0-9 < /dev/urandom | head -c 1) | md5sum | head -c 10; echo

Here, $() substitutes the result of for its place. And here, we have generated a random number using “tr -cd 0-9 < /dev/urandom | head -c 1”. So this command inputs a random number of characters (less than 10) to md5sum command.

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.