bash 在脚本中悄悄地更改 linux 密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15023432/
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
Change linux password in a script, quietly
提问by Joel G Mathew
As part of trying to implement a security measure in my root ssh session, I'm trying to devise a method of starting a script after n seconds of root user login, and change the user password and logout the user automatically.
作为尝试在我的 root ssh 会话中实施安全措施的一部分,我试图设计一种在 root 用户登录 n 秒后启动脚本的方法,并更改用户密码并自动注销用户。
I'm getting stuck at trying to change the password silently. I have the following code:
我一直在尝试默默地更改密码。我有以下代码:
echo -e "new\nnew" | passwd -q
This instead of changing the password "quietly" as mentioned in man pages, outputs this:
这不是像手册页中提到的那样“悄悄地”更改密码,而是输出:
~/php-pastebin-v3 #echo -e "new\nnew" | passwd -q
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
which doesnt help much.
这没有多大帮助。
I tried to pipe stdout and stderr, however I think I have misunderstood piping.
我试图通过管道标准输出和标准错误,但是我认为我误解了管道。
~/php-pastebin-v3 #echo -e "new\nnew" | passwd -q > /dev/null
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
~/php-pastebin-v3 #echo -e "new\nnew" | passwd -q /dev/null 2>&1
passwd: user '/dev/null' does not exist
What's the correct method to change the password via a script, quietly?
通过脚本悄悄更改密码的正确方法是什么?
采纳答案by Neuron
If you want to redirect both stdout and sterr:
如果要重定向 stdout 和 sterr:
echo "..." | passwd &> /dev/null
which is the equivalent of
这相当于
echo "..." | passwd > /dev/null 2>&1
which means "redirect stdout to /dev/null and then redirect (duplicate) stderr to stdout". This way you redirect both stdout and stderr to null ... but it might not be enough (it will be in this case I believe). But theoretically the program might write directly to terminal. For example this script
这意味着“将 stdout 重定向到 /dev/null,然后将(重复)stderr 重定向到 stdout”。通过这种方式,您将 stdout 和 stderr 都重定向到 null ......但这可能还不够(我相信在这种情况下)。但理论上程序可能会直接写入终端。例如这个脚本
$ cat test.sh
echo stdout
echo stderr 1 1>&2
echo stderr 2 >/dev/stderr
echo stderr 3 >/dev/fd/2
echo bad luck > /dev/tty
$ ./test.sh &> /dev/null
bad luck
To get rid even of this output you must force the program to run in pseudo terminal, for example http://empty.sourceforge.net/. But that is just a side note &> /dev/null will work fine.
要摆脱此输出,您必须强制程序在伪终端中运行,例如http://empty.sourceforge.net/。但这只是一个旁注 &> /dev/null 可以正常工作。
回答by Florian Fida
You can also do it that way:
你也可以这样做:
mkpasswd
# Password:blah
# BVR2Pnr3ro5B2
echo "user:BVR2Pnr3ro5B2" | chpasswd -e
so the password is already encrypted in the script.
所以密码已经在脚本中加密了。
回答by Davide Berra
This worked for me
这对我有用
echo "passssssword" | passwd root --stdin > /dev/null
Notice:--stdinworks for root user only
注意:--stdin仅适用于 root 用户

