Changing an AIX password via script?

109,431

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

Share:
109,431
Grushton94
Author by

Grushton94

Updated on July 04, 2020

Comments

  • Grushton94
    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
    TehesFR over 9 years
    I 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
    Grushton94 over 9 years
    yeah i tried that too: "echo oldpassword\nnewpasswd123\nnewpasswd123" | passwd --stdin user i get passwd: illegal option -- -
  • Grushton94
    Grushton94 over 9 years
    I su'd to root an i got the same "illegal option -- -"
  • Grushton94
    Grushton94 over 9 years
    seems like it, a lot of people's suggestions look like they should work, but don't.
  • Grushton94
    Grushton94 over 9 years
    thanks, but it's just this line echo -e "$password1\n$password1" | passwd $username that my system doesn't like
  • Ikruzzz
    Ikruzzz over 9 years
    Did you provided the new password here? echo -e "$password1\n$password1" | passwd $username
  • Mattisdada
    Mattisdada over 8 years
    For 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
    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
    Dr. Gianluigi Zane Zanettini over 7 years
    Doesn't work on Ubuntu 16.04: passwd: unrecognized option '--stdin'
  • Vercingatorix
    Vercingatorix over 7 years
    Newer versions do not support --stdin (e.g. openSUSE Leap 42.1; it worked in 12.1 but doesn't work anymore).
  • pythonmts
    pythonmts about 7 years
    This 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
    12431234123412341234123 almost 7 years
    this expect example do not work with the most locales. Maybe set it to C?
  • Eduardo Baitello
    Eduardo Baitello over 6 years
    What if the password contains a "\n"? Such as my@secret\npasword123 ?
  • Binita Bharati
    Binita Bharati over 6 years
    Great 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
    CoreyJJohnson about 6 years
    This is still a simple answer if you can get over the word sudo and add the old password on a line.
  • Taz
    Taz about 6 years
    echo username:password | chpasswd -c throws error