學習使用 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
來獲得密碼
1 2 3 4 |
root@kerneltalks# mkpasswd << on RHEL zt*hGW65c root@kerneltalks# mkpasswd teststring << on Ubuntu XnlrKxYOJ3vik |
這個命令在不同的系統上表現得不一樣,所以工作方式各異。你也可以通過引數來控制長度等選項,可以查閱 man 手冊來探索。
使用 openssl 生成密碼
幾乎所有 Linux 發行版都包含 openssl。我們可以利用它的隨機功能來生成可以用作密碼的隨機字母字串。
1 2 3 |
root@kerneltalks # openssl rand -base64 10 nU9LlHO5nsuUvw== |
這裡我們使用 base64
編碼隨機函式,最後一個數字參數列示長度。
使用 urandom 生成密碼
裝置檔案 /dev/urandom
是另一個獲得隨機字串的方法。我們使用 tr
功能並裁剪輸出來獲得隨機字串,並把它作為密碼。
1 2 3 |
root@kerneltalks # strings /dev/urandom |tr -dc A-Za-z0-9 | head -c20; echo UiXtr0NAOSIkqtjK4c0X |
使用 dd 命令生成密碼
我們甚至可以使用 /dev/urandom
裝置配合 dd 命令 來獲取隨機字串。
1 2 3 4 5 6 |
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 |
我們需要將結果通過 base64
編碼使它能被人類可讀。你可以使用數值來獲取想要的長度。想要獲得更簡潔的輸出的話,可以將“標準錯誤輸出”重定向到 /dev/null
。簡潔輸出的命令是:
1 2 3 |
root@kerneltalks # dd if=/dev/urandom bs=1 count=15 2>/dev/null|base64 -w 0 F8c3a4joS+a3BdPN9C++ |
使用 md5sum 生成密碼
另一種獲取可用作密碼的隨機字串的方法是計算 MD5 校驗值!校驗值看起來確實像是隨機字串組合在一起,我們可以用作密碼。確保你的計算源是個變數,這樣的話每次執行命令時生成的校驗值都不一樣。比如 date
!date 命令 總會生成不同的輸出。
1 2 3 |
root@kerneltalks # date |md5sum 4d8ce5c42073c7e9ca4aeffd3d157102 - |
在這裡我們將 date
命令的輸出通過 md5sum
得到了校驗和!你也可以用 cut 命令 裁剪你需要的長度。
使用 pwgen 生成密碼
pwgen
軟體包在類似 EPEL 軟體倉庫(LCTT 譯註:企業版 Linux 附加軟體包)中。pwgen
更專注於生成可發音的密碼,但它們不在英語詞典中,也不是純英文的。標準發行版倉庫中可能並不包含這個工具。安裝這個軟體包然後執行 pwgen
命令列。Boom !
1 2 3 4 5 6 7 |
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 |
你的終端會呈現出一個密碼列表!你還想要什麼呢?好吧。你還想再仔細探索的話, pwgen
還有很多自定義選項,這些都可以在 man 手冊裡查閱到。
使用 gpg 工具生成密碼
GPG 是一個遵循 OpenPGP 標準的加密及簽名工具。大部分 gpg 工具都預先被安裝好了(至少在我的 RHEL7 上是這樣)。但如果沒有的話你可以尋找 gpg
或 gpg2
軟體包並安裝它。
使用下面的命令以從 gpg 工具生成密碼。
1 2 3 |
root@kerneltalks # gpg --gen-random --armor 1 12 mL8i+PKZ3IuN6a7a |
這裡我們傳了生成隨機位元組序列選項(--gen-random
),質量為 1(第一個引數),次數 12 (第二個引數)。選項 --armor
保證以 base64
編碼輸出。
使用 xkcdpass 生成密碼
著名的極客幽默網站 xkcd,發表了一篇非常有趣的文章,是關於好記但又複雜的密碼的。你可以在這裡閱讀。所以 xkcdpass
工具就受這篇文章啟發,做了這樣的工作!這是一個 Python 軟體包,可以在這裡的 Python 的官網上找到它。
所有的安裝使用說明都在上面那個頁面提及了。這裡是安裝步驟和我的測試 RHEL 伺服器的輸出,以供參考。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
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 |
現在執行 xkcdpass
命令,將會隨機給出你幾個像下面這樣的字典單詞:
1 2 3 |
root@kerneltalks # xkcdpass broadside unpadded osmosis statistic cosmetics lugged |
你可以用這些單詞作為其他命令,比如 md5sum
的輸入,來獲取隨機密碼(就像下面這樣),甚至你也可以用每個單詞的第 N 個字母來生成你的密碼!
1 2 3 4 5 6 |
root@kerneltalks # xkcdpass |md5sum 45f2ec9b3ca980c7afbd100268c74819 - root@kerneltalks # xkcdpass |md5sum ad79546e8350744845c001d8836f2ff2 - |
或者你甚至可以把所有單詞串在一起作為一個超長的密碼,不僅非常好記,也不容易被電腦程式攻破。
Linux 上還有像 Diceware、 KeePassX、 Revelation、 PasswordMaker 這樣的工具,也可以考慮用來生成強隨機密碼。