In our last assignment we discuss about user and group managements. You learnt about the files which are responsible for creating user and groups. You saw what exactly happens when we add new user in these files.
To add a new user, use the useradd command. The basic syntax is
To unlock the account and create a password for the user, use the command passwd [username]. By default, the user's home directory is created and the files from /etc/skel/ are copied into it.
The two exceptions are if the –M option is used or if the home directory already exists.
We have already discussed about these two basic commands in our last article. If you haven't completed our last assignments we suggest you to review it before going with this article as it's the sequential of last assignments.
Create a user with additional command-line arguments.In this example you are going to assign home directory on other locations so first create it and same as create first desired user's secondary group.
loop for creating user
(replace users vinita nikkita niddhi sumit shweta vickey kaushal manoj jai to your users)
Loop for creating groups
Now create 3 groups named sales market productions using for loop
Now remove all the user which we created in pervious example.
For loop for deleting bulk users
Remove groups which we create in pervious example
By the end of this assignments you have learnt that
To add a new user, use the useradd command. The basic syntax is
The username is the only information required to add a new user; however, for exam prospective you should know some additional command-line arguments for useradd. The useradd command creates the account, but the account is locked.
# useradd [username]
To unlock the account and create a password for the user, use the command passwd [username]. By default, the user's home directory is created and the files from /etc/skel/ are copied into it.
The two exceptions are if the –M option is used or if the home directory already exists.
We have already discussed about these two basic commands in our last article. If you haven't completed our last assignments we suggest you to review it before going with this article as it's the sequential of last assignments.
Create a user with additional command-line arguments.In this example you are going to assign home directory on other locations so first create it and same as create first desired user's secondary group.
#mkdir /test
#groupadd example
#useradd –u 700 –d /test/user1 –g example –c “testing user” –s /bin/sh –m user1
#passwd user1
Now login form this user. And check where did this user logged in and why its shell prompt is looking different.
-c [fullname] Full name of the user (or a comment about the user).
If more than one word is needed, place quotation marks
around the value.
-d [directory] Home directory for the user. The default value is /home/[username]/.
-g [group] Default group for the user specified as a group name or group ID
number. The group name or GID must already exist. The default is
to create a private user group. If a private user group is not
created, the default is the users group.
-m Create a home directory for the user if it doesn't exist. Files from
/etc/skel/ are copied into the home directory.
-s [shell] Specify the user login shell for the user. The default shell if not
specified is /bin/bash.
-u [uid] Integer to use for the user ID. Must be unique unless -o is used.
Values less than 500 are reserved for system users.
-M Do not create a home directory for the user. By default, a home
directory is created unless this option is used or unless the
directory already exists.
By default user gets bash sell prompts. But we modified this by –s switch and given user to /bin/sh shell. Now change user shell again
$pwd
/test/user1
Verify by login again from user1
#usermod –s /bin/bash user1
How to manage bulk users
Consider a situation where you need to create more then thousand user. It will be really tedious task if you will do it by simple useradd commands. Here you have to switch to Linux shell scripts.loop for creating user
Example
# for USER in _ _ _ _ _ _ _ _ _ _ _
> do
>useradd $USER
>echo _ _ _ _ |passwd --stdin $USER
>done
(replace users vinita nikkita niddhi sumit shweta vickey kaushal manoj jai to your users)
This simple for loop will create 9 users and set their defaults passwords to friends.
# for USER in vinita nikkita niddhi sumit shewta vickey kaushal manoj jai
> do
>useradd $USER
>echo friends |passwd --stdin $USER
>done
Loop for creating groups
Now create 3 groups named sales market productions using for loop
For loop for deleting bulk users
#for GROUP in sales market productions
> do
>groupadd $GROUP
>done
Verify by cat and grep commands
Now remove all the user which we created in pervious example.
#for USER in vinita nikkita niddhi sumit shweta vickey kaushal manoj jai
>do
>userdel -r $USER
>done
For loop for deleting bulk users
Remove groups which we create in pervious example
#for GROUP in sales market productions
> do
>groupdel $GROUP
>done
- Which files are responsible for user and group managements
- How can you create a normal user
- How to create user without password
- How to create bulk users and groups
- How to delete bulk user and groups
0 Comment:
Post a Comment