Linux / UNIX – 使用 mkpasswd / makepasswd / pwgen 生成随机密码
您可以使用makepasswd或mkpasswd命令在类 Linux / UNIX 操作系统上生成随机密码。
- 该mkpasswd命令是功能丰富的加密函数前端。makepasswd 命令使用 Linux 的 /dev/random 功能生成真正的随机密码,重点是安全性而不是发音性。它还可以加密命令行中给出的明文密码。生成新密码的更新版本,可选择将其应用于用户。
- 该makepasswd命令使用 /dev/urandom 生成真正的随机密码。
- 该pwgen命令生成可发音的密码。
教程详细信息 | |
---|---|
难度等级 | 简单的 |
Root 权限 | 是的 |
要求 | Linux 或 Unix 终端 |
类别 | 终端/ssh |
操作系统兼容性 | BSD • Linux • macOS • Unix |
预计阅读时间 | 4 分钟 |
Linux / UNIX – 使用 mkpasswd / makepasswd / pwgen 命令生成随机密码
默认情况下,您的系统上可能未安装mkpasswd、makepasswd 和 pwgen命令。因此,请使用Alpine Linux 上的apk 命令、 RHEL 和 co 上的dnf 命令/ yum 命令、Debian、Ubuntu 和 co 上的apt 命令/ apt-get 命令、SUSE/OpenSUSE 上的 zypper 命令、Arch Linux 上的 pacman 命令来安装 mkpasswd、makepasswd 和 pwgen。
$ type -a mkpasswd
$ type -a makepasswd
$ type -a pwgen
#!/bin/bash # Name: check_password_bins.sh # Purpose: Find and print message if the CLI password is installed or not your Linux or Unix machine. # Author: Vivek Gite {https://www.example.com/} under GPL v2.x+ # ---------------------------------------------------------------------------------------------------- for p in mkpasswd makepasswd pwgen do if ! type "$p" &>/dev/null then echo "WARNING: '$p' command not found in Linux/Unix \$PATH. Please install it using the package manager or set the correct \$PATH" else echo "FOUND: $(type $p)" fi done
以下是使用 chmod 命令运行 .sh shell 脚本文件的方法:
$ chmod +x check_password_bins.sh
$ ./check_password_bins.sh
安装mkpasswd / makepasswd / pwgen随机密码生成器
在 shell 提示符下输入以下“ apt ”或“ apt-get ”在 Debian / Ubuntu Linux 上安装 mkpasswd、pwgen 和 makepasswd:
$ sudo apt-get install makepasswd
示例输出:
[sudo] password for vivek: Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libbytes-random-secure-perl libcrypt-passwdmd5-perl libcrypt-random-seed-perl libmath-random-isaac-perl libmath-random-isaac-xs-perl The following NEW packages will be installed: libbytes-random-secure-perl libcrypt-passwdmd5-perl libcrypt-random-seed-perl libmath-random-isaac-perl libmath-random-isaac-xs-perl makepasswd 0 upgraded, 6 newly installed, 0 to remove and 15 not upgraded. Need to get 101 kB of archives. After this operation, 321 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 libcrypt-random-seed-perl all 0.03-1 [22.0 kB] Get:2 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 libmath-random-isaac-perl all 1.004-1 [19.2 kB] Get:3 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 libbytes-random-secure-perl all 0.29-1 [26.7 kB] Get:4 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 libcrypt-passwdmd5-perl all 1.40-1 [8,220 B] Get:5 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 libmath-random-isaac-xs-perl amd64 1.004-2build5 [15.0 kB] Get:6 http://in.archive.ubuntu.com/ubuntu focal/universe amd64 makepasswd all 1.10-12 [10.3 kB] Fetched 101 kB in 2s (48.7 kB/s) Selecting previously unselected package libcrypt-random-seed-perl. (Reading database ... 291652 files and directories currently installed.) Preparing to unpack .../0-libcrypt-random-seed-perl_0.03-1_all.deb ... Unpacking libcrypt-random-seed-perl (0.03-1) ... Selecting previously unselected package libmath-random-isaac-perl. Preparing to unpack .../1-libmath-random-isaac-perl_1.004-1_all.deb ... Unpacking libmath-random-isaac-perl (1.004-1) ... Selecting previously unselected package libbytes-random-secure-perl. Preparing to unpack .../2-libbytes-random-secure-perl_0.29-1_all.deb ... Unpacking libbytes-random-secure-perl (0.29-1) ... Selecting previously unselected package libcrypt-passwdmd5-perl. Preparing to unpack .../3-libcrypt-passwdmd5-perl_1.40-1_all.deb ... Unpacking libcrypt-passwdmd5-perl (1.40-1) ... Selecting previously unselected package libmath-random-isaac-xs-perl. Preparing to unpack .../4-libmath-random-isaac-xs-perl_1.004-2build5_amd64.deb ... Unpacking libmath-random-isaac-xs-perl (1.004-2build5) ... Selecting previously unselected package makepasswd. Preparing to unpack .../5-makepasswd_1.10-12_all.deb ... Unpacking makepasswd (1.10-12) ... Setting up libmath-random-isaac-perl (1.004-1) ... Setting up libcrypt-passwdmd5-perl (1.40-1) ... Setting up libcrypt-random-seed-perl (0.03-1) ... Setting up libmath-random-isaac-xs-perl (1.004-2build5) ... Setting up libbytes-random-secure-perl (0.29-1) ... Setting up makepasswd (1.10-12) ... Processing triggers for man-db (2.9.1-1) ...
类似地,您可以在 Debian、Ubuntu 或 Linux Mint OS 上按如下方式安装 pwgen 和 mkpasswd:
$ sudo apt install pwgen
关于在 macOS 或 FreeBSD Unix 上安装 mkpasswd / makepasswd / pwgen 命令的说明
对于 macOS 或 OS X,你需要在 Mac OS 上安装 Homebrew 才能使用 brew 包管理器。例如,在 macOS/OS X 上打开终端应用程序并输入curl 命令下载文件:
FreeBSD 用户可以简单地使用 FreeBSD ports 安装包或 pkg 命令,如下所示:输出:
## 01. Install xcode ##
$ xcode-select --install
## 02. Download brew installer ##
$ curl -O https://raw.githubusercontent.com/Homebrew/install/master/install.sh
$ more install.sh
$ chmod +x install.sh
## 03. Install Homebrew ##
$ ./install.sh
## 04. Usage / install packages ##
$ man brew
$ brew search package_name
$ brew install package_name
$ sudo pkg install pwgen makepasswd
Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 8 package(s) will be affected (of 0 checked): New packages to be INSTALLED: makepasswd: 1.10_7 p5-Bytes-Random-Secure: 0.29 p5-Crypt-PasswdMD5: 1.42 p5-Crypt-Random-Seed: 0.03 p5-Crypt-Random-TESHA2: 0.01 p5-Math-Random-ISAAC: 1.004_2 p5-Math-Random-ISAAC-XS: 1.004_3 pwgen: 2.08,2 Number of packages to be installed: 8 152 KiB to be downloaded. Proceed with this action? [y/N]: y
mkpasswd 命令语法
语法是:
让我们看一些例子。
makepasswd
makepasswd [options]
Linux / UNIX makepasswd 生成随机密码的命令示例
只需输入以下命令:
$ makepasswd
示例输出:
m4peQm97s
要生成长度恰好为 16 个字符的密码,请将 16 选项传递给--chars选项,如下所示:
$ makepasswd --chars 16
示例输出:
cSYhQ55aNRshmBT5
让我们总共生成 7 个密码,而不是默认的一个密码,输入:
$ makepasswd --chars 16 --count 7
示例输出:
W6Lfrus9m08uAL7j gdTiP2F4Td94oyWN qoeezVzHh38m1aYK HI0R66ExHw558L2N 5I6UH7WdUmX9e3Xg XRTLM6V8D37JxWz0 qpvu7VaJecwG4qFb
其他命令行选项如下:
--crypt Produce encrypted passwords. --crypt-md5 Produce encrypted passwords using the MD5 digest (hash) algorithm. --cryptsalt=N Use crypt() salt N, a positive number <= 4096. If random seeds are desired, specify a zero value (the default). --maxchars=N Generate passwords with at most N characters (default=10). --minchars=N Generate passwords with at least N characters (default=8). --nocrypt Do not encrypt the generated password(s) (the default). --noverbose Display no labels on output (the default). --randomseed=N Use random number seed N, between 0 and 2^32 inclusive. A zero value results in a real-random seed. This option generates predictable passwords, and should normally be avoided. --rerandom=N Set the random seed value every N values used. Specify zero to use a single seed value (the default). Specify one to get true-random passwords, but plan on hitting the CONTROL key a lot while it's running. ;) --repeatpass=N Use each password N times (4096 maximum, --crypt or --crypt-md5 must be set and --cryptsalt may not be set). --string=STRING Use the characters in STRING to generate random passwords.
mkpasswd 命令语法
在大多数发行版中,mkpasswd 是默认安装的。语法如下:
mkpasswd
mkpasswd [options]
mkpasswd [options] [user]
mkpasswd 命令示例
只需输入 mkpasswd 命令并按下[enter]键即可。例如:
$ mkpasswd
示例输出:
Password: type-Your-Password
/o88/D4SvLix2
要设置密码长度,请输入:
$ mkpasswd -l 12
示例输出:
gk3Jh5s]Riaa
要将密码存储在 shell 变量中,请输入:
上述 shell 命令将生成长度恰好为 12 个字符的新密码。您可以编写如下脚本:
$ RPASS=$(mkpasswd -l 12)
# print it using the echo/printf #
$ echo "$RPASS"
#!/bin/bash # ... do something userPassword="$(mkpasswd -l 32)" # ... do something with the $userPassword echo "$userPassword" # ...
您可以将以下选项传递给 mkpasswd 命令:
-d # (min # of digits, default = 2) -c # (min # of lowercase chars, default = 2) -C # (min # of uppercase chars, default = 2) -s # (min # of special chars, default = 1)
以下示例创建一个 24 个字符的密码,其中至少包含 3 个数字、3 个字符和 5 个大写字符:
$ mkpasswd -l 22 -d 3 -C 5 -s 3
示例输出:
h@fq'h0;w9AkTN6GovszUg
在 Linux 上安装pwgen 命令
您可以安装并使用 pwgen 命令自动生成密码。要在 RHEL/CentOS/Fedora Linux 上安装 pwgen,请键入yum 命令/dnf 命令,如下所示:
$ sudo yum -y install pwgen
示例输出:
Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin, security This system is receiving updates from RHN Classic or RHN Satellite. 0 packages excluded due to repository protections Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package pwgen.x86_64 0:2.06-5.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================= Package Arch Version Repository Size ============================================================================================================================================================================= Installing: pwgen x86_64 2.06-5.el6 epel 19 k Transaction Summary ============================================================================================================================================================================= Install 1 Package(s) Total download size: 19 k Installed size: 30 k Is this ok [y/N]: y Downloading Packages: pwgen-2.06-5.el6.x86_64.rpm | 19 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : pwgen-2.06-5.el6.x86_64 1/1 Verifying : pwgen-2.06-5.el6.x86_64 1/1 Installed: pwgen.x86_64 0:2.06-5.el6 Complete!
要在 Debian/Ubuntu Linux 上安装 pwgen,请输入apt-get 命令,如下所示:
$ sudo apt-get install pwgen
示例输出:
[sudo] password for example: Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: nvidia-settings-experimental-310 Use 'apt-get autoremove' to remove them. The following NEW packages will be installed: pwgen 0 upgraded, 1 newly installed, 0 to remove and 15 not upgraded. Need to get 21.7 kB of archives. After this operation, 86.0 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu/ precise/main pwgen amd64 2.06-1ubuntu2 [21.7 kB] Fetched 21.7 kB in 1s (14.8 kB/s) Selecting previously unselected package pwgen. (Reading database ... 539106 files and directories currently installed.) Unpacking pwgen (from .../pwgen_2.06-1ubuntu2_amd64.deb) ... Processing triggers for man-db ... Setting up pwgen (2.06-1ubuntu2) ...
pwgen 命令语法和示例
语法是:
输入以下命令按列打印密码:
pwgen
pwgen -N 1
pwgen [options]
$ pwgen
Ayoh1uth chu9NahS id4iuYo0 ThaM7ic3 ohcagh7Y dooyoNg5 eeKee7ai eik4eeGi gi7uSopa coo1aZiS eeb0Ni2k Ji1aihai faij7Ahy La7aeN4E ail1Phah IZ5shing Shayie6o DeiB5che Ohgheic5 iChanga0 airahs5B cuoth5Xe Chagi1de weiV1ca0 Eevei6ph sieGai5o aith4Ooy Zucu1eeb iGh1ahVu pe2Jiede du0Eiviv EeW5Aiqu Yu9Ra3la Ahg6ziaX Fei6aigu Ud1aef3u thohf1Ie Uudiosh1 ait2yahG See7eev4 zae7eiCh eiZei4ai yae8iQua Che4If3l Opho6aid aep0uS9g aev7eNof eeXai3co xoo9Yiel zoo4Foo6 Phei1Ai4 Ohm7oqu4 Rohm3Nee neimo4Ae vai6JiuM Yohf0EiY aizui0Xa feh1Feeg Othie8Pe eigee3Zo bei3Lice Gav7gu4i CoBoo3Ni Ae9phile Cuj3ohqu cheip7Aa Wah9uph5 if6Ohhai Mie6vuk3 eeN9aiwi aeHaet7u Ahch8Ooc Aix6zei5 aichah8E soo6rooC fiLoghe0 JohgooT1 eeH3lif7 oot8Urah aM0ai3ee Thahwae1 eWiefub0 chee6Ki6 saide7Oe ozaiH1ph ahBohd8i Il7eishi Oonoo0th mooc6aiP eeFeof4g ei0Ne8ij Xah1quei aeh9rahM aRiR4air aQu0wuP2 Muadoo8A ye9choXe Weefuu2c aichei2H Aquoh4ir Ienahma9 vu9IYaiy roze2OoG eiHai4fo ohmooTh2 diuZu3bu Ien5Reem eeWee7ci xo3Nahng uJ3seque seth3Xi8 ei0xi9uY Uax0dieD aenoR3ai phie0Ait ooh3Meex Quook6oh ieru3eiH Daht4hee got3Zoov Uph8phib ne3Lahe1 zaesah3G biiQu2ga Vo4tiogh Au1Eic3l Wais4ohl Ohy9yie2 ahLekae6 tee0haiP hah1oYie uTh7aeh9 amoh3Ier peDi5joh jaeG4xah AiZoo3ah ub2Ayoh6 ces0Iuza Phoshij1 ooxe0Doo eoY0aev7 AepaeW1x ui8yuaNe eim3iNee veeZeni9 Aet5enah fei2eSai Rei3faa4 peen2vaH Aeg4oiyi Eifu5vo0 Uij2thah nuNg7ahj mequah2T Ohcai5ei ahPuos0o vu9Neong gei5Shai Aeth8ca2 Phaen3iG
生成一个密码而不是按列打印的满屏密码:
$ pwgen -N 1
示例输出:
ooQue4ce
以下示例生成一个长度为 20 个字符的密码:
$ pwgen 20 1
示例输出:
Aij2aeT2ob5yeyoonaer
pwgen 命令支持的选项:
-c or --capitalize Include at least one capital letter in the password -A or --no-capitalize Don't include capital letters in the password -n or --numerals Include at least one number in the password -0 or --no-numerals Don't include numbers in the password -y or --symbols Include at least one special symbol in the password -s or --secure Generate completely random passwords -B or --ambiguous Don't include ambiguous characters in the password -h or --help Print a help message -H or --sha1=path/to/file[#seed] Use sha1 hash of given file as a (not so) random generator -C Print the generated passwords in columns -1 Don't print the generated passwords in columns -v or --no-vowels Do not use any vowels so as to avoid accidental nasty words
总结
本快速教程向您介绍了各种 Linux 和 Unix 命令行密码生成器,供个人和专业使用。我还建议使用密码管理器以方便使用。
参见:
- Bash shell hack 在 Linux/Unix 命令行上生成随机密码
- 查看密码文件的格式 - /etc/passwd
- 另外,请参阅影子密码文件 - /etc/shadow
- Linux 或 Unix 组文件 - /etc/group
- 使用 man 命令或 help 命令查看以下手册页。例如:
$ man 1 makepasswd
$ man 5 passwd
$ man 1 passwd
$ man 1 gpasswd
$ man 1 mkpasswd
$ man 1 pwgen