How to generate random password from Linux command line

One of the fantastic things about Linux is that you can do the exact thing hundreds of different ways— even generating an easy random password can be performed with dozens of various commands. So next, I’m going to show you ten ways that you can do it.

We collected all of these commands from Command-Line Fu and tried them on our Linux PC to confirm they worked. You should be able to utilize some of these on Windows with Cygwin installed, though we didn’t test all of them—the last one works.

Generate a Random Password

If you want, you can change those random passwords commands to yield a distinct password length, or if you don’t want to use such a long password, you can use the first ten-fifteen characters from the generated password. But, of course, the best way to remember those long passwords is to use a password manager like LastPass.

This approach uses SHA to hash the date, runs via base64, and outputs the top 32 characters.

date +%s | sha256sum | base64 | head -c 32 ; echo

The next approach uses the built-in /dev/urandom feature and filters only characters you typically use in a password. And it outputs the top 32.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;

The next one uses OpenSSL’s rand function. Good thing there are lots of other samples.

openssl rand -base64 32

This one operates a lot like the other urandom one but does the job in reversal. Bash is mighty!

tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1

There’s another sample command, which yields printable strings from a file. In this case, it is the urandom feature.

strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo

There’s an actual, more straightforward version of the urandom one.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6

This one manages to use the convenient dd command.

dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

If you don’t know, you can make a random left-hand password that lets you write your password with just one hand.

</dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB' | head -c8; echo ""

If you want to use this all the time, it’s a better idea to put it into a function. In this case, once you execute the command once, you’ll be able to use randpw anytime you want to generate a random password. So you’d likely want to put this into your ~/.bashrc file.

randpw(){ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;}

You can use this exact syntax to make any of these into a function—replace everything inside the { }

And this is how to make a password from the command line in the easiest way, which works in Linux, Windows with Cygwin, and probably Mac OS X.

Some people will indeed whine that it’s not as random as some of the other options. Still, honestly, it’s random enough if you’re going to be using the whole thing.

date | md5sum

Yeah, that’s even easy enough to remember.

Recent Articles

Related Stories

Stay on op - Ge the daily news in your inbox