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?
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?
Open a terminal and type: vim /tmp/name
to create a file and the names of the users: eg:
vim /tmp/name
mika
mery
etc
Create User with Home Dir and default shell:
for i in `cat /tmp/name`; do useradd -m -d /home/$i -s /bin/bash $i; done
Create password for each user:
for i in `cat /tmp/name`; do passwd $i; done
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
Look at the answer here,which says unless you have a whole lot of users to add the best way is to use adduser
(for adding users) and usermod
(for adding a user to a group) which takes care of a lot of things. If you still wish for some pre-made script, there is one here and another one here, but I do not know if they really work for you
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