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:
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
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
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
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
raghu@raghu-Inspiron-1440:~$ date +%s | sha512sum
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.
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.
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
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
To include all characters, such as punctuation marks etc, use:
raghu@raghu-Inspiron-1440:~$ tr -cd [:alnum:][:punct:] < /dev/urandom | head -c 20 ; echo
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
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