Generate a random password


Alphanumeric:

head -c 250 /dev/urandom | LC_CTYPE=C tr -dc a-zA-Z0-9 | fold -w 32 | head -n 1

All characters:

head -c 500 /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9~!@#$%^&*_-' | fold -w 32 | head -n 1

To output without a newline, get rid of the fold and use head -c:

head -c 500 /dev/urandom | LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 32

To generate a hexadecimal value:

head -c 500 /dev/urandom | LC_CTYPE=C tr -dc a-f0-9 | head -c 32

NOTE: the initial number in head -c 500 needs to be signifigantly larger that the end result string, especially for the hexidecimal case, or your end result will be too short.

You can also use /dev/random, which might block but is theoretically more secure.

Date: 2017-05-24

Tags:  bash

Share: