Linux / UNIX: Generate Passwords

Posted on in Categories , last updated May 27, 2010

How do I generate random passwords under Linux / UNIX / Mac OS X and *BSD operating systems?

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

tr -dc A-Za-z0-9_ < /dev/urandom | head -c 16 | xargs

Sample outputs:

1z2G4SVmZOdd4uK4

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

genpasswd() { 
	local l=$1
       	[ "$l" == "" ] && l=16
      	tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs 
}

Now use genpasswd as follows to create 8 character long password:
genpasswd 8
Sample outputs:

Es5Yzf8H

See also:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

Leave a Comment