Linux 在 shell 脚本中使用 passwd 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/714915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the passwd command from within a shell script
提问by Jared
I'm writing a shell script to automatically add a new user and update their password. I don't know how to get passwd to read from the shell script instead of interactively prompting me for the new password. My code is below.
我正在编写一个 shell 脚本来自动添加一个新用户并更新他们的密码。我不知道如何让 passwd 从 shell 脚本中读取,而不是以交互方式提示我输入新密码。我的代码如下。
adduser passwd
采纳答案by 8jean
from "man 1 passwd
":
来自“ man 1 passwd
”:
--stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.
So in your case
所以在你的情况下
adduser ""
echo "" | passwd "" --stdin
[Update] a few issues were brought up in the comments:
[更新]评论中提出了一些问题:
Your passwd
command may not have a --stdin
option: use the chpasswd
utility instead, as suggested by ashawley.
您的passwd
命令可能没有--stdin
选项:改用该chpasswd
实用程序,如ashawley所建议的那样。
If you use a shell other than bash, "echo" might not be a builtin command,
and the shell will call /bin/echo
. This is insecure because the password
will show up in the process table and can be seen with tools like ps
.
如果您使用 bash 以外的 shell,则“echo”可能不是内置命令,并且 shell 将调用/bin/echo
. 这是不安全的,因为密码将显示在进程表中,并且可以使用ps
.
In this case, you should use another scripting language. Here is an example in Perl:
在这种情况下,您应该使用另一种脚本语言。这是 Perl 中的一个示例:
#!/usr/bin/perl -w
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe
回答by rmeador
Have you looked at the -p
option of adduser
(which AFAIK is just another name for useradd
)? You may also want to look at the -P
option of luseradd
which takes a plaintext password, but I don't know if luseradd
is a standard command (it may be part of SE Linux or perhaps just an oddity of Fedora).
你看的-p
的选项adduser
(据我所知这是另一个名字useradd
)?您可能还想查看其中采用明文密码的-P
选项luseradd
,但我不知道是否luseradd
是标准命令(它可能是 SE Linux 的一部分,也可能只是 Fedora 的一个奇怪的东西)。
回答by Don Werve
Tested this on a CentOS VMWare image that I keep around for this sort of thing. Note that you probably want to avoid putting passwords as command-line arguments, because anybody on the entire machine can read them out of 'ps -ef'.
在我为此类事情保留的 CentOS VMWare 映像上对此进行了测试。请注意,您可能希望避免将密码作为命令行参数,因为整个机器上的任何人都可以从“ps -ef”中读取它们。
That said, this will work:
也就是说,这将起作用:
user=""
password=""
adduser $user
echo $password | passwd --stdin $user
回答by lhunath
Read the wise words from:
阅读智慧的话:
I quote:
我引用:
Nothing you can do in bash can possibly work. passwd(1) does not read from standard input. This is intentional. It is for your protection. Passwords were never intended to be put into programs, or generated by programs. They were intended to be entered only by the fingers of an actual human being, with a functional brain, and never, ever written down anywhere.
Nonetheless, we get hordes of users asking how they can circumvent 35 years of Unix security.
您在 bash 中可以做的任何事情都不可能起作用。passwd(1) 不从标准输入读取。这是故意的。这是为了你的保护。密码从不打算放入程序或由程序生成。它们旨在仅由具有功能性大脑的真实人类的手指输入,并且永远不会写在任何地方。
尽管如此,我们还是有成群结队的用户询问他们如何绕过 35 年的 Unix 安全。
It goes on to explain how you can set your shadow(5)
password properly, and shows you the GNU-I-only-care-about-security-if-it-doesn't-make-me-think-too-much-way of abusing passwd(1)
.
它继续解释如何shadow(5)
正确设置密码,并向您展示 GNU -I-only-care-about-security-if-it-doesn't-make-me-think-too-more-滥用方式passwd(1)
.
Lastly, if you ARE going to use the silly GNU passwd(1) extension --stdin
, do notpass the password putting it on the command line.
最后,如果您打算使用愚蠢的 GNU passwd(1) 扩展名--stdin
,请不要将密码放在命令行上。
echo $mypassword | passwd --stdin # Eternal Sin.
echo "$mypassword" | passwd --stdin # Eternal Sin, but at least you remembered to quote your PE.
passwd --stdin <<< "$mypassword" # A little less insecure, still pretty insecure, though.
passwd --stdin < "passwordfile" # With a password file that was created with a secure `umask(1)`, a little bit secure.
The last is the best you can do with GNU passwd
. Though I still wouldn't recommend it.
最后一个是你可以用 GNU 做的最好的事情passwd
。虽然我仍然不会推荐它。
Putting the password on the command line means anyone with even the remotest hint of access to the box can be monitoring ps
or such and steal the password. Even if you think your box is safe; it's something you should reallyget in the habit of avoiding at all cost (yes, even the cost of doing a bit more trouble getting the job done).
将密码放在命令行上意味着任何人甚至可以通过最远程的方式访问该框,都可以进行监控ps
或窃取密码。即使您认为您的盒子是安全的;这是你真的应该养成不惜一切代价避免的习惯(是的,即使是在完成工作时遇到更多麻烦的代价)。
回答by Jens
You can use the expect
utilityto drive all programs that read from a tty (as opposed to stdin, which is what passwd does). Expect comes with ready to run examples for all sorts of interactive problems, like passwd entry.
您可以使用该expect
实用程序来驱动所有从 tty 读取的程序(与标准输入相反,这是 passwd 的作用)。Expect 带有准备运行各种交互式问题的示例,例如 passwd 条目。
回答by Fernando Kosh
The only solution works on Ubuntu 12.04:
唯一的解决方案适用于 Ubuntu 12.04:
echo -e "new_password\nnew_password" | (passwd user)
But the second option only works when I change from:
但是第二个选项仅在我从以下位置更改时才有效:
echo "password:name" | chpasswd
To:
到:
echo "user:password" | chpasswd
See explanations in original post: Changing password via a script
请参阅原始帖子中的说明:通过脚本更改密码
回答by bsmoo
You could use chpasswd
你可以使用 chpasswd
echo $1:$2 | chpasswd
echo $1:$2 | chpasswd
回答by BlackNoxis
Nowadays, you can use this command:
现在,您可以使用以下命令:
echo "user:pass" | chpasswd
回答by user3811862
Sometimes it is useful to set a password which nobody knows. This seems to work:
有时设置一个没人知道的密码很有用。这似乎有效:
tr -dc A-Za-z0-9 < /dev/urandom | head -c44 | passwd --stdin $user
回答by Mariano Anaya
I stumbled upon the same problem and for some reason the --stdin
option was not available on the version of passwd
I was using (shipped in Ubuntu 14.04).
我偶然发现了同样的问题,由于某种原因,该--stdin
选项在passwd
我使用的版本上不可用(在 Ubuntu 14.04 中提供)。
If any of you happen to experience the same issue, you can work it around as I did, by using the chpasswd
command like this:
如果你们中的任何人碰巧遇到同样的问题,你可以像我一样使用如下chpasswd
命令解决它:
echo "<user>:<password>" | chpasswd