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
Was it useful? Share this post with the world!

2 Replies to “Generating Random Passwords in the Linux Command Line”

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

  2. Yes hedy, you can. Kindly catch below one command and understand it accordingly.
    cat /dev/urandom | tr -dc ‘[a-z][A-Z][0-9]-_!@#$%^&*()_+{}|:?=’ | fold -w 12 | head -n 5

Leave a Reply