Generate password with command line in Linux or macOS


Most websites or application require a strong password that contains a mix of letters, numbers and special characters. And to make it more secure, it should be unique and randomly generated for each account. If you are using Linux or macOS and usually work with command line, you can use it to generate without installing any additional application.

Using /dev/urandom or /dev/random

This is my favorite way to generate password because /dev/urandom and /dev/random are available in most Unix-like operating systems.

< /dev/urandom tr -dc '[:graph:]' | head -c32; echo

This command will create a random password with printable character, length is 32.

In case that alphabet & numeric character, use alnum instead of graph.

For more information, see tr manpage

With macOS, an error occur when run above command because of UTF-8. Just add LC_ALL=C as below:

< /dev/urandom LC_ALL=C tr -dc '[:graph:]' | head -c32; echo

Random

/dev/random takes the random values from the entropy pool. If the entropy pool is empty, it will be blocked reading.

URandom (Unlimited Random)

/dev/urandom is similar to /dev/random but in case that entropy pool is empty, it generate value using hashing argorithm such as: SHA, MD5, …

In term of security

Because of high-quality randomness of /dev/random, it’s suitable for a password or a task focus on security.

Using openssl

openssl rand -base64 32

This command can only generate a base64 string password.

Using date and hashing

date | md5

It similar to openssl command, but it’s not really random like openssl or /dev/urandom.

Using gpg

gpg --gen-random 1 32