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
# useradd [username]
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.
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
-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.
Now login form this user. And check where did this user logged in and why its shell prompt is looking different.
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
#usermod –s /bin/bash user1
Verify by login again from 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
# 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)
# for USER in vinita nikkita niddhi sumit shewta vickey kaushal manoj jai > do >useradd $USER >echo friends |passwd --stdin $USER >done
This simple for loop will create 9 users and set their defaults passwords to friends.
Loop for creating groups
Now create 3 groups named sales market productions using for loop
#for GROUP in sales market productions > do >groupadd $GROUP >done
Verify by cat and grep commands
For loop for deleting bulk users
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
By the end of this assignments you have learnt that
- 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
In our next article you will learn about the files those are responsible for user profiles and how can you grant root privilege to a normal user.