八種幫你在Linux上生成隨機(jī)密碼的方法
學(xué)習(xí)使用 8 種 Linux 原生命令或第三方實(shí)用程序來(lái)生成隨機(jī)密碼。
在這篇文章中,我們將引導(dǎo)你通過(guò)幾種不同的方式在 Linux 終端中生成隨機(jī)密碼。其中幾種利用原生 Linux 命令,另外幾種則利用極易在 Linux 機(jī)器上安裝的第三方工具或?qū)嵱贸绦驅(qū)崿F(xiàn)。在這里我們利用像 openssl
, dd, md5sum
, tr
, urandom
這樣的原生命令和 mkpasswd,randpw,pwgen,spw,gpg,xkcdpass,diceware,revelation,keepaasx,passwordmaker 這樣的第三方工具。
其實(shí)這些方法就是生成一些能被用作密碼的隨機(jī)字母字符串。隨機(jī)密碼可以用于新用戶(hù)的密碼,不管用戶(hù)基數(shù)有多大,這些密碼都是***的。話(huà)不多說(shuō),讓我們來(lái)看看 8 種不同的在 Linux 上生成隨機(jī)密碼的方法吧。
使用 mkpasswd 實(shí)用程序生成密碼
mkpasswd
在基于 RHEL 的系統(tǒng)上隨 expect
軟件包一起安裝。在基于 Debian 的系統(tǒng)上 mkpasswd
則在軟件包 whois
中。直接安裝 mkpasswd
軟件包將會(huì)導(dǎo)致錯(cuò)誤:
- RHEL 系統(tǒng):軟件包 mkpasswd 不可用。
- Debian 系統(tǒng):錯(cuò)誤:無(wú)法定位軟件包 mkpasswd。
所以按照上面所述安裝他們的父軟件包,就沒(méi)問(wèn)題了。
運(yùn)行 mkpasswd
來(lái)獲得密碼
root@kerneltalks# mkpasswd << on RHEL
zt*hGW65c
root@kerneltalks# mkpasswd teststring << on Ubuntu
XnlrKxYOJ3vik
這個(gè)命令在不同的系統(tǒng)上表現(xiàn)得不一樣,所以工作方式各異。你也可以通過(guò)參數(shù)來(lái)控制長(zhǎng)度等選項(xiàng),可以查閱 man 手冊(cè)來(lái)探索。
使用 openssl 生成密碼
幾乎所有 Linux 發(fā)行版都包含 openssl。我們可以利用它的隨機(jī)功能來(lái)生成可以用作密碼的隨機(jī)字母字符串。
root@kerneltalks # openssl rand -base64 10
nU9LlHO5nsuUvw==
這里我們使用 base64
編碼隨機(jī)函數(shù),***一個(gè)數(shù)字參數(shù)表示長(zhǎng)度。
使用 urandom 生成密碼
設(shè)備文件 /dev/urandom
是另一個(gè)獲得隨機(jī)字符串的方法。我們使用 tr
功能并裁剪輸出來(lái)獲得隨機(jī)字符串,并把它作為密碼。
root@kerneltalks # strings /dev/urandom |tr -dc A-Za-z0-9 | head -c20; echo
UiXtr0NAOSIkqtjK4c0X
使用 dd 命令生成密碼
我們甚至可以使用 /dev/urandom
設(shè)備配合 dd 命令 來(lái)獲取隨機(jī)字符串。
root@kerneltalks# dd if=/dev/urandom bs=1 count=15|base64 -w 0
15+0 records in
15+0 records out
15 bytes (15 B) copied, 5.5484e-05 s, 270 kB/s
QMsbe2XbrqAc2NmXp8D0
我們需要將結(jié)果通過(guò) base64
編碼使它能被人類(lèi)可讀。你可以使用數(shù)值來(lái)獲取想要的長(zhǎng)度。想要獲得更簡(jiǎn)潔的輸出的話(huà),可以將“標(biāo)準(zhǔn)錯(cuò)誤輸出”重定向到 /dev/null
。簡(jiǎn)潔輸出的命令是:
root@kerneltalks # dd if=/dev/urandom bs=1 count=15 2>/dev/null|base64 -w 0
F8c3a4joS+a3BdPN9C++
使用 md5sum 生成密碼
另一種獲取可用作密碼的隨機(jī)字符串的方法是計(jì)算 MD5 校驗(yàn)值!校驗(yàn)值看起來(lái)確實(shí)像是隨機(jī)字符串組合在一起,我們可以用作密碼。確保你的計(jì)算源是個(gè)變量,這樣的話(huà)每次運(yùn)行命令時(shí)生成的校驗(yàn)值都不一樣。比如 date
!date 命令 總會(huì)生成不同的輸出。
root@kerneltalks # date |md5sum
4d8ce5c42073c7e9ca4aeffd3d157102 -
在這里我們將 date
命令的輸出通過(guò) md5sum
得到了校驗(yàn)和!你也可以用 cut 命令 裁剪你需要的長(zhǎng)度。
使用 pwgen 生成密碼
pwgen
軟件包在類(lèi)似 EPEL 軟件倉(cāng)庫(kù)(LCTT 譯注:企業(yè)版 Linux 附加軟件包)中。pwgen
更專(zhuān)注于生成可發(fā)音的密碼,但它們不在英語(yǔ)詞典中,也不是純英文的。標(biāo)準(zhǔn)發(fā)行版?zhèn)}庫(kù)中可能并不包含這個(gè)工具。安裝這個(gè)軟件包然后運(yùn)行 pwgen
命令行。Boom !
root@kerneltalks # pwgen
thu8Iox7 ahDeeQu8 Eexoh0ai oD8oozie ooPaeD9t meeNeiW2 Eip6ieph Ooh1tiet
cootad7O Gohci0vo wah9Thoh Ohh3Ziur Ao1thoma ojoo6aeW Oochai4v ialaiLo5
aic2OaDa iexieQu8 Aesoh4Ie Eixou9ph ShiKoh0i uThohth7 taaN3fuu Iege0aeZ
cah3zaiW Eephei0m AhTh8guo xah1Shoo uh8Iengo aifeev4E zoo4ohHa fieDei6c
aorieP7k ahna9AKe uveeX7Hi Ohji5pho AigheV7u Akee9fae aeWeiW4a tiex8Oht
你的終端會(huì)呈現(xiàn)出一個(gè)密碼列表!你還想要什么呢?好吧。你還想再仔細(xì)探索的話(huà), pwgen
還有很多自定義選項(xiàng),這些都可以在 man 手冊(cè)里查閱到。
使用 gpg 工具生成密碼
GPG 是一個(gè)遵循 OpenPGP 標(biāo)準(zhǔn)的加密及簽名工具。大部分 gpg 工具都預(yù)先被安裝好了(至少在我的 RHEL7 上是這樣)。但如果沒(méi)有的話(huà)你可以尋找 gpg
或 gpg2
軟件包并安裝它。
使用下面的命令以從 gpg 工具生成密碼。
root@kerneltalks # gpg --gen-random --armor 1 12
mL8i+PKZ3IuN6a7a
這里我們傳了生成隨機(jī)字節(jié)序列選項(xiàng)(--gen-random
),質(zhì)量為 1(***個(gè)參數(shù)),次數(shù) 12 (第二個(gè)參數(shù))。選項(xiàng) --armor
保證以 base64
編碼輸出。
使用 xkcdpass 生成密碼
著名的極客幽默網(wǎng)站 xkcd,發(fā)表了一篇非常有趣的文章,是關(guān)于好記但又復(fù)雜的密碼的。你可以在這里閱讀。所以 xkcdpass
工具就受這篇文章啟發(fā),做了這樣的工作!這是一個(gè) Python 軟件包,可以在這里的 Python 的官網(wǎng)上找到它。
所有的安裝使用說(shuō)明都在上面那個(gè)頁(yè)面提及了。這里是安裝步驟和我的測(cè)試 RHEL 服務(wù)器的輸出,以供參考。
root@kerneltalks # wget https://pypi.python.org/packages/b4/d7/3253bd2964390e034cf0bba227db96d94de361454530dc056d8c1c096abc/xkcdpass-1.14.3.tar.gz#md5=5f15d52f1d36207b07391f7a25c7965f
--2018-01-23 19:09:17-- https://pypi.python.org/packages/b4/d7/3253bd2964390e034cf0bba227db96d94de361454530dc056d8c1c096abc/xkcdpass-1.14.3.tar.gz
Resolving pypi.python.org (pypi.python.org)... 151.101.32.223, 2a04:4e42:8::223
Connecting to pypi.python.org (pypi.python.org)|151.101.32.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 871848 (851K) [binary/octet-stream]
Saving to: ‘xkcdpass-1.14.3.tar.gz’
100%[==============================================================================================================================>] 871,848 --.-K/s in 0.01s
2018-01-23 19:09:17 (63.9 MB/s) - ‘xkcdpass-1.14.3.tar.gz’ saved [871848/871848]
root@kerneltalks # tar -xvf xkcdpass-1.14.3.tar.gz
xkcdpass-1.14.3/
xkcdpass-1.14.3/examples/
xkcdpass-1.14.3/examples/example_import.py
xkcdpass-1.14.3/examples/example_json.py
xkcdpass-1.14.3/examples/example_postprocess.py
xkcdpass-1.14.3/LICENSE.BSD
xkcdpass-1.14.3/MANIFEST.in
xkcdpass-1.14.3/PKG-INFO
xkcdpass-1.14.3/README.rst
xkcdpass-1.14.3/setup.cfg
xkcdpass-1.14.3/setup.py
xkcdpass-1.14.3/tests/
xkcdpass-1.14.3/tests/test_list.txt
xkcdpass-1.14.3/tests/test_xkcdpass.py
xkcdpass-1.14.3/tests/__init__.py
xkcdpass-1.14.3/xkcdpass/
xkcdpass-1.14.3/xkcdpass/static/
xkcdpass-1.14.3/xkcdpass/static/eff-long
xkcdpass-1.14.3/xkcdpass/static/eff-short
xkcdpass-1.14.3/xkcdpass/static/eff-special
xkcdpass-1.14.3/xkcdpass/static/fin-kotus
xkcdpass-1.14.3/xkcdpass/static/ita-wiki
xkcdpass-1.14.3/xkcdpass/static/legacy
xkcdpass-1.14.3/xkcdpass/static/spa-mich
xkcdpass-1.14.3/xkcdpass/xkcd_password.py
xkcdpass-1.14.3/xkcdpass/__init__.py
xkcdpass-1.14.3/xkcdpass.1
xkcdpass-1.14.3/xkcdpass.egg-info/
xkcdpass-1.14.3/xkcdpass.egg-info/dependency_links.txt
xkcdpass-1.14.3/xkcdpass.egg-info/entry_points.txt
xkcdpass-1.14.3/xkcdpass.egg-info/not-zip-safe
xkcdpass-1.14.3/xkcdpass.egg-info/PKG-INFO
xkcdpass-1.14.3/xkcdpass.egg-info/SOURCES.txt
xkcdpass-1.14.3/xkcdpass.egg-info/top_level.txt
root@kerneltalks # cd xkcdpass-1.14.3
root@kerneltalks # python setup.py install
running install
running bdist_egg
running egg_info
writing xkcdpass.egg-info/PKG-INFO
writing top-level names to xkcdpass.egg-info/top_level.txt
writing dependency_links to xkcdpass.egg-info/dependency_links.txt
writing entry points to xkcdpass.egg-info/entry_points.txt
reading manifest file 'xkcdpass.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'xkcdpass.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/xkcdpass
copying xkcdpass/xkcd_password.py -> build/lib/xkcdpass
copying xkcdpass/__init__.py -> build/lib/xkcdpass
creating build/lib/xkcdpass/static
copying xkcdpass/static/eff-long -> build/lib/xkcdpass/static
copying xkcdpass/static/eff-short -> build/lib/xkcdpass/static
copying xkcdpass/static/eff-special -> build/lib/xkcdpass/static
copying xkcdpass/static/fin-kotus -> build/lib/xkcdpass/static
copying xkcdpass/static/ita-wiki -> build/lib/xkcdpass/static
copying xkcdpass/static/legacy -> build/lib/xkcdpass/static
copying xkcdpass/static/spa-mich -> build/lib/xkcdpass/static
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/xkcdpass
copying build/lib/xkcdpass/xkcd_password.py -> build/bdist.linux-x86_64/egg/xkcdpass
copying build/lib/xkcdpass/__init__.py -> build/bdist.linux-x86_64/egg/xkcdpass
creating build/bdist.linux-x86_64/egg/xkcdpass/static
copying build/lib/xkcdpass/static/eff-long -> build/bdist.linux-x86_64/egg/xkcdpass/static
copying build/lib/xkcdpass/static/eff-short -> build/bdist.linux-x86_64/egg/xkcdpass/static
copying build/lib/xkcdpass/static/eff-special -> build/bdist.linux-x86_64/egg/xkcdpass/static
copying build/lib/xkcdpass/static/fin-kotus -> build/bdist.linux-x86_64/egg/xkcdpass/static
copying build/lib/xkcdpass/static/ita-wiki -> build/bdist.linux-x86_64/egg/xkcdpass/static
copying build/lib/xkcdpass/static/legacy -> build/bdist.linux-x86_64/egg/xkcdpass/static
copying build/lib/xkcdpass/static/spa-mich -> build/bdist.linux-x86_64/egg/xkcdpass/static
byte-compiling build/bdist.linux-x86_64/egg/xkcdpass/xkcd_password.py to xkcd_password.pyc
byte-compiling build/bdist.linux-x86_64/egg/xkcdpass/__init__.py to __init__.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying xkcdpass.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying xkcdpass.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying xkcdpass.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying xkcdpass.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying xkcdpass.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying xkcdpass.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating dist
creating 'dist/xkcdpass-1.14.3-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing xkcdpass-1.14.3-py2.7.egg
creating /usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.egg
Extracting xkcdpass-1.14.3-py2.7.egg to /usr/lib/python2.7/site-packages
Adding xkcdpass 1.14.3 to easy-install.pth file
Installing xkcdpass script to /usr/bin
Installed /usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.egg
Processing dependencies for xkcdpass==1.14.3
Finished processing dependencies for xkcdpass==1.14.3
現(xiàn)在運(yùn)行 xkcdpass
命令,將會(huì)隨機(jī)給出你幾個(gè)像下面這樣的字典單詞:
root@kerneltalks # xkcdpass
broadside unpadded osmosis statistic cosmetics lugged
你可以用這些單詞作為其他命令,比如 md5sum
的輸入,來(lái)獲取隨機(jī)密碼(就像下面這樣),甚至你也可以用每個(gè)單詞的第 N 個(gè)字母來(lái)生成你的密碼!
root@kerneltalks # xkcdpass |md5sum
45f2ec9b3ca980c7afbd100268c74819 -
root@kerneltalks # xkcdpass |md5sum
ad79546e8350744845c001d8836f2ff2 -
或者你甚至可以把所有單詞串在一起作為一個(gè)超長(zhǎng)的密碼,不僅非常好記,也不容易被電腦程序攻破。
Linux 上還有像 Diceware、 KeePassX、 Revelation、 PasswordMaker 這樣的工具,也可以考慮用來(lái)生成強(qiáng)隨機(jī)密碼。