MC, 16:18 piątek, 01.06.2012 r.
Ilustracja do artykułu: Linux - How to automatically generate random passwords

Linux - How to automatically generate random passwords

In this article I'm going to show you how to easily generate random passwords on Linux operating systems. We will also try to prepare bash script which should automatically generate given number of passwords for specified length.

Why should I generate passwords?

Automatically generating passwords can be really useful for creating new user accounts. Because of Linux's virtual devices created for generating random bytes it is quite easy to do that. These devices are: /dev/random and /dev/urandom. The main difference between them is that the /dev/urandom is not restricted by buffer size and it can be used for generating great amount of bytes, but less randomness. So it is accurate for generating temporary passwords.

How to generate passwords?

First of all, let's see what /dev/urandom returns:
$ head -n 1 /dev/urandom
���f��fFR�#�P��+�<�O���㦙Ei2[H�P
Huh... from our point of view there's nothing but junk, so we have to select these characters which we are interested in. We will use for it egrep command. This is a list of its parameters which might be useful:
  • -i (ignore case distinctions)
  • -m <number> (number of matching lines to find)
  • -o (return only matching string)
  • -a (process a binary file, in this case /dev/urandom, as if it were text)
Let's say we want to generate one password composed of five characters and digits. Give the command:
$ egrep -aio -m 1 "[a-z0-9]{5}" /dev/urandom
2yQXU
It's truly easy, isn't it? So let's generate five passwords, each one composed of ten characters:
$ egrep -aio -m 5 "[a-z0-9]{10}" /dev/urandom
W7qwQ5IRK9
XsGdCadYrC
WwjEbkILfy
bExdl1VjhM
RYcqkWodT3
This is the quite easy approach to generate passwords for given parameters and what is more important - automatically.

How to improve this process?

If we are going to generate passwords often, it will be more comfortable to create bash script which takes two parameters - number of passwords and length of them. Scripts are easier to be used in other scripts or applications. What I propose is:
#!/bin/bash
# Mateusz Chmielewski (www.mblog.boo.pl)
# Script takes two parameters:
#1 - number of passwords
#2 - lenght of the password

egrep -aio -m $1 "[a-z0-9]{$2}" /dev/urandom

At the end, please look at this exemplary script execution:

$ sh generatePasswords.sh 10 7
vuzFRIl
cF2Zi31
xE88E9g
AomosM6
DneeVn7
vYpyMB7
b55mlO0
h1kJfAt
RUZAWSJ
qO7mMa9

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść:
Polish version: Linux urandom — Automatyczne generowanie haseł (silnych)