how do I (non-interactively) change a users password in a script ?

Garrett Cooper youshi10 at
Mon Jan 16 22:17:49 PST 2006

user wrote:
> /bin/sh script.
> Need to change a users password within the script based on a file of
> user/pass I am feeding the script.
> Easy.
> Except the passwd command does not seem to be able to take a password as
> an argument - I don't think that the passwd command can run
> non-interactively.
> So how is this done ?  It looks like the adduser script does it
> non-interactively somehow ... but I don't see how.
> Thanks.
    Yes, you can do this:

     -h fd         This option provides a special interface by which 
                   tive scripts can set an account password using pw.  
                   the command line and environment are fundamentally 
                   mechanisms by which programs can accept information, pw
                   will only allow setting of account and group 
passwords via
                   a file descriptor (usually a pipe between an interactive
                   script and the program).  sh, bash, ksh and perl all pos-
                   sess mechanisms by which this can be done.  
                   pw will prompt for the user's password if -h 0 is given,
                   nominating stdin as the file descriptor on which to read
                   the password.  Note that this password will be read only
                   once and is intended for use by a script rather than for
                   interactive use.  If you wish to have new password 
                   mation along the lines of passwd(1), this must be imple-
                   mented as part of an interactive script that calls pw.

                   If a value of `-' is given as the argument fd, then the
                   password will be set to `*', rendering the account 
                   sible via password-based login.

    Read more in man 8 pw.
    I'm also sure that there are examples out there as well if you 
search on Google, etc.

More information about the freebsd-questions mailing list