- Добавление пользователя в группу. Списки пользователей и групп в Linux
- Список групп, в которых состоит пользователь
- Добавление пользователя в группу
- Удаление пользователя из группы
- Список всех групп
- Список всех пользователей
- Linux Add User To Group Using Command-Line
- How to add user to group in Linux
- Linux command to add user to group
- How to become a root user
- Add a new user to secondary group using useradd
- How to add a new user to primary group using useradd
- How to add a existing user to existing group using usermod
- usermod command options summary
- A note about security
- A note about GUI tool
- Summing up
- 10 practical examples to add or remove user from group in Linux
- Difference between Primary vs Supplementary Group
- Primary group:
- Supplementary (or Secondary) Group:
- 1. Create a new user and add to existing primary group
- 2. Create a new user and add to existing supplementary group
- 3. Create a new user and add to existing primary and supplementary group
- 4. Change primary group of existing user
- 5. Add user to Group (Supplementary or Secondary) using usermod
- 6. Add user to multiple groups (Supplementary or Secondary) using usermod
- 7. Add user to Group (Supplementary or Secondary) using gpasswd
- 8. Add multiple users to same group
- 9. Remove user from Group (Supplementary or Secondary)
- 10. Remove multiple users from supplementary group
- 11. Remove user from all Groups (Supplementary or Secondary)
- Related Posts
Добавление пользователя в группу. Списки пользователей и групп в Linux
В данной заметке рассматривается как добавить пользователя в группу и как удалить из группы, вывод списка групп, в которых состоит пользователь, а также вывод всех пользователей и всех групп в системе Linux.
Список групп, в которых состоит пользователь
Для вывода списка групп, в которых состоит конкретный пользователь используется команда groups.
При выполнении команды groups без аргументов, выводится список групп текущего пользователя.
Можно указать имя пользователя, для которого нужно вывести список групп, в которых он состоит. Например, выведем группы, в которых состоит пользователь root.
Добавление пользователя в группу
Чтобы в Linux добавить существующего пользователя в группу используется команда usermod с ключами -a и -G группа . Например, добавим пользователя pingvinus в группу editorsgroup.
После выполнения данной команды пользователю pingvinus необходимо выйти и снова войти в систему. Можно воспользоваться командой su, чтобы войти от имени пользователя pingvinus в текущем сеансе.
Удаление пользователя из группы
Удалим пользователя pingvinus из группы editorsgroup.
Чтобы изменения вступили в силу, нужно выйти и войти в систему.
Список всех групп
Выведем список всех групп в текущей системе Linux.
Список всех пользователей
Выведем список всех пользователей в текущей системе Linux.
Обычно список довольно большой, так как содержит всех пользователей, включая пользователей, которые используются для запуска некоторых программ и служб.
Можно ограничить вывод только теми пользователями, для которых домашняя директория расположена в директории /home.
Источник
Linux Add User To Group Using Command-Line
- Primary user group. – It is the group that applied to you when login. Typically it is same as your login name. All of your process and files (including directories/folders) would have your primary group as the group membership. The primary group allows private group membership and security features. Your files or process cannot access by other group members or users on the Linux system.
- Secondary or supplementary user group – Users can be a member of other groups on the Linux system. It is useful for file sharing and other purposes. A sysadmin can fine-tune security too. For example, if you are a member of a secondary group called cdrom, you can mount and unmout cd-rom drive.
Tutorial details | |
---|---|
Difficulty level | Intermediate |
Root privileges | Yes |
Requirements | usermod/useradd |
Est. reading time | 5 minutes |
How to add user to group in Linux
Please note that all user accounts related information are stored in the following files:
- /etc/passwd – Contains one line for each user account.
- /etc/shadow – Contains the password information in encrypted formatfor the system’s accounts and optional account aging information.
- /etc/group – Defines the groups on the system.
- /etc/default/useradd – This file contains a value for the default group, if none is specified by the useradd command.
- /etc/login.defs – This file defines the site-specific configuration for the shadow password suite stored in /etc/shadow file.
We do not modify these files by hand. Instead, we add a user to a group in Linux using various commands.
Linux command to add user to group
Open the terminal and then type:
- Add a new user called jerry to secondary group named cartoons on Linux:
sudo useradd -G cartoons jerry - Want to add a new user called tom to primary group called cartoons? Run:
useradd -g cartoons tom - We can add a existing user named spike to existing group named cartoons in Linux:
useradd -g cartoons spike
How to become a root user
You must run all command as root user. To become a root user run:
su —
Alternatively use sudo command:
sudo -i
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Add a new user to secondary group using useradd
You need to the useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it. The syntax is as follows:
useradd -G < group-name >username
In this example, create a new user called vivek and add it to group called developers. First, make sure group developers exists using grep command:
# grep «^developers» /etc/group
Sample outputs:
If you do not see any output then you need to add group developers using the groupadd command:
# sudo groupadd developers
Verify that user vivek does not exists:
# grep «^vivek» /etc/passwd
You should not see any outputs from above command. Finally, add a new user called vivek to group developers:
# useradd -G developers vivek
Setup password for user vivek:
# passwd vivek
Ensure that user added properly to group developers:
# id vivek
Sample outputs:
Please note that capital G ( -G ) option add user to a list of supplementary groups. Each group is separated from the next by a comma, with no intervening whitespace. For example, add user jerry to groups admins, ftp, www, and developers, enter:
# useradd -G admins,ftp,www,developers jerry
How to add a new user to primary group using useradd
How to add a existing user to existing group using usermod
Add existing user tony to ftp supplementary/secondary group with the usermod command using the -a option
i.e. add the user to the supplemental group(s). Use only with -G option:
# usermod -a -G ftp tony
In this example, change tony user’s primary group to www, enter:
# usermod -g www tony
usermod command options summary
Option | Purpose |
---|---|
-a —append | Add the user to the supplementary group(s). Use only with the -G option. |
-g GROUP —gid GROUP | Use this GROUP as the default group. |
-G GRP1,GRP2 —groups GRP1,GRP2 | Add the user to GRP1,GRP2 secondary group. |
A note about security
If you add or delete user to existing group, you must change the owner of any crontab files or at jobs manually. You must make any changes involving NIS on the NIS server too.
A note about GUI tool
You will probably find the use of the GUI tool easy. KDE user can use KUser tool and the GNOME user can use users-admin:
users-admin is part of the GNOME system tools, a set of tools to easily access and manage system configuration
One can easily add users as follows from the gnome 3 settings on a Debian/Ubuntu Linux:
Fedora/RHEL/CentOS user can use system-config-users command as follows
# system-config-users
$ ssh -X -t vivek@server1.cyberciti.biz sudo system-config-users
Sample outputs:
Click on the Groups tab to add or view groups:
Summing up
You learned how to add a new or existing user to group on Linux operating systems. For more information type the following command at the shell prompt to read man pages:
$ man usermod
$ man useradd
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
10 practical examples to add or remove user from group in Linux
Table of Contents
In this article I will cover the below topics and share multiple practical examples cover these scenarios
- Different types of group in Linux
- Add user to group (single)
- Add user to multiple groups
- Change primary group of the user
- Remove user from group
So when you say Linux add user to group, which group do you mean? There are two types of group in Linux
- Primary Group
- Supplementary or Secondary Group
Difference between Primary vs Supplementary Group
Primary group:
- The group that is listed in the group membership field for a user in /etc/passwd .
For example here deepak is part of primary group with GID 1000 i.e. deepak (so primary group name is same as loginname)
- When we create user using useradd , depending upon USERGROUPS_ENAB variable in /etc/login.defs a primary group is created/assigned to user. If this variable is set to yes, a group will be created for the user, with the same name as her loginname . If the variable is set to no, useradd will set the primary group of the new user to the value specified by the GROUP variable in /etc/default/useradd , or 100 by default
- On Linux, every file and directory has a user owner and a group owner. Group ownership is set when files are created, and unless configured otherwise, it is set to the primary group of the user who creates the file.
- A user at a time can be part of only one primary group.
Supplementary (or Secondary) Group:
- A user can be part of multiple supplementary group
- A group that a user is a member of but which membership is not defined in the /etc/passwd file.
- When creating new files, the supplementary group will not automatically become the owner of those files.
1. Create a new user and add to existing primary group
- By default when we create a new user, a new primary group is created by the same name as of the user.
- But we can also use useradd to create a user and add this user to any existing group
- So this group will not become the primary group of your new user
In this example I will create a new group » admin «
Verify the group exists
Next I will create a new user » user1 » and add this user to » admin » group using useradd -g
Verify the primary group of user1
2. Create a new user and add to existing supplementary group
We want to create a new user and add him/her to supplementary group (please NOTE, we will add the user to supplementary group and not the primary group here)
I have below list of groups on my Linux server
I will create a new user » user2 » and add this user to all these supplementary groups using useradd -G , , ..
Verify the supplementary groups.
Please NOTE that since we did not specified primary group using -g , a new group user2 is created and assigned as primary group to the user
3. Create a new user and add to existing primary and supplementary group
Now we will combine both the above arguments i.e. -g to add primary_group and -G to add supplementary_group
In this example i will create user3 with primary_group as admin and with supplementary_group of devops and qa_team
Verify the new user group details
4. Change primary group of existing user
I have a user who is currently added to his own primary group
I will change the primary group of this user to admin using usermod
Verify the new primary group for user4
5. Add user to Group (Supplementary or Secondary) using usermod
- To add user to group we can use use usermod or gpasswd command
- We can add user to supplementary groups only
- In this example I will add user4 to devops supplementary group
Syntax to add user to group: usermod -G
Verify the user details
6. Add user to multiple groups (Supplementary or Secondary) using usermod
We can also add user to multiple supplementary groups using syntax usermod -G , , ..
In this example I will add user4 to multiple supplementary groups ( devops and qa_team )
Verify the user details
7. Add user to Group (Supplementary or Secondary) using gpasswd
- Similar to usermod we can also user gpasswd to add user to group
- The syntax to add user to group is gpasswd -M
In this example I will add user4 to devops as supplementary group
Verify the user details
Alternatively you can also user gpasswd -a
In this example I will add user4 to qa_team as supplementary group
Verify the user details
8. Add multiple users to same group
We need to user gpasswd to add multiple users to same group
Currently I already have admin group which does not contain any users at the moment
The syntax to add multiple users to single group would be gpasswd -M , , ..
In this example I will add my existing users i.e. user1 , user2 and user3 to admin as supplementary group
Verify the admin group details
9. Remove user from Group (Supplementary or Secondary)
Currently my user4 is part of three different supplementary groups
gpasswd is the best tool to remove user4 from qa_team group
We can also use usermod command to remove user from group. The problem with usermod is that you must define the complete list of supplementary group which the user is part of and only remove the group which you wat to remove the user from.
For example, my user4 is part of devops , admin and qa_team . So to remove user4 from qa_team we will re-add user to group devops and admin (not to qa_team )
Verify the user details
I would recommend using gpasswd to remove user from group
10. Remove multiple users from supplementary group
There is no single command to remove multiple users from single group but we can use a small script
Currently I have admin which has three users
I will write a small script to remove all the 3 users from admin group
remove multiple users from same group
11. Remove user from all Groups (Supplementary or Secondary)
- We can use gpasswd to remove user from group
- But if a user is part of multiple groups then you need to execute gpasswd multiple times
- Or write a script to remove user from all the supplementary groups
- Alternatively we can use usermod -G «»
Currently my user4 is part of multiple supplementary groups
To remove user from all supplementary groups, use:
Verify the user details
Lastly I hope the steps from the article to add user to group, remove user from group and difference between primary group and supplementary group on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Источник