八種幫你在Linux上生成隨機密碼的方法

學習使用 8 種 Linux 原生命令或第三方實用程序來生成隨機密碼。
在這篇文章中,我們將引導你通過幾種不同的方式在 Linux 終端中生成隨機密碼。其中幾種利用原生 Linux 命令,另外幾種則利用極易在 Linux 機器上安裝的第三方工具或實用程序實現。在這里我們利用像 openssl, dd, md5sum, tr, urandom 這樣的原生命令和 mkpasswd,randpw,pwgen,spw,gpg,xkcdpass,diceware,revelation,keepaasx,passwordmaker 這樣的第三方工具。
其實這些方法就是生成一些能被用作密碼的隨機字母字符串。隨機密碼可以用于新用戶的密碼,不管用戶基數有多大,這些密碼都是***的。話不多說,讓我們來看看 8 種不同的在 Linux 上生成隨機密碼的方法吧。
使用 mkpasswd 實用程序生成密碼
mkpasswd 在基于 RHEL 的系統上隨 expect 軟件包一起安裝。在基于 Debian 的系統上 mkpasswd 則在軟件包 whois 中。直接安裝 mkpasswd 軟件包將會導致錯誤:
- RHEL 系統:軟件包 mkpasswd 不可用。
- Debian 系統:錯誤:無法定位軟件包 mkpasswd。
所以按照上面所述安裝他們的父軟件包,就沒問題了。
運行 mkpasswd 來獲得密碼
root@kerneltalks# mkpasswd << on RHELzt*hGW65croot@kerneltalks# mkpasswd teststring << on UbuntuXnlrKxYOJ3vik
這個命令在不同的系統上表現得不一樣,所以工作方式各異。你也可以通過參數來控制長度等選項,可以查閱 man 手冊來探索。
使用 openssl 生成密碼
幾乎所有 Linux 發行版都包含 openssl。我們可以利用它的隨機功能來生成可以用作密碼的隨機字母字符串。
root@kerneltalks # openssl rand -base64 10nU9LlHO5nsuUvw==
這里我們使用 base64 編碼隨機函數,***一個數字參數表示長度。
使用 urandom 生成密碼
設備文件 /dev/urandom 是另一個獲得隨機字符串的方法。我們使用 tr 功能并裁剪輸出來獲得隨機字符串,并把它作為密碼。
root@kerneltalks # strings /dev/urandom |tr -dc A-Za-z0-9 | head -c20; echoUiXtr0NAOSIkqtjK4c0X
使用 dd 命令生成密碼
我們甚至可以使用 /dev/urandom 設備配合 dd 命令 來獲取隨機字符串。
root@kerneltalks# dd if=/dev/urandom bs=1 count=15|base64 -w 015+0 records in15+0 records out15 bytes (15 B) copied, 5.5484e-05 s, 270 kB/sQMsbe2XbrqAc2NmXp8D0
我們需要將結果通過 base64 編碼使它能被人類可讀。你可以使用數值來獲取想要的長度。想要獲得更簡潔的輸出的話,可以將“標準錯誤輸出”重定向到 /dev/null。簡潔輸出的命令是:
root@kerneltalks # dd if=/dev/urandom bs=1 count=15 2>/dev/null|base64 -w 0F8c3a4joS+a3BdPN9C++
使用 md5sum 生成密碼
另一種獲取可用作密碼的隨機字符串的方法是計算 MD5 校驗值!校驗值看起來確實像是隨機字符串組合在一起,我們可以用作密碼。確保你的計算源是個變量,這樣的話每次運行命令時生成的校驗值都不一樣。比如 date !date 命令 總會生成不同的輸出。
root@kerneltalks # date |md5sum4d8ce5c42073c7e9ca4aeffd3d157102 -
在這里我們將 date 命令的輸出通過 md5sum 得到了校驗和!你也可以用 cut 命令 裁剪你需要的長度。
使用 pwgen 生成密碼
pwgen 軟件包在類似 EPEL 軟件倉庫(LCTT 譯注:企業版 Linux 附加軟件包)中。pwgen 更專注于生成可發音的密碼,但它們不在英語詞典中,也不是純英文的。標準發行版倉庫中可能并不包含這個工具。安裝這個軟件包然后運行 pwgen 命令行。Boom !
root@kerneltalks # pwgenthu8Iox7 ahDeeQu8 Eexoh0ai oD8oozie ooPaeD9t meeNeiW2 Eip6ieph Ooh1tietcootad7O Gohci0vo wah9Thoh Ohh3Ziur Ao1thoma ojoo6aeW Oochai4v ialaiLo5aic2OaDa iexieQu8 Aesoh4Ie Eixou9ph ShiKoh0i uThohth7 taaN3fuu Iege0aeZcah3zaiW Eephei0m AhTh8guo xah1Shoo uh8Iengo aifeev4E zoo4ohHa fieDei6caorieP7k ahna9AKe uveeX7Hi Ohji5pho AigheV7u Akee9fae aeWeiW4a tiex8Oht
你的終端會呈現出一個密碼列表!你還想要什么呢?好吧。你還想再仔細探索的話, pwgen 還有很多自定義選項,這些都可以在 man 手冊里查閱到。
使用 gpg 工具生成密碼
GPG 是一個遵循 OpenPGP 標準的加密及簽名工具。大部分 gpg 工具都預先被安裝好了(至少在我的 RHEL7 上是這樣)。但如果沒有的話你可以尋找 gpg 或 gpg2 軟件包并安裝它。
使用下面的命令以從 gpg 工具生成密碼。
root@kerneltalks # gpg --gen-random --armor 1 12mL8i+PKZ3IuN6a7a
這里我們傳了生成隨機字節序列選項(--gen-random),質量為 1(***個參數),次數 12 (第二個參數)。選項 --armor 保證以 base64 編碼輸出。
使用 xkcdpass 生成密碼
著名的極客幽默網站 xkcd,發表了一篇非常有趣的文章,是關于好記但又復雜的密碼的。你可以在這里閱讀。所以 xkcdpass 工具就受這篇文章啟發,做了這樣的工作!這是一個 Python 軟件包,可以在這里的 Python 的官網上找到它。

所有的安裝使用說明都在上面那個頁面提及了。這里是安裝步驟和我的測試 RHEL 服務器的輸出,以供參考。
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.gzResolving pypi.python.org (pypi.python.org)... 151.101.32.223, 2a04:4e42:8::223Connecting to pypi.python.org (pypi.python.org)|151.101.32.223|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 871848 (851K) [binary/octet-stream]Saving to: ‘xkcdpass-1.14.3.tar.gz’100%[==============================================================================================================================>] 871,848 --.-K/s in 0.01s2018-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.gzxkcdpass-1.14.3/xkcdpass-1.14.3/examples/xkcdpass-1.14.3/examples/example_import.pyxkcdpass-1.14.3/examples/example_json.pyxkcdpass-1.14.3/examples/example_postprocess.pyxkcdpass-1.14.3/LICENSE.BSDxkcdpass-1.14.3/MANIFEST.inxkcdpass-1.14.3/PKG-INFOxkcdpass-1.14.3/README.rstxkcdpass-1.14.3/setup.cfgxkcdpass-1.14.3/setup.pyxkcdpass-1.14.3/tests/xkcdpass-1.14.3/tests/test_list.txtxkcdpass-1.14.3/tests/test_xkcdpass.pyxkcdpass-1.14.3/tests/__init__.pyxkcdpass-1.14.3/xkcdpass/xkcdpass-1.14.3/xkcdpass/static/xkcdpass-1.14.3/xkcdpass/static/eff-longxkcdpass-1.14.3/xkcdpass/static/eff-shortxkcdpass-1.14.3/xkcdpass/static/eff-specialxkcdpass-1.14.3/xkcdpass/static/fin-kotusxkcdpass-1.14.3/xkcdpass/static/ita-wikixkcdpass-1.14.3/xkcdpass/static/legacyxkcdpass-1.14.3/xkcdpass/static/spa-michxkcdpass-1.14.3/xkcdpass/xkcd_password.pyxkcdpass-1.14.3/xkcdpass/__init__.pyxkcdpass-1.14.3/xkcdpass.1xkcdpass-1.14.3/xkcdpass.egg-info/xkcdpass-1.14.3/xkcdpass.egg-info/dependency_links.txtxkcdpass-1.14.3/xkcdpass.egg-info/entry_points.txtxkcdpass-1.14.3/xkcdpass.egg-info/not-zip-safexkcdpass-1.14.3/xkcdpass.egg-info/PKG-INFOxkcdpass-1.14.3/xkcdpass.egg-info/SOURCES.txtxkcdpass-1.14.3/xkcdpass.egg-info/top_level.txtroot@kerneltalks # cd xkcdpass-1.14.3root@kerneltalks # python setup.py installrunning installrunning bdist_eggrunning egg_infowriting xkcdpass.egg-info/PKG-INFOwriting top-level names to xkcdpass.egg-info/top_level.txtwriting dependency_links to xkcdpass.egg-info/dependency_links.txtwriting entry points to xkcdpass.egg-info/entry_points.txtreading 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/eggrunning install_librunning build_pycreating buildcreating build/libcreating build/lib/xkcdpasscopying xkcdpass/xkcd_password.py -> build/lib/xkcdpasscopying xkcdpass/__init__.py -> build/lib/xkcdpasscreating build/lib/xkcdpass/staticcopying xkcdpass/static/eff-long -> build/lib/xkcdpass/staticcopying xkcdpass/static/eff-short -> build/lib/xkcdpass/staticcopying xkcdpass/static/eff-special -> build/lib/xkcdpass/staticcopying xkcdpass/static/fin-kotus -> build/lib/xkcdpass/staticcopying xkcdpass/static/ita-wiki -> build/lib/xkcdpass/staticcopying xkcdpass/static/legacy -> build/lib/xkcdpass/staticcopying xkcdpass/static/spa-mich -> build/lib/xkcdpass/staticcreating build/bdist.linux-x86_64creating build/bdist.linux-x86_64/eggcreating build/bdist.linux-x86_64/egg/xkcdpasscopying build/lib/xkcdpass/xkcd_password.py -> build/bdist.linux-x86_64/egg/xkcdpasscopying build/lib/xkcdpass/__init__.py -> build/bdist.linux-x86_64/egg/xkcdpasscreating build/bdist.linux-x86_64/egg/xkcdpass/staticcopying build/lib/xkcdpass/static/eff-long -> build/bdist.linux-x86_64/egg/xkcdpass/staticcopying build/lib/xkcdpass/static/eff-short -> build/bdist.linux-x86_64/egg/xkcdpass/staticcopying build/lib/xkcdpass/static/eff-special -> build/bdist.linux-x86_64/egg/xkcdpass/staticcopying build/lib/xkcdpass/static/fin-kotus -> build/bdist.linux-x86_64/egg/xkcdpass/staticcopying build/lib/xkcdpass/static/ita-wiki -> build/bdist.linux-x86_64/egg/xkcdpass/staticcopying build/lib/xkcdpass/static/legacy -> build/bdist.linux-x86_64/egg/xkcdpass/staticcopying build/lib/xkcdpass/static/spa-mich -> build/bdist.linux-x86_64/egg/xkcdpass/staticbyte-compiling build/bdist.linux-x86_64/egg/xkcdpass/xkcd_password.py to xkcd_password.pycbyte-compiling build/bdist.linux-x86_64/egg/xkcdpass/__init__.py to __init__.pyccreating build/bdist.linux-x86_64/egg/EGG-INFOcopying xkcdpass.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFOcopying xkcdpass.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFOcopying xkcdpass.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFOcopying xkcdpass.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFOcopying xkcdpass.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFOcopying xkcdpass.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFOcreating distcreating 'dist/xkcdpass-1.14.3-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to itremoving 'build/bdist.linux-x86_64/egg' (and everything under it)Processing xkcdpass-1.14.3-py2.7.eggcreating /usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.eggExtracting xkcdpass-1.14.3-py2.7.egg to /usr/lib/python2.7/site-packagesAdding xkcdpass 1.14.3 to easy-install.pth fileInstalling xkcdpass script to /usr/binInstalled /usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.eggProcessing dependencies for xkcdpass==1.14.3Finished processing dependencies for xkcdpass==1.14.3
現在運行 xkcdpass 命令,將會隨機給出你幾個像下面這樣的字典單詞:
root@kerneltalks # xkcdpassbroadside unpadded osmosis statistic cosmetics lugged
你可以用這些單詞作為其他命令,比如 md5sum 的輸入,來獲取隨機密碼(就像下面這樣),甚至你也可以用每個單詞的第 N 個字母來生成你的密碼!
root@kerneltalks # xkcdpass |md5sum45f2ec9b3ca980c7afbd100268c74819 -root@kerneltalks # xkcdpass |md5sumad79546e8350744845c001d8836f2ff2 -
或者你甚至可以把所有單詞串在一起作為一個超長的密碼,不僅非常好記,也不容易被電腦程序攻破。
Linux 上還有像 Diceware、 KeePassX、 Revelation、 PasswordMaker 這樣的工具,也可以考慮用來生成強隨機密碼。

























