Generating Random Passwords in the Linux Command Line

You can use the following command to generate the random password:

$ tr -dc A-Za-z0-9 < /dev/urandom | head -c 8 | xargs

Sample output:


You can create a bash shell function as follows (add to your ~/.bashrc):

# Random password generator
genpasswd() {
tr -dc A-Za-z0-9 < /dev/urandom | head -c ${1:-8} | xargs

Reload .bashrc file.

$ . ~/.bashrc

Now use genpasswd to generate random passwords:

$ genpasswd
$ genpasswd 10
$ genpasswd 16

One Reply to “Generating Random Passwords in the Linux Command Line”

  1. Is it possible to add symbol to the genpasswd function ?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.