3

I want to change the password of the Unix user through a shell script, but without being prompted to enter the old password.

That means I want to provide the old and new password in the shell script itself, such that during the prompt it will read it from the script file.

3 Answers 3

5

You should look at the chpasswd command (if available in your linux flavor):

echo 'userid:newpasswd' | chpasswd

Or, you can cat a file listing userid:passwd for each account on a separate line.

That's it.

Sign up to request clarification or add additional context in comments.

Comments

3
echo -e -n "newpasswd\nnewpasswd" | passwd user

Comments

0

$> echo -e -n "oldpasswd\nnewpasswd\nnewpasswd" | passwd

Or to make things a cleaner way, write a file with your passwords

oldpassword
newpassword
newpassword

and use the following command: $> passwd < file

The pipe and redirection operators are replacing the standard input with either the content of the file redirected, either the output of the command piped.

3 Comments

Does this really work? I'm under the illusion that the passwd program reads from /dev/tty rather than standard input, so redirecting the standard input like that doesn't achieve anything. To get around that, you normally resort to the expect program, which arranges to run the program with its /dev/tty connected to a pseudo-tty or pty that expect can drive programmatically.
Changing password for userx Old password: sh: -n: not found. this is the error while running this command and goes suspended mode
It is works with -e param for echo. -e enable interpretation of backslash escapes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.