Adding root level users non-interactively

I am trying to use pw useradd to add a user with root privileges, in a single command. So far I have done: [cmd=] pw useradd -n tom -s /bin/sh -m -g wheel -d /home/tom[/cmd] How do I assign a password now directly as a one liner?

I tried [cmd=]echo PASSWORD | pw user mod tom -H 0[/cmd] but that doesn't hash it. I tried echo-ing a salted string but $ cannot be escaped easily :-(
 
This should do the trick:

Code:
echo "somepassword" | pw user mod tom -h -

Or all in one go:
Code:
echo "somepassword" | pw useradd -n tom -s /bin/sh -m -g wheel -d /home/tom -h -
 
Didn't work :-(
No hash was found in the file.. also couldn't log in.

Code:
tom:*:1002:0::0:0:User &:/home/tom:/bin/sh
 
Yes,

You should put a 0 as the file desciptor. Something like:

Code:
echo "pass" | pw user add -n tom -h 0

the same holds for pw user mod as well. In the man page it states:

Code:
 -h fd         This option provides a special interface by which interac‐
                   tive scripts can set an account password using pw.  Because
                   the command line and environment are fundamentally insecure
                   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.  Alternatively,
                   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 confir‐
                   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 inacces‐
                   sible via password-based login.

where it explains why you got the * as password.
 
Hmm nice, worked. I had read about the -h fd and I remember somewhere this being mentioned that the password has to be the 10th argument. Couldn't get that to work ealier. I am using [cmd=]echo "pass" | pw user add -n tom -g wheel operator -h 0[/cmd]

How do I make tom wheel as well?
-g does not take 2 arguments
-g wheel -g operator gives tom only the last rights (operator).. Any inputs?
 
If I remember correctly, it must be the -G option. Search the man page for additional groups (or maybe secondary groups?).
 
You could also try this:

Code:
username='newuser' && pw_gen=$(openssl rand -base64 12) && echo "$username $pw_gen"; echo $pw_gen | pw useradd -n $username -s $(which bash) -m -d /home/$username -c 'comment if needed' -h 0

and You have pass auto generated and no history clean needed ;-)
 
Back
Top