Linux / UNIX: Generate Passwords

by on May 27, 2009 · 1 comment· last updated at 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:



You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 1 comment… read it below or add one }

1 turtle April 16, 2012 at 7:26 am

more elegant :

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

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: