# Setting root password from script



## balanga (Feb 4, 2020)

Is there a recommended way to set the root password from a shell script?

Having looked for an answer, it seems that I may need to use 'expect', although I suspect pw() may have the required options to set a password for a particular user, but looking at this gives me a headache...


```
SYNOPSIS
     pw    [-R rootdir] [-V etcdir] useradd [-n] name [-u uid] [-C    config]    [-q]
    [-c comment] [-d dir] [-e date]    [-p date] [-g group] [-G grouplist]
    [-m] [-M mode] [-k dir]    [-w method] [-s    shell] [-o] [-L    class]
    [-h fd | -H fd]    [-N] [-P] [-Y]
     pw    [-R rootdir] [-V etcdir] useradd -D [-C    config]    [-q] [-b dir]
    [-e days] [-p days] [-g    group] [-G grouplist] [-k dir] [-M mode]
    [-u min,max] [-i min,max] [-w method] [-s shell] [-y path]
     pw    [-R rootdir] [-V etcdir] userdel [-n] name|uid | -u uid    [-r] [-Y]
     pw    [-R rootdir] [-V etcdir] usermod [-n] name|uid [-u newuid] | -u    uid
    [-C config] [-q] [-c comment] [-d dir] [-e date] [-p date] [-g group]
    [-G grouplist] [-l newname] [-m] [-M mode] [-k dir] [-w    method]
    [-s shell] [-L class] [-h fd | -H fd] [-N] [-P]    [-Y]
     pw    [-R rootdir] [-V etcdir] usershow [-n] name|uid    | -u uid [-F] [-P]
    [-7] [-a]
     pw    [-R rootdir] [-V etcdir] usernext [-C config] [-q]
     pw    [-R rootdir] [-V etcdir] groupadd [-n] name [-g    gid] [-C config] [-q]
    [-M members] [-o] [-h fd | -H fd] [-N] [-P] [-Y]
     pw    [-R rootdir] [-V etcdir] groupdel [-n] name|gid    | -g gid [-Y]
     pw    [-R rootdir] [-V etcdir] groupmod [-n] name|gid    [-g newgid] | -g gid
    [-C config] [-q] [-l newname] [-M members] [-m newmembers]
    [-d oldmembers]    [-h fd | -H fd]    [-N] [-P] [-Y]
     pw    [-R rootdir] [-V etcdir] groupshow [-n]    name|gid | -g gid [-F] [-P]
    [-a]
     pw    [-R rootdir] [-V etcdir] groupnext [-C config] [-q]
     pw    [-R rootdir] [-V etcdir] lock [-n] name|uid | -u uid [-C config] [-q]
     pw    [-R rootdir] [-V etcdir] unlock    [-n] name|uid |    -u uid [-C config]
    [-q]
```


----------



## leebrown66 (Feb 4, 2020)

It's right there in the man page:

```
-h fd         This option provides a special interface by which
                   interactive scripts can set an account password using pw.
```


----------

