Generate random passwords with this Bash script |

Generate random passwords with this Bash script

Take the hassle out of picking passwords with this simple Bash script.

Password lock
Image credits : 
freeGraphicToday, via Pixabay. CC0.

Subscribe now

Get the highlights in your inbox every week.

Periodically, I need to come up with new passwords, some of which need to be more secure than others. My mind often seems to draw a blank when I have to create a new login, and this short Bash script fills that void. Full disclosure: I found most of this script posted somewhere and made a minor modification to it.

#!/usr/bin/env sh
echo 'Generating 12-character passwords'
for ((n=0;n<12;n++))
do dd if=/dev/urandom count=1 2> /dev/null | uuencode -m - | sed -ne 2p | cut -c-12

I don't know all the details of how this works, but I have done a bit of research and experimentation. The "meat" of the script is in the line beginning with do dd. You may have seen dd used to copy one disk to another, but in this case, it is used to generate one block of random characters with dev/urandom. The feedback messages from dd are stripped out, and then the characters are passed on to uuencode, whose job it is to transform the characters so that all are printable (i.e., no control characters).

The uuencode command can be obtained by installing the sharutils package.

The job of sed seems to be some further transformation so that the letters are mixed upper and lower case. Finally, cut slices off 12 characters to end with a 12-character password. The for loop runs 12 times, so you get 12 passwords.

Generating 12-character passwords

This example output shows a pretty good jumble of characters. One thing to notice is that the only non-alphanumeric characters are / and +.

When I need a new password, I look through this output for one that "appeals" to me. If it's a password I will use frequently, I'd rather it be something I think I can eventually remember.

There is no reason to use these literally as they are; you can add or modify characters as you want. I mostly use eight-character passwords. Some places require a password to have specific elements, such as at least one lower-case letter, one upper-case letter, one number, and one symbol.

If I don't like any of the passwords, I run the script again to generate another 12. Someone might comment that dev/urandom only generates pseudo-random numbers, but this script certainly does a much better job than I could do on my own.

bash logo on green background

Learn basic Bash programming syntax and tools, as well as how to use variables and control operators, in the first article in this three-part series.
connecting yellow dots in a maze

Learning useful Bash skills can be fun with these command line games.
Shells in a competition

In our miniseries Variables in Shells, learn how to handle local variables in PowerShell.

About the author

Greg Pittman - Greg is a retired neurologist in Louisville, Kentucky, with a long-standing interest in computers and programming, beginning with Fortran IV in the 1960s. When Linux and open source software came along, it kindled a commitment to learning more, and eventually contributing. He is a member of the Scribus Team.