Set password in single command

Hi,
Can we set password for linux in non-interactive mode.
I didn't find suitable option for this in man page.
If we wnter "passwd" command, it will tell "Enter password" and "Re-enter password". i wanted to run this step in a script, and i don't want to use "expect" so i am looking for a single command to set password.

Hi.

Do you have chpasswd installed?.

# yum whatprovides /usr/sbin/chpasswd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirror.switch.ch
 * base: mirror.switch.ch
 * extras: mirror.switch.ch
 * updates: mirrors.supportex.net
2:shadow-utils-4.0.17-15.el5.x86_64 : Utilities for managing accounts and shadow password files.
Repo        : base
Matched from:
Filename    : /usr/sbin/chpasswd



2:shadow-utils-4.0.17-14.el5.x86_64 : Utilities for managing accounts and shadow password files.
Repo        : installed
Matched from:
Other       : Provides-match: /usr/sbin/chpasswd

1 Like

If it doesn't matter if it is set to a known password you could overwrite the second field of the shadow file with a known hash.

chpasswd also expects some user input, i wanted a way to set password in non-interactive way.
Is this posible?

I would read the man page (or read it again if you already have :))

chpasswd(8)

$ chpasswd -c < file_with_passwords

where file_with_passwords contains user-password pairs:

$ cat file_with_passwords
user1:password1
user2:password2

or

echo user1:password1 | chpasswd -c

/usr/sbin/chpasswd -c < t
/usr/sbin/chpasswd: invalid option -- c
Usage: chpasswd [options]

I think "-c" option is not there.

in the above command, t is the file having user:password

Oh yes, -c is an option on AIX chpasswd. Maybe I should have read that man page myself :smiley:

Then leave it off, or find a Linux equivalent.

In AIX it means:

What Linux system are you running?
The man pages for both SUSE and UBUNTU indicate that chpasswd update passwords in batch mode, so it is not interactive. It does not require any flag to be non-interactive.

chpasswd works fine noninteractively as root. It just reads the user:pass pairs from stdin. For example, to change xyzzy's password:

echo "xyzzy:qwertyuiop" | chpasswd

Anyone but root needs to authorize themselves with PAM somehow, usually by password. This is as it should be I think. Obviously a powerful bulk password changing tool shouldn't be available to just anyone...

This may vary on your system if your PAM settings are different.