27 May, 2013

User and Group Management

A new user or update default new user information creates with useradd command. Usermod command modifies a user account; it is helpful to add a user to an existing group. Group has two types. The first is primary group of users and another is a secondary group. All information related to user accounts is stored in /etc/passwd, /etc/shadow, and /etc/group files to store user information.
 

The following commands are used to manage users and groups for all operations:
id, useradd, usermod, userdel, groupadd, groupdel, groupmod, passwd

User info

The id command shows information for a said user. It will use like this:
[root@linuxguideco /]# id user1
uid=502(user1) gid=503(user1) groups=503(user1)

Create a user

To create a new user:
[root@linuxguideco /]# useradd -c "User for Training Purpose" user1
 

The recently created user have to assign a password with passwd command for activation of the user. Some useful useradd options include the following:
-c : to sets a comment for the user.
-s : is used to define the default login shell of the user. If default login shell is not used then the system default shell becomes the user’s default login shell.
-r : option is create a user with UID<500 (system account)
-d : option is set the home directory of the user. The default home directory will be created.
-M : the home directory is not created. This is useful when the directory already exists.

Add a new user to existing secondary group

[root@linuxguideco /]# useradd -G group2 user2

[root@linuxguideco /]# grep group2 /etc/group
group2:x:505:user2

Change password of the user

[root@linuxguideco /]# passwd user2
Changing password for user user2.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@linuxguideco /]#

Note that capital G (-G) option add user to a list of groups, only use comma in between groups without space. As shown below example:

[root@linuxguideco /]# useradd -G group3,group4,group5,group6 user3

If we don't specify username, then password will be changed of currently logged in user.

Add a user to a group

To modify user account's settings, we will use usermod command. We can check manual of usermod command with man page for available options. Add a user to a group is one of the useful command with usermod:

[root@linuxguideco /]# usermod -a -G group3 user3

Remove a user from a group

It a trickier to remove a user from a group. We can do that from command line. First of all you need to check a list of groups that your user is a member of:

[root@linuxguideco /]# id -nG user3
group3 group4 group5 group6

After that you need to put all groups with separated by comma to the usermod -G option, except the group which you want the user to be removed. So, to remove the user3 from group6, as shown below:

[root@linuxguideco /]# usermod -G group3,group4,group5 user3
[root@linuxguideco /]#

[root@linuxguideco /]# id -nG user3
group3 group4 group5

Lock and Unlock user accounts

Usermod command uses to lock and unlock user accounts. See shown below to lock out a user:

[root@linuxguideco /]# usermod -L user1

To unlock the user:

[root@linuxguideco /]# usermod -U user1

Delete a user

Userdel command is used to delete a user account. If we use the -r option then the user’s home directory and mail spool are also deleted:

[root@linuxguideco /]# userdel -r user1

Create a new group

To create a new group, as shown below:

[root@linuxguideco /]# groupadd linuxguide


The -r option can be used to create a group with GID<500 (system).


[root@linuxguideco /]# grep linuxguide /etc/grouplinuxguide:x:158:
 
Change a group’s name

Groupmod can be used to change a group name:

[root@linuxguideco /]# groupmod -n linuxtutorial linuxguide

[root@linuxguideco /]# grep linuxtutorial /etc/group
linuxtutorial:x:158:

Delete a group

Groupdel can delete a group:

[root@linuxguideco /]# groupdel linuxtutorial

User must be deleted before deleting a primary group of the user in the home directory, user's group will be same as username.

You can go to manual pages for more info of commands. We will type as shown below:

[root@linuxguideco /]# man adduser

No comments:

Post a Comment