[How To] Add user to the group in Linux

The Linux operating system was originally created as a multi-user system. To control access rights for each user, three flags are used: read, write and execute. But this was not enough, so Linux user groups were implemented. Using groups, you can give multiple users access to a single resource.

A group can be assigned to a file and rights can be described for group. Users by joining this group can get access to the file or files. In the following article you can read more about groups of Linux. Here we will discuss how to add a user to the group.

For each user there are two types of groups, primary (the main group for it) and the additional ones.

Primary group: Created automatically when a user is logged on to the system, in most cases, has the same name as the user name. A user can have only one primary group.
Secondary groups: Additional groups to which the user can be added in as needed, the maximum number of secondary groups for a user is 32.

As usual, it’s add a user to the group through the terminal, as this will give you more flexibility and opportunities. To change user settings, use the usermod command.

$ usermod -h
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS mentioned by the -G option without removing him/her from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
  -V, --del-subuids FIRST-LAST  remove range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

[adinserter block=”6″]

Here we will be interested in only a few options with which you can add a user to the group.
Let’s consider a few examples. For example, to add a testuser to the adm group, use this combination:

$ sudo usermod -a -G adm testuser

If you do not use the -a option, and specify only -G, the utility will overwrite all the groups that were specified earlier, which can cause serious problems. For example, you want to add a user to the disk group and erase the wheel, then you will no longer be able to use the superuser’s rights and you will have to reset the password.
Now let’s look at the user information:

$ id testuser
uid=1001(testuser) gid=1001(testuser) groups=1001(testuser),4(adm)

If you want to specify several groups, you can do this by separating them with a comma:

$ sudo usermod -a -G disks,vboxusers testuser

The main group of the user corresponds to his name, but we can change it to another, for example users:

$ sudo usermod -g users testuser

Now the main group has been changed. You can use exactly the same options to add a user to the sudo linux group while creating it with the useradd command.

Useful Articles

Source

losst.ru

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.