adduser vs pw

Hi All,
I used to create users for my local hosting with adduser. also I need to add some directories in home dir and sql Databases. I decided to create script for automation.

Code:
#!/bin/sh
#Creates new project
#ver. 0.1 from 26.12.2009

### user ###
echo "Enter projectname to create:"
read PROJECT
if [ -z $PROJECT ]; then
exit
fi
USERPASS=`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`
SQLPASS=`< /dev/urandom tr -dc A-Za-z0-9 | head -c8`

pw groupadd $PROJECT
pw user add $PROJECT -d /home/$PROJECT -m -s /bin/sh -G $PROJECT
echo '$USERPASS' |pw usermod $PROJECT  -h 0

### directories ###
mkdir /home/$PROJECT/htdocs
mkdir /home/$PROJECT/logs
chmod -R 755 /home/$PROJECT
chown -R $PROJECT /home/$PROJECT


### MySQL ###

mysql -t <<STOP
CREATE DATABASE $PROJECT default character set utf8 collate utf8_general_ci;
CREATE USER '$PROJECT'@'localhost' Identified by '$SQLPASS';
GRANT ALL ON $PROJECT.* TO '$PROJECT'@'localhost';
\q
STOP

echo "User name: $PROJECT"
echo "User password: $USERPASS"
echo "MySQL password: $SQLPASS"

The first problem that I can not login by new user. Even when I add user to wheel group and change password with passwd command.
some info...
Code:
h01# ./addproject.sh
Enter projectname to create:
test12
User name: test12
User password: LNfIt1SW
MySQL password: cNDgEZwv
h01# cat /etc/passwd | grep test12
test12:*:1015:1015:User &:/home/test12:/bin/sh
h01# cat /etc/group | grep test12
test12:*:1015:test12
h01# cat /var/log/auth.log |grep test12
Dec 26 18:58:11 h01 sshd[46756]: error: PAM: authentication error for test12 from x.x.x.74
h01#
Help please.
 
I have found problem in
Code:
echo '$USERPASS' |pw usermod $PROJECT  -h 0

need
Code:
echo $USERPASS |pw usermod $PROJECT  -h 0
 
User passwords are not exposed in /etc/passwd anymore (as you noticed). They are actually in /etc/master.passwd which is only readable by root.

As an alternative to the mkdir commands, you could create the necessary entries in /usr/share/skel which is the source for the files for newly created user directories.

You should probably also change that from [cmd=]echo $USERPASS[/cmd] to [cmd=]echo "$USERPASS"[/cmd] which is a little safer for special characters.
 
Yes, careful with the quotes. There's a slight difference in [cmd=]echo '$USERPASS'[/cmd] and [cmd=]echo "$USERPASS"[/cmd]. On the first the $USERPASS variable doesn't get evaluated. So it will print the literal sting $USERPASS. On the second the variable does get evaluated and the echo prints the contents of the variable.
Code:
dice@williscorto:~>sh
$ 
$ TEST='something here'
$ echo '$TEST'
$TEST
$ echo "$TEST"
something here
$
 
Back
Top