You may have noted that often when you create an account on some website, you are provided with a random password or something need to generate random string in bash. 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.
Generate random password methods
Linux has a powerful way of chaining commands together that we will use here to generate random data. The 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:
$ 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).
$ 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”,
$ date +%s | md5sum
Here, instead of using “md5sum”, “sha256sum” or “sha512sum” can also be used to generate 256 or 512 bit output.
$ date | sha256sum
$ 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.
$ date +%s | md5sum | head -c 10 ; echo
$ date | sha256sum | head -c 8 ; echo
Here, echo command is used just for a return after the output. Otherwise, the 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.
$ 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):
$ 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.
$ 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.
$ tr -cd [:alnum:] < /dev/urandom | head -c 10 ; echo
To include all characters, such as punctuation marks etc, use:
$ tr -cd [:alnum:][:punct:] < /dev/urandom | head -c 20 ; echo
This output can be further given to md5sum or sha256sum command:
$ 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).
$ tr -cd [:alnum:] < /dev/urandom | head -c $(tr -cd 0-9 < /dev/urandom | head -c 1) | md5sum | head -c 10; echo