This article will provide you with some hints on how to generate random password using shell. Among many other tools which can be used on the Linux command line to generate random passwords such as openssl, mktemp, od or /dev/urandom the specific and easiest tool designed for this purpose is pwgen.

Installation

DEBIAN/UBUNTU
# apt-get install -y pwgen
FEDORA
# dnf install -y pwgen
CENTOS
# yum install -y pwgen

Usage examples

Let's start by generating bunch of passwords by simple executing pwgen command. If no options are used pwgen will simply spit out 160 passwords with 8 characters each:
# pwgen 
AeD7Amiv Reech6ie dei7aiPe yoogh1Ch Quoh0zae shu2Eepa aoquu3Xa aiR4Raiv
eeWah9Ro uPhe9pif uZiegh2h xie6Mail wa2xaHai saeLoh4O Yai8be7u aGhue7fe
unieB3du yaiK7She ooh0ohGe Go5zuKai Aeseeph1 UJ3Moo5i xaeg6ohG aJ8oogah
suFo3ohr jaTee0mi eeK9zuoh tho5xieP Ec6uob9j iyap1eNo at1Hahqu viebei2O
zeeg9cuX iP9pooW6 ohS1nahl zaese9Vu dee1Tooh aS2Bahyu aeH5shoh hichooW6
Aishie2v phee8Ahp ce2Peith Ez0thoos oot0Aibo aizepe8J reeC6she aihaeSh6
EVaaroh1 apuviCh2 Ui2aec5t Yoh8jong eo2zaeWo queiTo7i aebai4Oe jahj5ieN
Co5aije5 ooSh1oon taeJe5ae Kah3quie eeXae6Ma wu6ohp8U Eil4sha4 iiSohc4a
eeNg0ied HaeH3ohl Shie9aih ohC5phoi boos6euG uadai3Oo Cohpai9I thuogh8E
thie7Zai aem6sooN ieFu7uif tiewoh4U Kaes1bai Aox4zah0 OoH5utoh uo4aWied
eew7baeV Iej7piel ohMa4iet Oi2aephi Yailov9t oaTheiz2 lexea9Ie iez0The2
et3Phaey hee2Oa7u Pee1Ibee ohbae1iZ thoo9eiZ eiCh3aiw Veiqu1ph quohF2oh
xoh2Iwo3 aiQuai8i aeKae5Hi owaiz3Ve oJa2ret8 yie4baiK iex3Chug ruPie2ig
Ahv7ciev kiemok7U veiX5cae thuL0Pho AiyeuB5t ooX4igeb bi0sohBu Ohl0hoaf
Wooy1ja9 ue8ooj3B iik5yeP5 Ieboo1oi neeNiu7E aeV8aimu oojeiW6x Thoh6Eix
zoaPh1au aiCh3eem quevoo8T Iey0soRo ceiC3Do4 Dae9ua4f pieyie2U noXu5ooK
Zuexe5du oNeu8eef heu7iTah kahpa8Um thoSa7sh Lai4Keik oit7Dahc aiy5iaZo
siv2Ut3a Tha8eePh eb6ieM2c DeVie1ee Cocofo7E Rihee6ya thi1Iigi iBi6quar
Adeeh6Us Eicho5so siTho2Pu Dahcho5I kowe7Iev gee9IeNu Ohso2jae Aegha1oo
Oomil4Ee Ahghoot3 Izai3oov yegeey6C ea8uG1ai Ahsh4Aeb quie5aeR paWaiqu4
By default the pwgen attempts to generate passwords which should be easier to remember. To get a completely random strings combine pwgen with -s secure option:

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


# pwgen -s
YR6FIFKb x99QzcAm aiCA5QyM Hwc8P1au SGvXZF3y L77txyCJ 8qUYXXwx QvFMaT4P
rM9xqYbJ CZK0OI9v O23kfj80 Y7joS806 uL3FLhe6 R9ADY1d9 1iQuWPxC Pk6hRYlQ
uQ2tuQlt nL3Nrosf D2eI1fPx wxuy0VXs lXU5qb3n DcYI7XXl pl7OK8nZ efKra5H6
m7AM9eYy k1WcTP38 Phq555oE VrU8cMuP I5QpFd6S G9p59kmu 0Q4jVFl7 Ekc0whrP
z8COjbSQ 6CEX05ie 2EnvcrEY tw5lqu6Q 6O2265tc RIO2C1tM Qd472HiW ngkNx7gG
B9vqilLf mN4Y9w7g g8qzYqgv ChX4LMPu 9F1tE3Lw d9C5f2l7 qCXefQ1b 7eJ3ITpW
ZMhK8Ff4 4joawsSW YZMAzhQ0 rd78OORp SfNn4rmO pynejSa9 TF06DrAP slAO7Xkb
QK0F6bZE GWYN2EP1 jr5e0cXp A7NoW2Kd a3toKeMU KqZB0r9g DDIMav0i LuuGK8wz
c6n3BwNk SMJbCG9P T00ok0o7 8ruAutyF oimrrS33 3VeTeG4B ZjFJIO7b eD8poTKz
c1sWOD7m E4QxOANq 7r1OYJzE iHLmOL3G G4FXGV5t 2TZC2BRp zUQi0ZpV E90UlPOU
k1XxKZ25 MRbVYAT5 6cYYzwrl HLp3lmTv nsF1EBQk u0g5G1zI F5RJOWfG O3zDOt26
ZB4yEmVd LiUwmn0Y HxkiJeu8 DCnhMl1d C2FlQk9A gcJKSGX5 Rumpt4ib bvXXUPL2
xGJxlh8S oTQldBZ5 zfTZG1RJ nF02PCFE Vb2AgA6J p4ToqDzZ 0jwg9zOr hoCG1DIJ
6lBFjsmd SDigaNb4 eV4xA5fl w39o4iTC nh8nOBWh 6zBZf8nY GtQFDIU2 CbWjKN7l
bt42dFd5 V7ja2j5M TtF0kLF6 Tz5asvKw 5M6FmxuW 12r4AeQe OFK1ko6e q7Qre03t
7l0EsWon V5Q7BLaQ LtD2bHMB kNsF7Tu4 PwwcU0pj 3bmSKBbm jAVx6885 CdzRNJv4
e4EQgeGF PBmKP4ZU 8P9gaBIm 3HkS4ZTk 9nQAXPms WV9YOEB1 nque0XJ3 JBIF9qDw
vUUz207Z PNH77adG Nmu6RvPx otkdC6aX 5VkvFB9Y 1mAJmwx7 9clxLgTg xuDCnT5L
ERg3zj0S tmwY1BOa yl3dU5I3 zaS55Hf4 fFDnV2Ny kC891kpv 1Qb3GR2U gft1tQqO
3iUiCVbK FE4EHCgm JUtsfcj9 71lZOBea wRjjz81s gUcsO8BC ds75sOdH vDYDkL16
Next, let's see how we can alter pwgen's output to better fit our needs. Generate a single secure 8 characters long random password:
$ pwgen -s -1
fD8oZJhO
The following example will use pwgen password generator to produce a single secure 14 characters long random password:
$ pwgen -s -1 14
tRRW0eHYTp6ljk
To generate two 15 characters long secure random password run:
$ pwgen -s 15 2
Myr3AnuWUTNJaoT uCOPKRygS2ZVnlv
Next pwgen command will generate a column of 5 random secure passwords with 10 characters each and at least one special character:
# pwgen -s 10 5 -1 -y 
b|c;y[Y3xe
z07|'[J/!g
)[4r^S/oO>
Po#]#QKbK9
<4(0XQPoZ#
Alternatively, we can also generate 80 characters long single password with no vowels and numbers but at least with a single capital letter:


$ pwgen -s 80 -1 -v -c -0
wBLlhMzWKDZSwlLrwDMDMpXkrRklMhQxBVvGLMjtSFLDzmPDssgLmWjWPSMFSHzBQnhDsXDNhtlQRdDJ
In the last example we will show how to generate passwords for a list of users. Let's say that file users-list.txt contains a list of users for which we need to generate random passwords:
$ cat users-list.txt 
user1
user2
user3
user4
user5
user6
user7
user8
user9
user10
Next, we can use a simple bash for loop to generate 15 characters long password for each user in the list:
$ for i in $( cat users-list.txt ); do pwgen -s -1 15 1 | sed "s/^/$i /"; done
user1 tRVd0cbtviFN9oj
user2 QJQeELnhxqeg3v3
user3 u11HAOYWlSyJDPv
user4 BalUhUbmejS5HHW
user5 PzTz7UV3Kp4yeBb
user6 QVa8ZYkuM3wJI77
user7 dFF3GwAauBIPvc9
user8 tinOKCsHaD3NYkO
user9 mNfTtjp7dL6aUPw
user10 UnZulfCEz6yFhLJ
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: