Add Bulk Users/Group

To add bulk user we have 2 options...

1. Using newusers command. First see the last line of /etc/passwd and /etc/group

[root@localhost ~]# less /etc/passwd
xxx:x:1002:1002::/home/xxx:/bin/bash
zxc:x:1003:1003::/home/zxc:/bin/bash

For exit use "esc : q" one by one.

[root@localhost ~]# less /etc/group
xxx:x:1002:
zxc:x:1003:

Now create a text file with root permissions(0600)

[root@localhost ~]# vi /root/user-bulk.txt

[root@localhost ~]# chmod 0600/root/user-bulk.txt

And enter the details as follows:

user:password:user-id:group-id:Comment:home directory:login shell

EX: xcv:xcv123:1004:1004:Guest-Faculty:/home/xcv:/bin/bash

After that create user with the following command:

[root@localhost ~]# newusers /root/user-bulk.txt

After that verify /etc/passwd, /etc/group, /etc/shadow files are updated....


2. We can make bulk users by using for loop at command prompt. But through this way all users will get the same password, but this method is much easy comparison to upper method.

[root@localhost /]# for USER in xxx yyy zzz aaa
> do
> useradd $USER
> echo qwe123 | passwd --stdin $USER
> done

Changing password for user xxx.

passwd: all authentication tokens updated successfully.

Changing password for user yyy.

passwd: all authentication tokens updated successfully.

Changing password for user zzz.

passwd: all authentication tokens updated successfully.

Changing password for user aaa.

passwd: all authentication tokens updated successfully.

[root@localhost /]# cd /home/

[root@localhost home]# ll

total 4

drwx------.  3 aaa   aaa     74 Sep  7 23:06 aaa
drwx------. 28 Singh Singh 4096 Sep  7 19:10 Singh
drwx------.  3 xxx   xxx     74 Sep  7 23:06 xxx
drwx------.  3 yyy   yyy     74 Sep  7 23:06 yyy
drwx------.  3 zzz   zzz     74 Sep  7 23:06 zzz

[root@localhost home]#

Same as FOR loop to create multiple groups..


[root@localhost /]# for GROUP in Faculty Student Hostel
> do
> groupadd $GROUP
> done

[root@localhost /]# cat /etc/group | grep Faculty
Faculty:x:1005:

[root@localhost /]#


Now delete multiple users OR group using FOR loop...

[root@localhost /]# for USER in xxx yyy zzz aaa
> do
> userdel -r $USER
> done

[root@localhost /]# cd /home/

[root@localhost home]# ll

total 4
drwx------. 28 Singh Singh 4096 Sep  7 19:10 Singh

[root@localhost home]#

[root@localhost /]# for GROUP in Faculty Student Hostel
> do
> groupdel $GROUP
> done

[root@localhost /]# cat /etc/group | grep Faculty

[root@localhost /]#


To change bulk user passwords you have 2 options:

1. chpasswd

2. echo


1. Use chpasswd as following:

#chpasswd
user_name:password
user_name:password

ctrl+D (to save and exit)


2. Use echo in the different following ways:

# echo "password" | passwd --stdin username
or
# echo  -e "password\npassword" | passwd username
or
# echo -e "old_password\nnew_password\nnew_password" | passwd username


(if you are changing your own password then do not use username)

You can put the above echo command in a script and execute it.





I hope it helps you.

No comments:

Post a Comment