HowTo: Linux Random Password Generator Command

by on February 18, 2011 · 9 comments· LAST UPDATED November 15, 2013

in , ,

How do I generate random passwords on the Linux command line using the bash shell? How to create random password using Linux command line options?

You can use the following shell function to generate random password. The function use the combination of the following commands and files:

    Tutorial details
    DifficultyEasy (rss)
    Root privilegesNo
    Estimated completion time2m
  1. /dev/urandom file - Linux kernel's random number generator source/interface. A read from the /dev/urandom device will not block waiting for more entropy.
  2. tr command - Translate or delete characters. Used to remove unwanted characters from /dev/urandom.
  3. head command - Output the first part of files.
  4. xargs command - build and execute command lines from standard input/pipe.

Bash function to create random password

Edit ~/.bashrc file, enter:
$ vi $HOME/.bashrc
Append the following code:

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

Save and close the file. Source ~/.bashrc again, enter:
$ source ~/.bashrc

To generate random password, enter:
$ genpasswd
Sample outputs:


To generate 8 character long random password, enter:
$ genpasswd 8
Sample outputs:

Linux Random Password Generator Command
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 9 comments… read them below or add one }

1 Alpha01 March 1, 2011 at 6:31 am

Cool tip, the following command always worked for me:
openssl rand -base64 6


2 Diceroll March 1, 2011 at 2:11 pm

Thanks for the Tips, and cool blog too.
keep up the good work.


3 HS March 18, 2011 at 7:15 pm

Debian / Ubuntu: sudo apt-get install pwgen
Centos / Redhat : yum –enablerepo=rpmforge install bind-utils secpwgen


4 3y3lop April 25, 2011 at 5:21 am

Thanks for the tips…its work. ^_^


5 Philippe Petrinko April 30, 2011 at 11:18 am

Well, the need for random password generation is an important topic, and your example of User-Defined Function is nice to know, but there is already a nice command for it! ;-D

It’s named [mkpasswd]

To get manual page, type in : man mkpasswd

mkpasswd – Overfeatured front end to crypt(3)


mkpasswd encrypts the given password with the crypt(3) libc function using the given salt.

-S, –salt=STRING
Use the STRING as salt. It must not contain prefixes such as $1$.

-R, –rounds=NUMBER
Use NUMBER rounds. This argument is ignored if the method choosen does not support variable rounds. For the
OpenBSD Blowfish method this is the logarithm of the number of rounds.

-m, –method=TYPE
Compute the password using the TYPE method. If TYPE is help then the available methods are printed.

-P, –password-fd=NUM
Read the password from file descriptor NUM instead of using getpass(3). If the file descriptor is not connected
to a tty then no other message than the hashed password is printed on stdout.

-s, –stdin
Like –password-fd=0.


6 cabaro June 9, 2011 at 5:17 pm

For ubuntu i like to use apg
apt-get install apg

Then just run:


7 Timo Juhani Lindfors February 19, 2012 at 10:08 am

The original example is very unsafe since all local users can see your password via the process list.

Details available in


8 Sebastian Palucha August 7, 2014 at 10:32 am

On Mac OS I have to add LC_TYPE in front of tr line e.g.

LC_CTYPE=C tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs

This was to avoid this "Illegal byte sequence" issue

Thanks for you solution.


9 David October 25, 2014 at 4:48 am

Thanks, Sebastian! I was getting that error, too.


Leave a Comment

Tagged as: , , , , , ,

Previous Faq:

Next Faq: