≡ Menu

Linux / UNIX: Generate Passwords

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:

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 1 comment… add one }
  • turtle April 16, 2012, 7:26 am

    more elegant :

    genpasswd() {
            tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${1:-8} | xargs
    }
    
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , , , , , , ,