当前位置: 首页 > 科技观察

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

时间:2023-03-18 14:37:54 科技观察

学习使用8个本机Linux命令或第三方实用程序来生成随机密码。在本文中,我们将带您了解在Linux终端中生成随机密码的几种不同方法。其中一些使用本机Linux命令,其他使用第三方工具或实用程序,这些工具或实用程序非常容易安装在Linux机器上。在这里,我们使用原生命令,如openssl、dd、md5sum、tr、urandom和第三方工具,如mkpasswd、randpw、pwgen、spw、gpg、xkcdpass、diceware、revelation、keepaasx、passwordmaker。从本质上讲,这些方法都是生成一些随机的字母串,可以用作密码。新用户的密码可以使用随机密码,无论用户群有多大,这些密码都是有保障的。事不宜迟,让我们来看看在Linux上生成随机密码的8种不同方法。使用mkpasswd实用程序生成密码mkpasswd与基于RHEL的系统上的expect软件包一起安装。在基于Debian的系统上,mkpasswd位于whois包中。直接安装mkpasswd包会报错:RHELsystems:Packagemkpasswdisnotavailable。Debian系统:错误:无法找到软件包mkpasswd。所以按照上面的描述安装它们的父包,你会没事的。运行mkpasswd以获取密码root@kerneltalks#mkpasswd<<在RHELzt*hGW65croot@kerneltalks#mkpasswdteststring<<在UbuntuXnlrKxYOJ3vik上此命令在不同系统上的行为不同,因此它以不同的方式工作。还可以通过参数控制长度等选项,可以查阅man手册探索。使用openssl生成密码几乎所有的Linux发行版都包含openssl。我们可以利用它的随机特性来生成随机的字母串作为密码。root@kerneltalks#opensslrand-base6410nU9LlHO5nsuUvw==这里我们使用base64对随机函数进行编码,最后一个数字参数表示长度。使用urandom生成加密设备文件/dev/urandom是另一种获取随机字符串的方法。我们使用tr函数并裁剪输出以获取随机字符串并将其用作密码。root@kerneltalks#字符串/dev/urandom|tr-dcA-Za-z0-9|头-c20;echoUiXtr0NAOSIkqtjK4c0X使用dd命令生成密码我们甚至可以使用/dev/urandom设备和dd命令来获取随机字符串。root@kerneltalks#ddif=/dev/urandombs=1count=15|base64-w015+0recordsin15+0recordsout15bytes(15B)copied,5.5484e-05s,270kB/sQMsbe2XbrqAc2NmXp8D0我们需要结果经过base64编码以使其易于阅读。您可以使用数值来获得所需的长度。对于更简洁的输出,您可以将“标准错误输出”重定向到/dev/null。简洁输出的命令是:root@kerneltalks#ddif=/dev/urandombs=1count=152>/dev/null|base64-w0F8c3a4joS+a3BdPN9C++使用md5sum生成密码另一种获取随机数的方法可以作为密码字符串的方法就是计算MD5校验值!校验值确实看起来像一个随机字符串放在一起,我们可以将其用作密码。确保您的计算源是一个变量,以便每次运行命令时生成的校验和都不同。喜欢约会!date命令总是产生不同的输出。root@kerneltalks#date|md5sum4d8ce5c42073c7e9ca4aeffd3d157102-这里我们通过md5sum得到date命令输出的校验和!您也可以使用cut命令将其切割成您需要的长度。使用pwgen生成密码pwgen包在像EPEL这样??的repo中。pwgen更侧重于生成可发音的密码,但它们不在英语词典中,也不是纯英语。此工具可能不包含在标准分发存储库中。安装此软件包并运行pwgen命令行。Boom!root@kerneltalks#pwgenthu8Iox7ahDeeQu8Eexoh0aioD8oozieooPaeD9tmeeNeiW2Eip6iephOoh1tietcootad7OGohci0vowah9ThohOhh3ZiurAo1thomaojoo6aeWOochai4vialaiLo5aic2OaDaiexieQu8Aesoh4IeEixou9phShiKoh0iuThohth7taaN3fuuIege0aeZcah3zaiWEephei0mAhTh8guoxah1Shoouh8Iengoaifeev4Ezoo4ohHafieDei6caorieP7kahna9AKeuveeX7HiOhji5phoAigheV7uAkee9faeaeWeiW4atiex8Oht你的终端会呈现出一个密码列表!你还想要什么?好的。如果您还想更仔细地探索,pwgen有许多自定义选项,可以在手册页中找到。使用gpg工具生成密码GPG是一种遵循OpenPGP标准的加密和签名工具。大多数gpg工具都是预装的(至少在我的RHEL7上)。但如果没有,您可以寻找gpg或gpg2包并安装它。使用以下命令从gpg工具生成密码。root@kerneltalks#gpg--gen-random--armor112mL8i+PKZ3IuN6a7a这里我们通过了生成随机字节序列的选项(--gen-random),质量为1(第一个参数),数量为times是12(第一个参数第二个参数)。选项--armor保证base64编码输出。使用xkcdpass生成密码著名的极客幽默网站xkcd有一篇非常有趣的文章,介绍了令人难忘但复杂的密码。你可以在这里读到它。所以xkcdpass这个工具就是受到了这篇文章的启发,做了这么一个工作!这是一个Python包,可以在此处的Python官方网站上找到。上页中提到了所有安装和使用说明。以下是安装步骤和我的测试RHEL服务器的输出以供参考。root@kerneltalks#wgethttps://pypi.python.org/packages/b4/d7/3253bd2964390e034cf0bba227db96d94de361454530dc056d8c1c096abc/xkcdpass-1.14.3.tar.gz#md5=5f15d52f1d36207b07391f7a25c7965f--2018-01-2319:09:17--https://pypi.python.org/packages/b4/d7/3253bd2964390e034cf0bba227db96d94de361454530dc056d8c1c096abc/xkcdpass-1.14.3.tar.gzResolvingpypi.python.org(pypi.python.org)...152.4203:4.2038::223正在连接到pypi.python.org(pypi.python.org)|151.101.32.223|:443...已连接。已发送HTTP请求,正在等待响应...200OKLength:871848(851K)[二进制/八位字节-流]保存到:'xkcdpass-1.14.3.tar.gz'100%[==========================================================================================================================>]871,848--.-K/sin0.01s2018-01-2319:09:17(63.9MB/s)-'xkcdpass-1.14.3.tar.gz'已保存[871848/871848]root@kerneltalks#tar-xvfxkcdpass-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#cdxkcdpass-1.14.3root@kerneltalks#pythonsetup.pyinstallrunninginstallrunningbdist_eggrunningegg_infowritingxkcdpass.egg-info/PKG复制代码-INFO将顶级名称写入xkcdpass.egg-info/top_level.txt将dependency_links写入xkcdpass.egg-info/dependency_links.txt将入口点写入xkcdpass.egg-info/entry_points.txt读取清单文件'xkcdpass.egg-info/SOURCES。txt'读取清单模板'MANIFEST.in'写入清单文件'xkcdpass.egg-info/SOURCES.txt'安装库代码构建/bdist.linux-x86_64/eggrunninginstall_librunningbuild_pycreatingbuildcreatingbuild/libcreatingbuild/lib/xkcdpasscopyingxkcdpass/xkcd_password.py->build/lib/xkcdpasscopyingxkcdpass/__init__.py->build/lib/xkcdpasscreating构建/lib/xkcdpass/staticcopyingxkcdpass/static/eff-long->构建/lib/xkcdpass/staticcopyingxkcdpass/static/eff-short->build/lib/xkcdpass/staticcopyingxkcdpass/static/eff-special->build/lib/xkcdpass/staticcopyingxkcdpass/static/fin-kotus->build/lib/xkcdpass/staticcopyingxkcdpass/static/ita-wiki->build/lib/xkcdpass/staticcopyingxkcdpass/static/legacy->构建/lib/xkcdpass/staticcopyingxkcdpass/static/spa-mich->build/lib/xkcdpass/staticcreatingbuild/bdist.linux-x86_64creatingbuild/bdist.linux-x86_64/eggcreatingbuild/bdist.linux-x86_64/egg/xkcdpasscopyingbuild/lib/xkcdpass/xkcd_password.py->build/bdist.linux-x86_64/egg/xkcdpasscopyingbuild/lib/xkcdpass/__init__.py->build/bdist.linux-x86_64/egg/xkcdpasscreatingbuild/bdist.linux-x86_64/egg/xkcdpass/staticcopyingbuild/lib/xkcdpass/static/eff-long->build/bdist.linux-x86_64/egg/xkcdpass/staticcopyingbuild/lib/xkcdpass/static/eff-short->build/bdist.linux-x86_64/egg/xkcdpass/staticcopyingbuild/lib/xkcdpass/static/eff-special->build/bdist.linux-x86_64/egg/xkcdpass/staticcopyingbuild/lib/xkcdpass/static/fin-kotus->build/bdist.linux-x86_64/egg/xkcdpass/staticcopyingbuild/lib/xkcdpass/static/ita-wiki->build/bdist.linux-x86_64/egg/xkcdpass/staticcopyingbuild/lib/xkcdpass/static/legacy->build/bdist.linux-x86_64/egg/xkcdpass/staticcopyingbuild/lib/xkcdpass/static/spa-mich->build/bdist.linux-x86_64/egg/xkcdpass/staticbyte-编译build/bdist.linux-x86_64/egg/xkcdpass/xkcd_password.pytoxkcd_password.pycbyte-编译build/bdist.linux-x86_64/egg/xkcdpass/__init__.pyto__init__.pyccreatingbuild/bdist.linux-x86_64/egg/EGG-INFO复制xkcdpass.egg-info/PKG-INFO->build/bdist.linux-x86_64/egg/EGG-INFO复制xkcdpass.egg-info/SOURCES.txt->build/bdist.linux-x86_64/egg/EGG-信息复制xkcdpass.egg-info/dependency_links.txt->构建/bdist.linux-x86_64/egg/EGG-INFOcopyingxkcdpass.egg-info/entry_points.txt->build/bdist.linux-x86_64/egg/EGG-INFOcopyingxkcdpass.egg-info/not-zip-safe->build/bdist.linux-x86_64/egg/EGG-INFO复制xkcdpass.egg-info/top_level.txt->build/bdist.linux-x86_64/egg/EGG-INFOcreatingdistcreating'dist/xkcdpass-1.14.3-py2.7.egg'并将'build/bdist.linux-x86_64/egg'添加到itremoving'build/bdist.linux-x86_64/egg'(以及它下面的所有内容)处理xkcdpass-1.14.3-py2.7.egg创建/usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.egg提取xkcdpass-1.14.3-py2.7.egg到/usr/lib/python2.7/site-packages添加xkcdpass1.14.3到easy-install.pth文件Installingxkcdpass脚本到/usr/binInstalled/usr/lib/python2.7/site-packages/xkcdpass-1.14.3-py2.7.eggProcessingxkcdpass的依赖项==1.14.3Finishedprocessingdependenciesforxkcdpass==1.14.3现在运行xkcdpass命令,它会随机给你一些字典单词,如下所示:root@kerneltalks#xkcdpassbroadside取消填充edosmosisstatisticcosmeticslugged你可以使用这些词作为其他命令的输入,比如md5sum来获得随机密码(如下所示),你甚至可以使用每个词的第N个字母来生成你的密码!root@kerneltalks#xkcdpass|md5sum45f2ec9b3ca980c7afbd100268c74819-root@kerneltalks#xkcdpass|md5sumad79546e8350744845c001d8836f2ff2-或者你甚至可以把所有的单词串起来作为一个超长密码,它不仅容易被计算机记住,而且在像Dicesware这样的Linuxe工具上也很容易记住。,PasswordMaker也可以考虑用于生成强随机密码。