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:

4fFUND1d

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
vmuWt7TS
$ genpasswd 10
ymkIgxcdCh
$ genpasswd 16
wQnqgdc5tAQoiBdf