Blog Archives

How to generate a random password using command-line tools

Here is a little command-line example to generate a random password.

To generate a random string you can use /dev/random or /dev/urandom. Both are kernel random number source devices. Read the man page to get detailed information. But if you use
cat /dev/urandom
you’ll get printable and non-printable bytes. Non-printable bytes are useless for a password. We have to use a filter to get only printable character. For this purpose we can use tr.
In this case SET is the charset we want, e.g. only lowercase chars, only digits, etc. So we have to delete all chars execpt SET. To delete these “bad chars” we can use the options cd. c is the complement of SET and d deletes the chars in SET.
The last thing we have to set is the password length. For this purpose we’ll use the head-command with option c. This option will print only the first N bytes.

A little example:
cat /dev/urandom | tr -cd [:graph:] | head -c 10

Read the rest of this entry