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.
作为在我的根ssh会话中实现安全措施的一部分,我正在尝试设计一种方法,在根用户登录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:
这不是像man页面中提到的那样“悄悄地”更改密码,而是输出如下:
~/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.
我试着去管stdout和stderr,但是我想我误解了管道。
~/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?
用脚本修改密码的正确方法是什么?
3 个解决方案
#1
3
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.
为了消除这个输出,您必须强制程序在pseudo终端运行,例如:http://empty.sourceforge.net/。但这只是一个边注&> /dev/null将会很好地工作。
#2
3
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.
所以密码已经在脚本中加密了。
#3
0
This worked for me
这为我工作
echo "passssssword" | passwd root --stdin > /dev/null
Notice: --stdin
works for root user only
注意:-stdin只对根用户有效。
#1
3
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.
为了消除这个输出,您必须强制程序在pseudo终端运行,例如:http://empty.sourceforge.net/。但这只是一个边注&> /dev/null将会很好地工作。
#2
3
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.
所以密码已经在脚本中加密了。
#3
0
This worked for me
这为我工作
echo "passssssword" | passwd root --stdin > /dev/null
Notice: --stdin
works for root user only
注意:-stdin只对根用户有效。