[Linux]: Bulk user creation script

[root@myhostname ~]# cat usercreation_script.sh
#!/bin/bash
# shell script to add a users to Linux server

## checking for highest volume partition on the server to create user’s home directory
partition=`df -h | awk {‘print $4 ” ” $6 ‘} | grep “G” | sort -u | head -1 | awk {‘print $2 ‘}`

echo “Your user’s home directory will be $partition”

# Expiry date of users
expiry_date=`date +”%Y-%m-%d” -d “+2 days”`

## To set password for the users
password=’Welcome123′
pass=$(perl -e ‘print crypt($ARGV[0], “password”)’ $password)

#Number of users you want to create.
max=5

for i in `seq 1 $max`

do

## deleting the existing data
userdel -fr user$i

useradd -s /bin/bash -d $partition/user$i -e $expiry_date -c “MongoDB training purpose” -p $pass user$i
echo “user$i got created”

#Copying required files each user.
mkdir $partition/user$i/bin
cp /opt/mongodb/bin/* $partition/user$i/bin/
#cp /tmp/mongo* $partition/user$i/bin/

done
[root@myhostname ~]#
[root@myhostname ~]# sh -x usercreation_script.sh
++ df -h
++ awk ‘{print $4 ” ” $6 }’
++ grep G
++ sort -u
++ awk ‘{print $2 }’
++ head -1
+ partition=/data/6
+ echo ‘Your user’\”s home directory will be /data/6’
Your user’s home directory will be /data/6
++ date +%Y-%m-%d -d ‘+2 days’
+ expiry_date=2015-03-07
+ password=Welcome123
++ perl -e ‘print crypt($ARGV[0], “password”)’ Welcome123
+ pass=paePp1iKRcHfs
+ max=5
++ seq 1 5
+ for i in ‘`seq 1 $max`’
+ userdel -fr user1
+ useradd -s /bin/bash -d /data/6/user1 -e 2015-03-07 -c ‘MongoDB training purpose’ -p paePp1iKRcHfs user1
+ echo ‘user1 got created’
user1 got created
+ mkdir /data/6/user1/bin
+ cp /opt/mongodb/bin/bsondump /opt/mongodb/bin/dump /opt/mongodb/bin/mongo /opt/mongodb/bin/mongod /opt/mongodb/bin/mongodump /opt/mongodb/bin/mongoexport /opt/mongodb/bin/mongofiles /opt/mongodb/bin/mongoimport /opt/mongodb/bin/mongooplog /opt/mongodb/bin/mongoperf /opt/mongodb/bin/mongorestore /opt/mongodb/bin/mongos /opt/mongodb/bin/mongosniff /opt/mongodb/bin/mongostat /opt/mongodb/bin/mongotop /opt/mongodb/bin/shard_creation_script.sh /data/6/user1/bin/
cp: omitting directory `/opt/mongodb/bin/dump’
+ for i in ‘`seq 1 $max`’
+ userdel -fr user2
+ useradd -s /bin/bash -d /data/6/user2 -e 2015-03-07 -c ‘MongoDB training purpose’ -p paePp1iKRcHfs user2
+ echo ‘user2 got created’
user2 got created
+ mkdir /data/6/user2/bin
+ cp /opt/mongodb/bin/bsondump /opt/mongodb/bin/dump /opt/mongodb/bin/mongo /opt/mongodb/bin/mongod /opt/mongodb/bin/mongodump /opt/mongodb/bin/mongoexport /opt/mongodb/bin/mongofiles /opt/mongodb/bin/mongoimport /opt/mongodb/bin/mongooplog /opt/mongodb/bin/mongoperf /opt/mongodb/bin/mongorestore /opt/mongodb/bin/mongos /opt/mongodb/bin/mongosniff /opt/mongodb/bin/mongostat /opt/mongodb/bin/mongotop /opt/mongodb/bin/shard_creation_script.sh /data/6/user2/bin/
cp: omitting directory `/opt/mongodb/bin/dump’
+ for i in ‘`seq 1 $max`’
+ userdel -fr user3
+ useradd -s /bin/bash -d /data/6/user3 -e 2015-03-07 -c ‘MongoDB training purpose’ -p paePp1iKRcHfs user3
+ echo ‘user3 got created’
user3 got created
+ mkdir /data/6/user3/bin
+ cp /opt/mongodb/bin/bsondump /opt/mongodb/bin/dump /opt/mongodb/bin/mongo /opt/mongodb/bin/mongod /opt/mongodb/bin/mongodump /opt/mongodb/bin/mongoexport /opt/mongodb/bin/mongofiles /opt/mongodb/bin/mongoimport /opt/mongodb/bin/mongooplog /opt/mongodb/bin/mongoperf /opt/mongodb/bin/mongorestore /opt/mongodb/bin/mongos /opt/mongodb/bin/mongosniff /opt/mongodb/bin/mongostat /opt/mongodb/bin/mongotop /opt/mongodb/bin/shard_creation_script.sh /data/6/user3/bin/
cp: omitting directory `/opt/mongodb/bin/dump’
+ for i in ‘`seq 1 $max`’
+ userdel -fr user4
+ useradd -s /bin/bash -d /data/6/user4 -e 2015-03-07 -c ‘MongoDB training purpose’ -p paePp1iKRcHfs user4
+ echo ‘user4 got created’
user4 got created
+ mkdir /data/6/user4/bin
+ cp /opt/mongodb/bin/bsondump /opt/mongodb/bin/dump /opt/mongodb/bin/mongo /opt/mongodb/bin/mongod /opt/mongodb/bin/mongodump /opt/mongodb/bin/mongoexport /opt/mongodb/bin/mongofiles /opt/mongodb/bin/mongoimport /opt/mongodb/bin/mongooplog /opt/mongodb/bin/mongoperf /opt/mongodb/bin/mongorestore /opt/mongodb/bin/mongos /opt/mongodb/bin/mongosniff /opt/mongodb/bin/mongostat /opt/mongodb/bin/mongotop /opt/mongodb/bin/shard_creation_script.sh /data/6/user4/bin/
cp: omitting directory `/opt/mongodb/bin/dump’
+ for i in ‘`seq 1 $max`’
+ userdel -fr user5
+ useradd -s /bin/bash -d /data/6/user5 -e 2015-03-07 -c ‘MongoDB training purpose’ -p paePp1iKRcHfs user5
+ echo ‘user5 got created’
user5 got created
+ mkdir /data/6/user5/bin
+ cp /opt/mongodb/bin/bsondump /opt/mongodb/bin/dump /opt/mongodb/bin/mongo /opt/mongodb/bin/mongod /opt/mongodb/bin/mongodump /opt/mongodb/bin/mongoexport /opt/mongodb/bin/mongofiles /opt/mongodb/bin/mongoimport /opt/mongodb/bin/mongooplog /opt/mongodb/bin/mongoperf /opt/mongodb/bin/mongorestore /opt/mongodb/bin/mongos /opt/mongodb/bin/mongosniff /opt/mongodb/bin/mongostat /opt/mongodb/bin/mongotop /opt/mongodb/bin/shard_creation_script.sh /data/6/user5/bin/
cp: omitting directory `/opt/mongodb/bin/dump’
[root@myhostname ~]#
[root@myhostname ~]#
[root@myhostname ~]# su – user1
[user1@myhostname ~]$
[user1@myhostname ~]$ pwd
/data/6/user1
[user1@myhostname ~]$
[user1@myhostname ~]$ ll -lhtr
total 4.0K
drwxr-xr-x 2 root root 4.0K Mar 5 09:52 bin
[user1@myhostname ~]$
[user1@myhostname ~]$
[user1@myhostname ~]$ cd bin/
[user1@myhostname bin]$
[user1@myhostname bin]$ ll -lhtr
total 233M
-rwxr-xr-x 1 root root 18M Mar 5 09:52 bsondump
-rwxr-xr-x 1 root root 9.1M Mar 5 09:52 mongo
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongod
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongodump
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongoexport
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongofiles
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongoimport
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongooplog
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongoperf
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongorestore
-rwxr-xr-x 1 root root 14M Mar 5 09:52 mongos
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongosniff
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongostat
-rw-r–r– 1 root root 4.0K Mar 5 09:52 shard_creation_script.sh
-rwxr-xr-x 1 root root 18M Mar 5 09:52 mongotop
[user1@myhostname bin]$
[user1@myhostname bin]$
[user1@myhostname bin]$ mkdir test
mkdir: cannot create directory `test’: Permission denied
[user1@myhostname bin]$
[user1@myhostname bin]$
[user1@myhostname bin]$ su – user5
Password:
[user5@myhostname ~]$
[user5@myhostname ~]$
[user5@myhostname ~]$
[user5@myhostname ~]$ pwd
/data/6/user5
[user5@myhostname ~]$
[user5@myhostname ~]$
[user5@myhostname ~]$
[user5@myhostname ~]$ grep user /etc/passwd
user1:x:1026:1026:MongoDB training purpose:/data/6/user1:/bin/bash
user2:x:1027:1027:MongoDB training purpose:/data/6/user2:/bin/bash
user3:x:1028:1028:MongoDB training purpose:/data/6/user3:/bin/bash
user4:x:1029:1029:MongoDB training purpose:/data/6/user4:/bin/bash
user5:x:1030:1030:MongoDB training purpose:/data/6/user5:/bin/bash
[user5@myhostname ~]$

  • Ask Question