八种帮你在Linux上生成随机密码的方法

系统 Linux
在这篇文章中,我们将引导你通过几种不同的方式在 Linux 终端中生成随机密码。其中几种利用原生 Linux 命令,另外几种则利用极易在 Linux 机器上安装的第三方工具或实用程序实现。

 https://s5.51cto.com/oss/201802/07/0216c08682255123b4accc2c1763474f.png

学习使用 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. root@kerneltalks# mkpasswd << on RHEL
  2. zt*hGW65c
  3.  
  4. root@kerneltalks# mkpasswd teststring << on Ubuntu
  5. XnlrKxYOJ3vik

这个命令在不同的系统上表现得不一样,所以工作方式各异。你也可以通过参数来控制长度等选项,可以查阅 man 手册来探索。

使用 openssl 生成密码

几乎所有 Linux 发行版都包含 openssl。我们可以利用它的随机功能来生成可以用作密码的随机字母字符串。

  1. root@kerneltalks # openssl rand -base64 10
  2. nU9LlHO5nsuUvw==

这里我们使用 base64 编码随机函数,***一个数字参数表示长度。

使用 urandom 生成密码

设备文件 /dev/urandom 是另一个获得随机字符串的方法。我们使用 tr 功能并裁剪输出来获得随机字符串,并把它作为密码。

  1. root@kerneltalks # strings /dev/urandom |tr -dc A-Za-z0-9 | head -c20; echo
  2. UiXtr0NAOSIkqtjK4c0X

使用 dd 命令生成密码

我们甚至可以使用 /dev/urandom 设备配合 dd 命令 来获取随机字符串。

  1. root@kerneltalks# dd if=/dev/urandom bs=1 count=15|base64 -w 0
  2. 15+0 records in
  3. 15+0 records out
  4. 15 bytes (15 B) copied, 5.5484e-05 s, 270 kB/s
  5. QMsbe2XbrqAc2NmXp8D0

我们需要将结果通过 base64 编码使它能被人类可读。你可以使用数值来获取想要的长度。想要获得更简洁的输出的话,可以将“标准错误输出”重定向到 /dev/null。简洁输出的命令是:

  1. root@kerneltalks # dd if=/dev/urandom bs=1 count=15 2>/dev/null|base64 -w 0
  2. F8c3a4joS+a3BdPN9C++

使用 md5sum 生成密码

另一种获取可用作密码的随机字符串的方法是计算 MD5 校验值!校验值看起来确实像是随机字符串组合在一起,我们可以用作密码。确保你的计算源是个变量,这样的话每次运行命令时生成的校验值都不一样。比如 datedate 命令 总会生成不同的输出。

  1. root@kerneltalks # date |md5sum
  2. 4d8ce5c42073c7e9ca4aeffd3d157102 -

在这里我们将 date 命令的输出通过 md5sum 得到了校验和!你也可以用 cut 命令 裁剪你需要的长度。

使用 pwgen 生成密码

pwgen 软件包在类似 EPEL 软件仓库(LCTT 译注:企业版 Linux 附加软件包)中。pwgen 更专注于生成可发音的密码,但它们不在英语词典中,也不是纯英文的。标准发行版仓库中可能并不包含这个工具。安装这个软件包然后运行 pwgen 命令行。Boom !

  1. root@kerneltalks # pwgen
  2. thu8Iox7 ahDeeQu8 Eexoh0ai oD8oozie ooPaeD9t meeNeiW2 Eip6ieph Ooh1tiet
  3. cootad7O Gohci0vo wah9Thoh Ohh3Ziur Ao1thoma ojoo6aeW Oochai4v ialaiLo5
  4. aic2OaDa iexieQu8 Aesoh4Ie Eixou9ph ShiKoh0i uThohth7 taaN3fuu Iege0aeZ
  5. cah3zaiW Eephei0m AhTh8guo xah1Shoo uh8Iengo aifeev4E zoo4ohHa fieDei6c
  6. aorieP7k ahna9AKe uveeX7Hi Ohji5pho AigheV7u Akee9fae aeWeiW4a tiex8Oht

你的终端会呈现出一个密码列表!你还想要什么呢?好吧。你还想再仔细探索的话, pwgen 还有很多自定义选项,这些都可以在 man 手册里查阅到。

使用 gpg 工具生成密码

GPG 是一个遵循 OpenPGP 标准的加密及签名工具。大部分 gpg 工具都预先被安装好了(至少在我的 RHEL7 上是这样)。但如果没有的话你可以寻找 gpggpg2 软件包并安装它。

使用下面的命令以从 gpg 工具生成密码。

  1. root@kerneltalks # gpg --gen-random --armor 1 12
  2. mL8i+PKZ3IuN6a7a

这里我们传了生成随机字节序列选项(--gen-random),质量为 1(***个参数),次数 12 (第二个参数)。选项 --armor 保证以 base64 编码输出。

使用 xkcdpass 生成密码

著名的极客幽默网站 xkcd,发表了一篇非常有趣的文章,是关于好记但又复杂的密码的。你可以在这里阅读。所以 xkcdpass 工具就受这篇文章启发,做了这样的工作!这是一个 Python 软件包,可以在这里的 Python 的官网上找到它。

所有的安装使用说明都在上面那个页面提及了。这里是安装步骤和我的测试 RHEL 服务器的输出,以供参考。

  1. root@kerneltalks # wget https://pypi.python.org/packages/b4/d7/3253bd2964390e034cf0bba227db96d94de361454530dc056d8c1c096abc/xkcdpass-1.14.3.tar.gz#md5=5f15d52f1d36207b07391f7a25c7965f
  2. --2018-01-23 19:09:17-- https://pypi.python.org/packages/b4/d7/3253bd2964390e034cf0bba227db96d94de361454530dc056d8c1c096abc/xkcdpass-1.14.3.tar.gz
  3. Resolving pypi.python.org (pypi.python.org)... 151.101.32.223, 2a04:4e42:8::223
  4. Connecting to pypi.python.org (pypi.python.org)|151.101.32.223|:443... connected.
  5. HTTP request sent, awaiting response... 200 OK
  6. Length: 871848 (851K) [binary/octet-stream]
  7. Saving to: xkcdpass-1.14.3.tar.gz
  8.  
  9. 100%[==============================================================================================================================>] 871,848 --.-K/s in 0.01s
  10.  
  11. 2018-01-23 19:09:17 (63.9 MB/s) - xkcdpass-1.14.3.tar.gz saved [871848/871848]
  12.  
  13.  
  14. root@kerneltalks # tar -xvf xkcdpass-1.14.3.tar.gz
  15. xkcdpass-1.14.3/
  16. xkcdpass-1.14.3/examples/
  17. xkcdpass-1.14.3/examples/example_import.py
  18. xkcdpass-1.14.3/examples/example_json.py
  19. xkcdpass-1.14.3/examples/example_postprocess.py
  20. xkcdpass-1.14.3/LICENSE.BSD
  21. xkcdpass-1.14.3/MANIFEST.in
  22. xkcdpass-1.14.3/PKG-INFO
  23. xkcdpass-1.14.3/README.rst
  24. xkcdpass-1.14.3/setup.cfg
  25. xkcdpass-1.14.3/setup.py
  26. xkcdpass-1.14.3/tests/
  27. xkcdpass-1.14.3/tests/test_list.txt
  28. xkcdpass-1.14.3/tests/test_xkcdpass.py
  29. xkcdpass-1.14.3/tests/__init__.py
  30. xkcdpass-1.14.3/xkcdpass/
  31. xkcdpass-1.14.3/xkcdpass/static/
  32. xkcdpass-1.14.3/xkcdpass/static/eff-long
  33. xkcdpass-1.14.3/xkcdpass/static/eff-short
  34. xkcdpass-1.14.3/xkcdpass/static/eff-special
  35. xkcdpass-1.14.3/xkcdpass/static/fin-kotus
  36. xkcdpass-1.14.3/xkcdpass/static/ita-wiki
  37. xkcdpass-1.14.3/xkcdpass/static/legacy
  38. xkcdpass-1.14.3/xkcdpass/static/spa-mich
  39. xkcdpass-1.14.3/xkcdpass/xkcd_password.py
  40. xkcdpass-1.14.3/xkcdpass/__init__.py
  41. xkcdpass-1.14.3/xkcdpass.1
  42. xkcdpass-1.14.3/xkcdpass.egg-info/
  43. xkcdpass-1.14.3/xkcdpass.egg-info/dependency_links.txt
  44. xkcdpass-1.14.3/xkcdpass.egg-info/entry_points.txt
  45. xkcdpass-1.14.3/xkcdpass.egg-info/not-zip-safe
  46. xkcdpass-1.14.3/xkcdpass.egg-info/PKG-INFO
  47. xkcdpass-1.14.3/xkcdpass.egg-info/SOURCES.txt
  48. xkcdpass-1.14.3/xkcdpass.egg-info/top_level.txt
  49.  
  50.  
  51. root@kerneltalks # cd xkcdpass-1.14.3
  52.  
  53. root@kerneltalks # python setup.py install
  54. running install
  55. running bdist_egg
  56. running egg_info
  57. writing xkcdpass.egg-info/PKG-INFO
  58. writing top-level names to xkcdpass.egg-info/top_level.txt
  59. writing dependency_links to xkcdpass.egg-info/dependency_links.txt
  60. writing entry points to xkcdpass.egg-info/entry_points.txt
  61. reading manifest file 'xkcdpass.egg-info/SOURCES.txt'
  62. reading manifest template 'MANIFEST.in'
  63. writing manifest file 'xkcdpass.egg-info/SOURCES.txt'
  64. installing library code to build/bdist.linux-x86_64/egg
  65. running install_lib
  66. running build_py
  67. creating build
  68. creating build/lib
  69. creating build/lib/xkcdpass
  70. copying xkcdpass/xkcd_password.py -> build/lib/xkcdpass
  71. copying xkcdpass/__init__.py -> build/lib/xkcdpass
  72. creating build/lib/xkcdpass/static
  73. copying xkcdpass/static/eff-long -> build/lib/xkcdpass/static
  74. copying xkcdpass/static/eff-short -> build/lib/xkcdpass/static
  75. copying xkcdpass/static/eff-special -> build/lib/xkcdpass/static
  76. copying xkcdpass/static/fin-kotus -> build/lib/xkcdpass/static
  77. copying xkcdpass/static/ita-wiki -> build/lib/xkcdpass/static
  78. copying xkcdpass/static/legacy -> build/lib/xkcdpass/static
  79. copying xkcdpass/static/spa-mich -> build/lib/xkcdpass/static
  80. creating build/bdist.linux-x86_64
  81. creating build/bdist.linux-x86_64/egg
  82. creating build/bdist.linux-x86_64/egg/xkcdpass
  83. copying build/lib/xkcdpass/xkcd_password.py -> build/bdist.linux-x86_64/egg/xkcdpass
  84. copying build/lib/xkcdpass/__init__.py -> build/bdist.linux-x86_64/egg/xkcdpass
  85. creating build/bdist.linux-x86_64/egg/xkcdpass/static
  86. copying build/lib/xkcdpass/static/eff-long -> build/bdist.linux-x86_64/egg/xkcdpass/static
  87. copying build/lib/xkcdpass/static/eff-short -> build/bdist.linux-x86_64/egg/xkcdpass/static
  88. copying build/lib/xkcdpass/static/eff-special -> build/bdist.linux-x86_64/egg/xkcdpass/static
  89. copying build/lib/xkcdpass/static/fin-kotus -> build/bdist.linux-x86_64/egg/xkcdpass/static
  90. copying build/lib/xkcdpass/static/ita-wiki -> build/bdist.linux-x86_64/egg/xkcdpass/static
  91. copying build/lib/xkcdpass/static/legacy -> build/bdist.linux-x86_64/egg/xkcdpass/static
  92. copying build/lib/xkcdpass/static/spa-mich -> build/bdist.linux-x86_64/egg/xkcdpass/static
  93. byte-compiling build/bdist.linux-x86_64/egg/xkcdpass/xkcd_password.py to xkcd_password.pyc
  94. byte-compiling build/bdist.linux-x86_64/egg/xkcdpass/__init__.py to __init__.pyc
  95. creating build/bdist.linux-x86_64/egg/EGG-INFO
  96. copying xkcdpass.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
  97. copying xkcdpass.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
  98. copying xkcdpass.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
  99. copying xkcdpass.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
  100. copying xkcdpass.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
  101. copying xkcdpass.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
  102. creating dist
  103. creating 'dist/xkcdpass-1.14.3-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
  104. removing 'build/bdist.linux-x86_64/egg' (and everything under it)
  105. Processing xkcdpass-1.14.3-py2.7.egg
  106. creating /usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.egg
  107. Extracting xkcdpass-1.14.3-py2.7.egg to /usr/lib/python2.7/site-packages
  108. Adding xkcdpass 1.14.3 to easy-install.pth file
  109. Installing xkcdpass script to /usr/bin
  110.  
  111. Installed /usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.egg
  112. Processing dependencies for xkcdpass==1.14.3
  113. Finished processing dependencies for xkcdpass==1.14.3

现在运行 xkcdpass 命令,将会随机给出你几个像下面这样的字典单词:

  1. root@kerneltalks # xkcdpass
  2. broadside unpadded osmosis statistic cosmetics lugged

你可以用这些单词作为其他命令,比如 md5sum 的输入,来获取随机密码(就像下面这样),甚至你也可以用每个单词的第 N 个字母来生成你的密码!

  1. root@kerneltalks # xkcdpass |md5sum
  2. 45f2ec9b3ca980c7afbd100268c74819 -
  3.  
  4. root@kerneltalks # xkcdpass |md5sum
  5. ad79546e8350744845c001d8836f2ff2 -

或者你甚至可以把所有单词串在一起作为一个超长的密码,不仅非常好记,也不容易被电脑程序攻破。

 

Linux 上还有像 DicewareKeePassXRevelationPasswordMaker 这样的工具,也可以考虑用来生成强随机密码。

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2014-08-21 15:26:22

2014-03-04 10:47:52

Linux命令行随机密码

2014-03-27 14:53:33

随机密码

2015-05-04 14:50:48

PHPPHP生成随机密码

2017-02-08 12:00:45

PHP性能对比

2019-03-27 11:30:30

Linux终端密码生成器

2011-02-25 14:14:32

Linux随机密码

2009-07-31 09:00:44

ASP.NET生成随机

2015-03-26 11:51:22

2022-10-17 18:29:55

2022-12-28 15:10:39

LinuxNginx服务器

2022-05-22 13:59:27

Go编程语言

2011-05-19 11:30:00

密码密码生成器

2022-07-06 08:33:15

服务器安全SSH

2019-10-16 00:53:56

物联网设备数据安全物联网安全

2022-08-19 11:17:09

Linux

2019-07-02 13:16:05

密码账号安全数据安全

2020-06-07 11:46:05

密码信息泄露高强度密码

2023-05-04 18:45:11

2011-07-05 09:50:21

点赞
收藏

51CTO技术栈公众号