Linux中怎么生成随机密码

发布时间:2021-08-11 16:17:08 作者:Leah
来源:亿速云 阅读:115

Linux中怎么生成随机密码,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

使用 mkpasswd 实用程序生成密码

mkpasswd 在基于 RHEL 的系统上随 expect 软件包一起安装。在基于 Debian 的系统上 mkpasswd 则在软件包 whois 中。直接安装 mkpasswd 软件包将会导致错误:

所以按照上面所述安装他们的父软件包,就没问题了。

运行 mkpasswd 来获得密码

root@kerneltalks# mkpasswd << on RHELzt*hGW65c root@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 Ooh2tietcootad7O Gohci0vo wah9Thoh Ohh4Ziur Ao1thoma ojoo6aeW Oochai4v ialaiLo5aic2OaDa iexieQu8 Aesoh5Ie Eixou9ph ShiKoh0i uThohth7 taaN3fuu Iege0aeZcah4zaiW Eephei0m AhTh8guo xah2Shoo uh8Iengo aifeev4E zoo4ohHa fieDei6caorieP7k ahna9AKe uveeX7Hi Ohji5pho AigheV7u Akee9fae aeWeiW4a tiex8Oht

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

使用 gpg 工具生成密码

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

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

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

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

使用 xkcdpass 生成密码

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

Linux中怎么生成随机密码

所有的安装使用说明都在上面那个页面提及了。这里是安装步骤和我的测试 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: &lsquo;xkcdpass-1.14.3.tar.gz&rsquo; 100%[==============================================================================================================================>] 871,848     --.-K/s   in 0.01s 2018-01-23 19:09:17 (63.9 MB/s) - &lsquo;xkcdpass-1.14.3.tar.gz&rsquo; 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.txt  root@kerneltalks # cd xkcdpass-1.14.3 root@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/bin Installed /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中怎么生成随机密码的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. JS生成随机密码
  2. php 生成随机密码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux

上一篇:Linux中怎么清空或删除大文件内容

下一篇:Linux中怎么创建或扩展交换分区

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》