Change linux password in script, calmly

As part of an attempt to implement a security measure in my root ssh session, I am trying to develop a method to run the script after n seconds of root login, as well as change the user password and log out automatically.

I am trying to change the password silently. I have the following code:

echo -e "new\nnew" | passwd -q 

This, instead of changing the password silently, as mentioned in the man pages, displays this:

 ~/php-pastebin-v3 #echo -e "new\nnew" | passwd -q Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully 

which does not help.

I tried to connect stdout and stderr, however, I think I misunderstood the pipeline.

 ~/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 is the correct way to change the password through a script, calmly?

+4
source share
3 answers

If you want to redirect both stdout and sterr:

 echo "..." | passwd &> /dev/null 

which is equivalent

 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 that may not be enough (in this case, I believe). But theoretically, a program can write directly to the terminal. For example, this is a script

 $ 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 of even this conclusion, you must make the program work in a pseudo-terminal, for example http://empty.sourceforge.net/ . But this is just a side note &> / dev / null will work fine.

+3
source

You can also do it like this:

 mkpasswd # Password:blah # BVR2Pnr3ro5B2 echo "user:BVR2Pnr3ro5B2" | chpasswd -e 

therefore the password is already encrypted in the script.

+3
source

It worked for me

 echo "passssssword" | passwd root --stdin > /dev/null 

Note: --stdin works only for root user

0
source

All Articles