Users and Groups Administration in Linux

The user information is stored in the system /etc/passwd and /etc/shadow files, and that additionally, group membership information is stored in the /etc/group file. The update-passwd tool keeps the entries in these master files in sync on all Debian systems.While it is possible to edit these files by hand, it is not recommended.There exist several command line tools, which can be used to manage these files instead.

Now we are going to see the complete users and groups administration commands with examples and man pages.

Users Administration in Linux

Add New User in Linux

useradd – Create a new user or update default new user information

Syntax

useradd [-c comment] [-d home_dir] [-e expire_date] [-f inactive_days] [-g initial_group] [-G group[,…]] [-m [-k skeleton_dir]] [-o] [-p passwd] [-s shell] [-u uid] login

useradd -D [-g default_group] [-b default_home] [-e default_expire_date]
[-f default_inactive] [-s default_shell]

If you want to know more available options you need to check the useradd man page

Examples

Adding New User

First you need to create three (test1,test2,admin1) groups for our examples using groupadd

Options

-d home directory

-s starting program (shell)

-p password

-g (primary group assigned to the users)

-G (Other groups the user belongs to)

-m (Create the user’s home directory )

To add a new user with

a primary group of test1

a second group test2

starting shell /bin/bash

password of xxxx

home directory of admin

create home directory

a login name of admin

#useradd -g test1 -G test2 -s /bin/bash -p xxxx -d/home/admin -m admin

This will create a new user admin.

One additional switch worth mentioning is “-D”, which controls the defaults for useradd.

Specifying the “-D” switch on its own will simply display the default settings, while specifying -D in conjunction with other switches will change the defaults to those values.

# useradd -D

GROUP=100
INACTIVE=-1HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel

# useradd -D -s /bin/sh

# useradd -D

GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel

As you can see, this changes the default shell for created users from “bash” to “sh”.

adduser – User Friendly Frontend for useradd command

Syntax

adduser [options] user group

If you want to know available option refer add user man page

Example

#adduser admin

Adding user `admin’ …
Adding new group `admin’ (1001) …
Adding new user `admin’ (1001) with group `admin’ …
Creating home directory `/home/admin’ …
Copying files from `/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for admin
Enter the new value, or press ENTER for the default
Full Name []: Admin
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [y/N] y

You’ll notice that, by default, the adduser command creates a group with the same name as the username, and makes this group the primary group for that user. This is called a user private group (UPG)

Modify User in Linux

usermod – Modify a user account

Syntax

usermod [-c comment] [-d home_dir [-m]] [-e expire_date] [-f inactive_days]
[-g initial_group] [-G group [,…]] [-l login_name] [-p passwd] [-s shell] [-u uid [-o]] [-L|-U] login

If you want to know available option check usermod man page

Example

Options:

-d home directory

-s starting program (shell)

-p password

-g (primary group assigned to the users)

-G (Other groups the user belongs to)

To add the group ‘others’ to the user admin

#usermod -G others admin

Delete User in Linux

userdel – Delete a user account and related files

Syntax

userdel [-r] login

If you want to know available options check userdel man page

Example

Options

-r (remove home directory)

To remove the user ‘admin’ and his home directory

#userdel -r admin

deluser – remove a user from the system

Syntax

deluser [options] user group

If you want more options check deluser man page

Example

By default, deluser will remove the user without removing the home directory, the mail spool or any other files on the system owned by the user. Removing the home directory and mail spool can be achieved using the –remove-home option. If the –home option is given, deluser will only remove the user if the directory given to the –home option matches the user’s real home directory.

#deluser –remove-home admin

Groups Administration in Linux

Add New Group in Linux

groupadd – Create a new group

Syntax

groupadd [-g gid [-o]] group

For more options check groupadd man page

Example

#groupadd test1

This will create a test1 group

addgroup – add a group to the system

Syntax

addgroup [options] [–gid ID] group

If you want to know available options check addgroup man page

#addgroup

Enter a groupname to add: admin1
Adding group `admin1′ (1001)…
Done.

Modify Group in Linux

groupmod – Modify a group

Syntax

groupmod [-g gid [-o]] [-n group_name ] group

For more options check groupmod man page

Example

#groupmod test1 test2

This will modify group name test1 to test2

Delete group in Linux

groupdel – Delete a group

Syntax

groupdel groupname

For more options check groupdel man page

Example

#groupdel test2

this will delete the test2 group

delgroup – remove a group from the system

Syntax

delgroup [options] [–only-if-empty] group

For more details about options check delgroup man page

Example

#delgroup –only-if-empty test2

Removing group `test2’…
done.

groups Command

print the groups a user is in

Syntax

groups [username]

This simple command displays what groups a user is a member of. It takes the username of user as a parameter. If no username is given, it defaults to the current user.

# groups

root

# groups admin

test1 : test2

Sponsored Link

13 thoughts on “Users and Groups Administration in Linux

  1. so i guess i would like to make use of FUSE so i can “mount” Gmailfs as a user.can you tell me:do i need to place my user in the fuse group or do i need to place fuse in my usergroup? and wile you’re at it how do i do it….
    it would be nice to understand this better.
    thanks

  2. how to delete group in Linux those who are not having primary group users presented in that group.
    I have added a users like this
    useradd -g hr user1
    useradd -g hr user2

    now I want to delete hr group please answer my query?
    please any body help me. my email id:[email protected]

  3. What is the real meaning of Groups ?
    Most of the default group names are named after programs.
    It seems that it is not like a group of people but some sort of category related to programs.
    Please make this more clear.

  4. You have to add yourself to the fuse group… sorry for late reply 😉
    only commenting so others will learn from it.

  5. How to add a user to a group:

    useradd -G {group-name} username

    To see if it worked:
    groups {username}

  6. A group is a group of users. Often some programs require special privileges In this case, groups are used to reserve the full access to this program for only those who are in this special group.

    A common example is Fuse. Fuse let’s you mount filesystems, which traditionally is a right reserved to administrators. By adding a user to the fuse group, this user gets elevated privileges – the privilege to use Fuse.

    The groups you create manually will probably be more related to your organizational structure. You could add groups such as students or accounting, and use those for restricting access to various files.

  7. Could be important to know that:
    useradd -G {group1},{group2} {username} will set the {username}’s group membership to just group1 and group2.
    To add an extra group membership, use
    useradd -a -G {group3} {username}
    After these are completed, do
    groups {username}
    to see group membership, which should be {group1}, {group2} and {group3), along with the user’s primary group membership.

Leave a comment

Your email address will not be published. Required fields are marked *