Wednesday, September 7, 2016

create multiple users with single command

create multiple users using shell script, if you get an requirement to create multiple users you have to spend lot of time but here sharing script which will execute in a minute and create number of users.
#!/bin/bash
## Purpose: Creating Multiple users in a single attempt
if [ "$#" = 0 ]; then
 echo "Usage: /scripts/createusers.sh file"
else if [ -f "$1" ]; then
 for i in `cat $1`; do useradd -s /bin/bash $i; done
for y in `cat $1`; do echo -e "password\npassword" | passwd $y; done
for z in `cat $1`; do cat /etc/passwd |grep $z; done
else echo "$1 not found"
fi
fi
Create Script file for create multiple users
[root@server scripts]# mkdir /scripts/
[root@server scripts]# vim /scripts/createusers.sh
PASTE above script here
:wq! (Save & Exit)
Change the permission script file
[root@server scripts]# chmod u+x /scripts/createuser.sh
Create a file with users list and execute
[root@server scripts]# vim file
harry
champu
john
[root@server scripts]# sh /scripts/createusers.sh file
default password for created users is password

No comments:

Post a Comment