本文将为您提供一些有关如何使用Shell生成随机密码的提示。可以在Linux命令行上用于生成随机密码的许多其他工具中,例如
openssl
,mktemp
,od
要么/dev/urandom
为此目的设计的最具体,最简单的工具是pwgen
。
安装
DEBIAN/UBUNTU
# apt-get install -y pwgen
FEDORA
# dnf install -y pwgen
CENTOS
# yum install -y pwgen
用法示例
让我们从简单执行生成一堆密码开始pwgen
命令。如果不使用任何选项pwgen
只需吐出160个密码,每个密码包含8个字符:
# 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
默认情况下pwgen
尝试生成应该更容易记住的密码。要获得完全随机的字符串,请结合使用pwgen
与-s
安全选项:
# 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
接下来,让我们看看如何改变pwgen
的输出以更好地满足我们的需求。生成单个安全的8个字符长的随机密码:
$ pwgen -s -1
fD8oZJhO
以下示例将使用pwgen
密码生成器,以生成单个安全的14个字符长的随机密码:
$ pwgen -s -1 14
tRRW0eHYTp6ljk
要生成两个15个字符长的安全随机密码,请运行:
$ pwgen -s 15 2
Myr3AnuWUTNJaoT uCOPKRygS2ZVnlv
下一个pwgen
该命令将生成一列包含5个随机安全密码的列,每个密码分别包含10个字符和至少一个特殊字符:
# pwgen -s 10 5 -1 -y
b|c;y[Y3xe
z07|'[J/!g
)[4r^S/oO>
Po#]#QKbK9
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