HowTo: Linux Random Password Generator Command

last updated in Categories , ,

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:


  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


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

11 comment

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

    1. 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 PASSWORD SALT

      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.

  2. on mac this one not work for me

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

    but if we change with LC_ALL can work

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

  3. is xargs really necessary here?

    also, for passwords no special chars is quick to hack, so I like to use ASCII chars 50 -172 (right after the quotes so that it doesn’t interfere with variable assignment.

    tr -dc \\050-\\172 < /dev/urandom | head -c ${l}

    Still, have a question? Get help on our forum!