Linux chpasswd 命令初学者教程(5 个示例)
在此页
- Linux chpasswd 命令
- Q1。如何使用 chpasswd 命令?
- Q2。如何让 chpasswd 从文件中读取信息?
- Q3。 chpasswd 如何处理加密?
- Q4。如何启用不同的加密方法?
- Q5。如何告诉 chpasswd 加密输入?
- 结论
在 HowtoForge,我们已经讨论过 passwd 命令,它可以让你在 Linux 系统上更改你的帐户密码。但是如果你想一次更改多个用户的密码怎么办?好吧,有一个命令——chpasswd——可以让你这样做。
在本教程中,我们将使用一些易于理解的示例来讨论该工具的基础知识。但在我们这样做之前,值得一提的是,这里的所有示例都已经在 Ubuntu 18.04 LTS 机器上进行了测试。
Linux chpasswd 命令
Linux 中的 chpasswd 命令允许您以批处理模式更新密码。以下是它的语法:
chpasswd [options]
下面是工具手册页关于它的内容:
The chpasswd command reads a list of user name and password pairs from
standard input and uses this information to update a group of existing
users. Each line is of the format:
user_name:password
By default the passwords must be supplied in clear-text, and are
encrypted by chpasswd. Also the password age will be updated, if
present.
以下是一些 Q&A 风格的示例,应该可以让您对 chpasswd 的工作原理有一个很好的了解。
Q1。如何使用 chpasswd 命令?
如上面介绍部分所述,基本用法非常简单。只需运行没有任何选项的 chpasswd 命令(具有 root 权限),然后以用户名:密码格式输入新密码。
例如,我执行了命令:
$ chpasswd
并提供以下输入:
himanshu:thisismynewpassword789
test:latestpassword123
注意 1:由于 chpasswd 希望您在 stdin 上输入输入内容,因此在输入用户名和新密码后不要忘记按 Ctrl+D。
注意2:此处示例中使用的密码仅用于演示目的,请勿以任何方式使用它们。
Q2。如何让 chpasswd 从文件中读取信息?
有时,您可能希望该工具从文件(而不是标准输入)中读取输入信息。这可以通过以下方式完成:
cat [FILENAME] | chpasswd
例如:
cat newpass.txt | chpasswd
请注意,输入的格式保持不变:用户名:密码,只是现在写入文件中。
Q3. chpasswd 如何处理加密?
以下是工具手册页对此的解释:
By default, passwords are encrypted by PAM, but (even if not
recommended) you can select a different encryption method with the -e,
-m, or -c options.
Except when PAM is used to encrypt the passwords,chpasswd first updates
all the passwords in memory, and then commits all the changes to disk
if no errors occurred for any user.
When PAM is used to encrypt the passwords (and update the passwords in
the system database) then if a password cannot be updated chpasswd
continues updating the passwords of the next users, and will return an
error code on exit.
This command is intended to be used in a large system environment where
many accounts are created at a single time.
Q4.如何启用不同的加密方法?
这可以使用 -c 命令行选项来完成。默认情况下,您现在可能已经知道,PAM 用作加密方法,但使用 -c,您可以指定以下任何一种:DES、MD5、NONE 和 SHA256 或 SHA512(前提是您的 libc 支持您使用的方法选择)。
例如:
chpasswd -c DES
Q5.如何告诉 chpasswd 加密输入?
如果需要,您还可以在输入中提供加密密码。但是为此,您需要使用 -e 选项,以便 chpasswd 知道这一点。
chpasswd -e
结论
chpasswd 命令通常由系统管理员或受托在 Linux PC 或网络上进行用户管理的人员使用,尽管即使您是普通用户,了解它也没有什么坏处。在这里,我们讨论了该工具的几个主要命令行选项。要了解更多信息,请访问其手册页。