Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need to add 10 users by a script in Ubuntu 12.04.

That could reduces time by adding users manually , and i can apply this script on other desktop .

Each user will has a specific user and in specific group.

Any idea?

share|improve this question
add comment

2 Answers

You could try this, I suppose.

for user in {1..200}; do
    echo "Creating user$user"
    cat <<EOF | adduser --gid 500 user$user
password
password
user$user







EOF
done
share|improve this answer
    
here all users are in the same group. –  guntbert Jan 25 at 22:21
    
@guntbert They were in the OP's answer, as well. All were in 513 in the OP's. –  minerz029 Jan 25 at 22:33
    
true, but the question stated: "Each user will has a specific user and in specific group." - which you could achieve by omitting the --gid 500 part. –  guntbert Jan 26 at 11:27
add comment

It is my solution. I created file /tmp/userlist and added all users name, after completion of script it will create users and you can find user name and given random password in file /tmp/userlist-created. I know it is not perfect solution.

root@demobox:/root : cat /tmp/userlist
xyz1
zyz2
zyz3

root@demobox:/root : cat demo.sh
#!/bin/bash

if [ -f /tmp/userlist ]
then
for i in $(cat /tmp/userlist)
do
if [ -f /usr/bin/pwgen ]
then
PASSWORD=$(pwgen -1 -s 16)
else
PASSWORD=$(cat /dev/urandom | tr -dc "passwordNSR!@#$%0-9" | fold -w 9 | head -1)
fi
useradd -s /bin/bash -d "/home/$i" -m -p "$PASSWORD" "$i"
echo "$i --------- $PASSWORD" >>/tmp/userlist-created
done
else
echo "File /tmp/userlist not found"
fi
chmod 0600 /tmp/userlist-created

root@demobox:/root : ./demo.sh

root@demobox:/root : cat /tmp/userlist-created
xyz1 --------- FivSHfdbDCRffhc7
zyz2 --------- AIjFjE0bv3FslHnp
zyz3 --------- JazcGcKYp2Y0I3Rk

root@demobox:/root : egrep "xyz1|zyz2|zyz3" /etc/passwd
xyz1:x:9016:100::/home/xyz1:/bin/bash
zyz2:x:9017:100::/home/zyz2:/bin/bash
zyz3:x:9018:100::/home/zyz3:/bin/bash
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.