How-To Geek

10 Ways to Generate a Random Password from the Command Line

One of the great things about Linux is that you can do the same thing hundreds of different ways—even something as simple as generating a random password can be accomplished with dozens of different commands. Here’s 10 ways you can do it.

We gathered all of these commands from Command-Line Fu and tested them out on our own Linux PC to make sure they work. You should be able to use at least some of these on Windows with Cygwin installed, though we didn’t test all of them—the last one definitely works though.

Generate a Random Password

For any of these random password commands, you can either modify them to output a different password length, or you can just use the first x characters of the generated password if you don’t want such a long password. Hopefully you’re using a password manager like LastPass anyway so you don’t need to memorize them.

This method uses SHA to hash the date, runs through base64, and then outputs the top 32 characters.

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

This method used the built-in /dev/urandom feature, and filters out only characters that you would normally use in a password. Then it outputs the top 32.

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

This one uses openssl’s rand function, which may not be installed on your system. Good thing there’s lots of other examples, right?

openssl rand -base64 32

This one works a lot like the other urandom one, but just does the work in reverse. Bash is very powerful!

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

Here’s another example that filters using the strings command, which outputs printable strings from a file, which in this case is the urandom feature.

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

Here’s an even simpler version of the urandom one.

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

This one manages to use the very useful dd command.

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

You can even create a random left-hand password, which would let you type your password with one hand.

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

If you’re going to be using this all the time, it’s probably a better idea to put it into a function. In this case, once you run the command once, you’ll be able to use randpw anytime you want to generate a random password. You’d probably 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 same syntax to make any of these into a function—just replace everything inside the { }

And here’s the easiest way to make a password from the command line, which works in Linux, Windows with Cygwin, and probably Mac OS X. I’m sure that some people will complain that it’s not as random as some of the other options, but 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.

There’s loads of other ways that you can create a random password from the command line in Linux—for instance, the mkpasswd command, which can actually assign the password to a Linux user account. So what’s your favorite way?

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 12/20/14
  • In Mint I use "apg". Typing apg -a 1 gives you 6 passwords about 9-10 characters long, containing upper and lower case letters, numerals and US keyboard symbols. Add -m followed by a number to specify a different length for the passwords, e.g. apg -a 1 -m15 would give you 6 passwords 15 characters long. Add -n followed by a number to specify a different number of passwords, e.g apg -a 1 -m12 -n10 would give you 10 passwords 12 characters long. There are other commands available to specify alphanumeric only, etc. If you just type apg it will ask you to enter random characters (which don't appear), and will then give you a selection of more-or-less pronounceable alphanumeric passwords 8-12 characters long.

  • My favorites are:</dev/urandom tr -d [:cntrl:] | head -c32 && echo, if you can simply cut & paste and don't have to type it at a keyboardelse </dev/urandom tr -dc [:print:] | head -c32 && echo

  • M Henri Day

    Nice, Paddleless - many thanks !... smile


  • Yu0

    I'd recommend against such passwords. While cryptographically fine, they are a pain to work with, e.g. when you need to enter them somewhere, copy paste or autofill from a password manager doesn't work (e.g. in some game clients), and typing in such passwords on a mobile device is especially tedious.

    The best method for passwords both cryptographically sufficiently safe and practical for everyday use are all-lowercase, space separated random lists of 4 or more words, see also (though what it really critiices are the non-random passwords resulting from letting average users create passwords with some "number and special character" rule).

    Except for cases of utmost importance, where convenience isn't relevant making randomly generated passwords just thinking of 4 or more words will usually give an acceptable result, though some webpages will negligibly more secure passwords at the cost of memorizability by enforcing the use of special characters / uppercase + lowercase combinations or disallowing spaces. A common mal-practice also is enforcing a maximum number of characters (restricts the cryptographic strength of random-word passwords).

    Nice list of methods for getting one though smile

  • I use a password manager, so for the majority of my passwords it is no more difficult for me to use "/J'%!K1j\p&as,Q{?lbt_2h~U!7[5H,diL+E4Uf\c1p~q-6(;a" than to use "correcthorsebatterystaple", while being quicker and easier to generate. I use passwords that are easier for me to remember where necessary, such as the password to my password manager, but these are a small percentage of the total.

More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!