Method #1 - using passwd
You can do something like this via a script:
echo -n "$passwd" | passwd "$uname" --stdin
Where the password you want to set is $passwd
and the user you want to set it for is $uname
.
Method #2 - using useradd
You could also provide it to useradd
directly:
useradd -n -M -s $shell -g $group -d "/home/$homedir" "$uname" -p "$passwd"
NOTE: Assuming you're on a Red Hat based distro such as CentOS or RHEL in method #2, since you're example shows yum
commands. The -n
switch, for example, is in older versions of useradd
:
-n A group having the same name as the user being added to the system
will be created by default. This option will turn off this
Red Hat Linux specific behavior. When this option is used, users by
default will be placed in whatever group is specified in
/etc/default/useradd. If no default group is defined, group 1 will be
used.
newer versions of useradd
now have the option this way on Red Hat and non Red Hat distros:
-N, --no-user-group
Do not create a group with the same name as the user, but add the
user to the group specified by the -g option or by the GROUP
variable in /etc/default/useradd.
So you could use this command for other distros:
useradd -N -M -s $shell -g $group -d "/home/$homedir" "$uname" -p "$passwd"
useradd
available on Fedora or CentOS or whatever your RPM-based distribution is? – terdon♦ Nov 19 '14 at 16:56