Changing an AIX password via script?
Solution 1
You can try:
echo "USERNAME:NEWPASSWORD" | chpasswd
Solution 2
Use GNU passwd
stdin flag.
From the man
page:
--stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.
NOTE: Only for root user.
Example
$ adduser foo
$ echo "NewPass" |passwd foo --stdin
Changing password for user foo.
passwd: all authentication tokens updated successfully.
Alternatively you can use expect
, this simple code will do the trick:
#!/usr/bin/expect
spawn passwd foo
expect "password:"
send "Xcv15kl\r"
expect "Retype new password:"
send "Xcv15kl\r"
interact
Results
$ ./passwd.xp
spawn passwd foo
Changing password for user foo.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Solution 3
In addition to the other suggestions, you can also achieve this using a HEREDOC.
In your immediate case, this might look like:
$ /usr/bin/passwd root <<EOF
test
test
EOF
Solution 4
You need echo -e for the newline characters to take affect
you wrote
echo "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
you should try
echo -e "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
more than likely, you will not need the oldpassword\n portion of that command, you should just need the two new passwords. Don't forget to use single quotes around exclamation points!
echo -e "new"'!'"passwd123\nnew"'!'"passwd123" | passwd user
Solution 5
You can try :
echo -e "newpasswd123\nnnewpasswd123" | passwd user
Grushton94
Updated on July 04, 2020Comments
-
Grushton94 almost 4 years
I am trying to change a password of a user via script. I cannot use sudo as there is a feature that requires the user to change the password again if another user changes their password.
AIX is running on the system.
unfortunately, chpasswd is unavailable.
I have expected installed, but I am having trouble with that also.
here is what I thought would work
echo "oldpassword\nnewpasswd123\nnewpasswd123" | passwd user
However once run the script I am prompted with
please enter user's old password
shouldn't they all be echoed in?I am a beginner with shell scripting and this has been baffled.
-
TehesFR over 9 yearsI have the following code in a bash script and it works fine :
useradd -s /bin/bash -g www-data -d /var/www/$vhost $Name; echo -e "$sshPasswd\n$sshPasswd" | passwd $Name; id $Name
-
Grushton94 over 9 yearsyeah i tried that too:
"echo oldpassword\nnewpasswd123\nnewpasswd123" | passwd --stdin user
i get passwd: illegal option -- - -
Grushton94 over 9 yearsI su'd to root an i got the same "illegal option -- -"
-
Grushton94 over 9 yearsseems like it, a lot of people's suggestions look like they should work, but don't.
-
Grushton94 over 9 yearsthanks, but it's just this line
echo -e "$password1\n$password1" | passwd $username
that my system doesn't like -
Ikruzzz over 9 yearsDid you provided the new password here? echo -e "$password1\n$password1" | passwd $username
-
Mattisdada over 8 yearsFor it to work for me I had to change it to:
echo "USERNAME:NEWPASSWORD" | sudo chpasswd
when executing through a non sudo parent script -
Andrew Wolfe over 8 years
chpasswd
to me seems like the right answer. Worked for me setting up a Docker image. I like the following (in bash) to avoid the 'echo' noise:chpasswd <<<"$USERNAME:$PASSWORD"
-
Dr. Gianluigi Zane Zanettini over 7 yearsDoesn't work on Ubuntu 16.04:
passwd: unrecognized option '--stdin'
-
Vercingatorix over 7 yearsNewer versions do not support --stdin (e.g. openSUSE Leap 42.1; it worked in 12.1 but doesn't work anymore).
-
pythonmts about 7 yearsThis script can be run in a linux machine. You can modify certain things according to your environment like mail relay server or change the limit in the password characters etc.
-
12431234123412341234123 almost 7 yearsthis
expect
example do not work with the most locales. Maybe set it toC
? -
Eduardo Baitello over 6 yearsWhat if the password contains a "\n"? Such as
my@secret\npasword123
? -
Binita Bharati over 6 yearsGreat answer! Worked fine for me on Ubuntu. I had a similar requiremnt, but for first time setting (and not changing) a user's password. The mention of \n character along with -e option to seperate out the reply to the 2 password prompts really helped me.
-
CoreyJJohnson about 6 yearsThis is still a simple answer if you can get over the word sudo and add the old password on a line.
-
Taz about 6 years
echo username:password | chpasswd -c
throws error