Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing an AIX password via script?

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.

like image 585
Grushton94 Avatar asked Jan 08 '15 10:01

Grushton94


People also ask

Which command can be used to change the user password?

The passwd command sets and changes passwords for users. Use this command to change your own password or another user's password. You can also use the passwd command to change the full name (gecos) associated with your login name and the shell you use as an interface to the operating system.

Which command is used to change password of Unix system?

The passwd command changes passwords for user accounts. A normal user may only change the password for their own account, while the superuser may change the password for any account.

How do I force a user to change password at first login in AIX?

2. Using passwd command. Another way to force user for password change is to use the command passwd with -e option. The -e option expires the current user password forcing user to set a new one on next login.

How do I change the root password in shell script?

Type the command 'passwd' and press 'Enter. ' You should then see the message: 'Changing password for user root. ' Enter the new password when prompted and re-enter it at the prompt 'Retype new password.


4 Answers

You can try:

echo "USERNAME:NEWPASSWORD" | chpasswd

like image 62
Mandar Shinde Avatar answered Oct 17 '22 16:10

Mandar Shinde


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.
like image 21
Juan Diego Godoy Robles Avatar answered Oct 17 '22 18:10

Juan Diego Godoy Robles


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
like image 7
Andrew Yasinsky Avatar answered Oct 17 '22 18:10

Andrew Yasinsky


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
like image 6
Elliot Beaumont Avatar answered Oct 17 '22 18:10

Elliot Beaumont