Generating passwords on command line

Navigating through one those awesome pages on Github, I found this command to generate passwords on linux/bsd/osx:

$ LC_ALL=C tr -d -c "[:alpha:][:alnum:]" < /dev/urandom | head -c 20

Pretty nice, uh? You ran it, it generates a string with 20 characters of number and letters. Yes. But how??? tr is one of those simple commands that sometimes you took time to understand what it does.

I decided that I would understand what it does. It took sometime but I (almost) managed it:

LC_ALL=C => set tr to use C locale. ( I’ll have to write a post about locale.)

=> command to translate or remove characters.

=> delete the following chars

=> Everything but this chars. So, joined with -d,  tr will NOT remove letters and numers

< /dev/urandom
=> generate random numbers and chars and send to tr. However it generate a lot of special characters which are remove by the switches just described.

|head -c 20
=> receive tr output until it reaches 20 chars and prints it.

If you wan to generate a password with special characters you can use this:

$LC_ALL=C tr -d -c "[:print:]" < /dev/urandom | head -c 20


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s