Linux下隨機生成密碼的命令總結


有時候經常為如何設置一個安全、符合密碼復雜度的密碼而絞盡腦汁,說實話,這實在是一個體力活而且浪費時間,更重要的是設置密碼的時候經常糾結。終於有一天實在忍不住了,於是學習、整理了一下如何使用Linux下命令來隨機生成安全、符合密碼復雜度的命令。

俗話說“工欲善其事必先利其器”。如果能用挖土機的,你為什么要用鋤頭? 最近一直在反思自己:習慣性用原始、老舊的方式解決問題。而不擅長利用一些工具快捷、方便的解決問題。

 

1:mkpaswd命令

 

mkpasswd 是Linux自帶的一個密碼生成工具,可以說是非常安全、可靠的(如果你擔心一些在線的隨機密碼生成工具,會收集、記錄你生成的密碼的話)。個人非常喜歡這個工具的原因就是它可以設置生成密碼的復雜度。另外,它不僅可以隨機幫你生成密碼,還可以直接幫你修改相關賬號密碼。如果你找不到這個命令,那么可能你沒有安裝expect包。

 

方式1: yum安裝方式。

yum -y install expect

 

 

方式2: rpm安裝方式。

 
[root@DB-Server Server]# ls *expect*
expect-5.43.0-5.1.i386.rpm  expect-5.43.0-5.1.x86_64.rpm  expect-devel-5.43.0-5.1.i386.rpm  expect-devel-5.43.0-5.1.x86_64.rpm  expectk-5.43.0-5.1.x86_64.rpm  pexpect-2.3-3.el5.noarch.rpm
[root@DB-Server Server]# rpm -ivh expect-5.43.0-5.1.x86_64.rpm
warning: expect-5.43.0-5.1.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...                ########################################### [100%]
   1:expect                 ########################################### [100%]

 

關於mkpasswd命令的一些參數,如下所示:

參數

    功能描述

-l

定義生成密碼的長度,默認9

-d

定義密碼里面包含數字的最少個數,默認2

-c

定義密碼里面包含小寫字母的最少個數,默認2

-C

定義密碼里面包含大寫字母的最少個數,默認2

-s

定義密碼密碼包含特殊字符的最少個數,默認1

-p

指定另外一個生成密碼的程序,默認是/etc/yppasswd

[root@DB-Server Server]# mkpasswd -l 14 -d 2 -C 2 -s 2 -v kerry
spawn passwd kerry
passwd: Unknown user name 'kerry'.
password for kerry is 1S[(qillErt8xf
[root@DB-Server Server]# mkpasswd -l 14 -d 2 -C 2 -s 2 -v test
spawn passwd test
passwd: Unknown user name 'test'.
password for test is 1ar1t$-qwIHcrb
[root@DB-Server Server]# mkpasswd -l 14 -d 2 -C 2 -s 2 -v oracle
spawn passwd oracle
Changing password for user oracle.
New UNIX password: 
Retype new UNIX password: 
passwd: all authentication tokens updated successfully.
password for oracle is 24_vFkxqsol^pR
[root@DB-Server Server]# 
 
[root@DB-Server Server]# mkpasswd -l 14 -d 2 -C 2 -s 2  oracle
i&jijfgB92*fDa
[root@DB-Server Server]# 

clip_image001

 

2:apg命令

 

apg是一款開源免費的密碼隨機生成器,官方網址為http://www.adel.nursat.kz/apg/

安裝方式:

 

1:Ubuntu下的apt-get方式:

sudo apt-get install apg

 

2:使用RPM方式:

[root@DB-Server tmp]# rpm -ivh apg-2.2.3-3.el5.rf.i386.rpm 
warning: apg-2.2.3-3.el5.rf.i386.rpm: Header V3 DSA signature: NOKEY, key ID 6b8d79e6
Preparing...                ########################################### [100%]
   1:apg                    ########################################### [100%]
[root@DB-Server tmp]# 

 

關於apg命令的一些參數,如下所示:

參數

    功能描述

-M

生成密碼的模式

-E

生成密碼過程中排除字符串

-r

apply dictionary check against file

-b

 

-p

paranoid modifier for bloom filter check

-a

1: 隨機密碼生成模式。  0:可發音的密碼生成

-n

包含數字,但是不是每個密碼都有

-N

每個密碼都必須包含數字

-m

生成密碼最小長度,默認為8,實際生成的隨機密碼長度都可能超過這個值

-x

生成密碼最大長度。

-s

包含特殊字符,但是不一定每個都有

-S

每個密碼都必須有特殊字符

-c

包含大寫字母。

-C

每個密碼都必須包含小寫字母

-d

 在生成的密碼中間不加任何分割符,默認6個密碼連成一長串

-l

生成密碼的spell

-t

輸出密碼中可發音字符的發音

-y

輸出加密密碼

-q

靜默模式,不輸出告警信息

-h

輸出幫助信息

-v

版本信息。

[root@DB-Server ~]# 
[root@DB-Server ~]# apg -M SNCL -m 8  -x 12  
Cupif6Swib?
Kij/OvMuk7
cyn=OtDuIf1
eic3Enn>
newCod"Rab4
{Flipnayt0
[root@DB-Server ~]# apg -M SNCL -m 8  -x 12  -t
dujghitt6Ob` (duj-ghitt-SIX-Ob-GRAVE)
RewgEmEc7Ow~ (Rewg-Em-Ec-SEVEN-Ow-TILDE)
Oc%Knegg1 (Oc-PERCENT_SIGN-Knegg-ONE)
loon5Quem+ (loon-FIVE-Quem-PLUS_SIGN)
El5Bleb{ (El-FIVE-Bleb-LEFT_BRACE)
Te4qued( (Te-FOUR-qued-LEFT_PARENTHESIS)

 

3:pwgen

 

pwgen生成的密碼容易記憶而且安全。pwgen的下載地址為https://sourceforge.net/projects/pwgen-win/files/PWGen/2.9.0/

apt-get install pwgen

關於pwgen命令的一些參數,如下所示:參數說明:

參數

    功能描述

-c

密碼里面包含至少一個大寫字母

-A

密碼里面不包含大寫字母

-n

密碼里面至少包含一個數字。

-0

密碼中不包含數字

-y

密碼中至少包含一個特殊字符。

-s

生成安全隨機的密碼

-B

密碼中不包含混淆的字符,例如“1”和“I”

-h

查看幫助信息

-H

使用指定文件sha1哈希生成,不適用隨機生成

-C

按列打印生成的隨機密碼

-1

每行輸出一個密碼,不按列打印生成密碼

-v

密碼不包括元音字母或者可能被誤認為是元音字母的數字

 

root@DB-Server:~# pwgen -h
Usage: pwgen [ OPTIONS ] [ pw_length ] [ num_pw ]
 
Options supported by pwgen:
  -c or --capitalize
        Include at least one capital letter in the password
  -A or --no-capitalize
        Don't include capital letters in the password
  -n or --numerals
        Include at least one number in the password
  -0 or --no-numerals
        Don't include numbers in the password
  -y or --symbols
        Include at least one special symbol in the password
  -s or --secure
        Generate completely random passwords
  -B or --ambiguous
        Don't include ambiguous characters in the password
  -h or --help
        Print a help message
  -H or --sha1=path/to/file[#seed]
        Use sha1 hash of given file as a (not so) random generator
  -C
        Print the generated passwords in columns
  -1
        Don't print the generated passwords in columns
  -v or --no-vowels
        Do not use any vowels so as to avoid accidental nasty words
 
root@DB-Server:~# pwgen -C
Xu3aiQuu choo2Aij ohCh3eip ou8ofeeL aYai5thi iet3eeNe ieth1Dei Mairah0o
gad9ooTh faBohZ6e ooJee3Oo Thiec4ah ahb1Iedi Sah7ahth Kainee4o beew7EeW
EedoHa1B eom3Aiph daigee2I AoG4Gei8 Siejoo9o Eemahk6a erur1Aim ohbu6Thi
ae0Ohbai Ehai9go0 ni0iYe4k Ais0eedo Ki3Aiku4 cue7AeXi Ao3lae5o oot2ahTh
yiev2Lei iNgeeth5 aeBi1sai quahh0Ei wei3aeP5 Bohng8ae Eecuor5i kie1eiTh
hiepa7Th uchaiTh5 eiX5Echi reiR4ele RaWaic5l Ahk6fae1 eiD5aen2 saep4Voh
irea6Quo Iakoh7pi eiPhoo1o pie9NeiR aj2Thied Mi3shugh oingi8Oo Vohthoo3
eph6Eed1 Ibooc3ux wo5TaPha Ceigh8aH eeX7ae6r aeB1phoo zohquo9H ahPhaig5
daenu0Ue qui2Feph queT5wo7 fooquo1M Vei4ceej Nie3nae2 IpahLa7e Ku6ahbuf
Oong2ye8 eiJ2haej ohChie4i oowah4Oz utaiM8Sh hah3Oong ieQu1pai aewai8Mi
waip3Aiz Hi0se4ai boon0AZi iR0eicee theeTh5n aeFo4air Tah7teef Aogh3Quo
Iya6eigo ie9Heegh aeyoh9Bo eiXoos4z YeZ7obai doosei4W sapeeBe8 ood1CahC
ie5ohF1s Goonoob2 aiy9Wu7W Oeh3sa5v EiTh9vai iyahs1Fu she5Oogi kohsha7E
Deeleit4 eibie0Th ou4Deihi Pai0oluk Vae6baih Doocee5e ahD2iex3 eil9aiRu
zeiNou0C uu4baiRi ahx2Eew2 Ashai0Ee Yae0aita paph4Air Ich4nei4 Mooph1Ch
eZ0AiTha Hoo8gaeh aiTha3Ga Quaif7ei Iel9oafu Cai0iD2u EeCuL0qu oochae0A
vuk1She4 Bai3Ohte aiChah9v Mohz7iYo zaeyoo8J xah4ohHi ohZ1ohw6 Eed0aeKo
tooQu3ar eiH8rata wiech2UB noMooch6 ahv8Boo3 zea2Gei4 oaM4Eile Cier9ieV
baeReef9 Iom2the0 Foe3ieza zaiNgat4 do1Bah7R ATuyom2u EeQu1lie chaeXo2j
eiza0Aed Leimuol0 eid5yu5C ahp4ahTu OobieSh2 Av8Iaxoh yeid0Eej gooR1aeY
root@DB-Server:~# pwgen -1
au9ni2Fa

clip_image002

生成一個10個長度包含大小字母,數字、特殊字符的密碼:

root@DB-Server:~# pwgen -1cny 10
 
The9ciep#a

 

生成12個密碼長度為10,並且至少包含一個特殊字符、數字、大寫字母的密碼

root@DB-Server:~# pwgen -cy -1 -n 10 12
cahw]ie7uG
nie1OoKo-u
Anguu4Iu!N
hie%m2ojeH
be7Wo{w:ah
Ee^quaif2u
lo{Ngia1ce
AeToh"D7Ah
ieS5Cho=ch
Lu4seey<aB
Si>oR.aiH6
eo;l6Fie1m

clip_image003

 

當然還有一些其它密碼生成工具,例如makepasswd、passwordmaker-cli 。個人覺得工具的掌握在精不在多,熟練掌握一兩款工具即可。網上有篇用Linux命令行生成隨機密碼的十種方法, 里面介紹了很多種生成密碼的工具和方法,只是有些命令和方法非常不實用。

 

參考資料:

http://www.geekfan.net/7064/

http://www.makeuseof.com/tag/5-ways-generate-secure-passwords-linux/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM