- Удалить пользователя из группы Linux
- Удаление пользователя из группы Linux
- Выводы
- How to Remove a User from a Group Linux
- What are groups in Linux?
- How to remove a user from a group in Linux using gpasswd:
- How to remove a user from a group in Debian or Ubuntu with deluser:
- Removing a user from a group in Linux by editing the /etc/group file:
- Conclusion:
- About the author
- David Adams
- How to Remove User From Group in Linux [Quick Tip]
- How to remove user from group
- Step 1: Find the groups of the user
- Step 2: Removing user from the group
- How to Add or Remove Linux User From Group
- Check a User Group in Linux
- Add a User to a Group in Linux
- Remove a User from a Group in Linux
Удалить пользователя из группы Linux
Группы пользователей в Linux используются для тонкой настройки доступа к файлам операционной системы. И поскольку в Linux все объекты ОС являются файлами, то с помощью групп можно настроить или ограничить доступ к любой возможности системы.
В одной из предыдущих статей мы говорили о том, как посмотреть список групп Linux, в которых состоит пользователь. В этой же заметке разберём, как удалить юзера из группы Linux, чтобы он больше не мог иметь доступа к файлам, которые находятся в этой группе.
Удаление пользователя из группы Linux
Очень много слов вступления, а дела не более чем на одну команду. Мы будем использовать gpasswd. Она имеет такой синтаксис:
gpasswd -d пользователь группа
Опция -d сообщает, что нужно выполнить удаление, далее мы указываем пользователя, с которым будем работать и в конце группу, из которой его надо удалить. Теперь откройте терминал с помощью сочетания клавиш Ctrl+Alt+T и наберите следующую команду, чтобы удалить пользователя testuser из группы wheel:
sudo gpasswd -d testuser wheel
С помощью команды usermod вы можете удалить текущего пользователя из всех дополнительных групп:
sudo usermod -G «» имя_пользователя
Только будьте осторожны с этой командой, применяя её к текущему пользователю, иначе вы удалите себя из группы sudo и больше не сможете выполнять административные действия. В Ubuntu, где нет прямого доступа к пользователю root это может создать серьёзные проблемы для новичков, смотрите статью сброс пароля в Ubuntu.
В некоторых дистрибутивах можно использовать ещё и команду deluser. Точно также указываем сначала имя пользователя, а затем группу, из которой собираемся его удалить:
sudo deluser testuser adm
Если вы случайно удалили пользователя из группы, в которой он должен быть, можно всё вернуть обратно. Смотрите статью «как добавить пользователя в группу Linux».
Выводы
В этой небольшой статье мы рассмотрели, как удалить пользователя из группы Linux. Как видите, это совсем не сложно, только надо соблюдать осторожность, чтобы не удалить ничего лишнего и не создать себе проблем. Если у вас остались вопросы, спрашивайте в комментариях! Почитать более подробно о группах можно в статье «группы пользователей Linux».
Обнаружили ошибку в тексте? Сообщите мне об этом. Выделите текст с ошибкой и нажмите Ctrl+Enter.
How to Remove a User from a Group Linux
This tutorial explains how to remove a user from a group in Linux. After reading this, you’ll know how to delete users from groups easily using both gpasswd and deluser commands.
What are groups in Linux?
In Linux, users are associated with groups defining their permissions and accesses. To understand what groups are in Linux, it is important to note that there are 3 ways to grant or restrict permissions and accesses: individually, collectively, and “by elimination”.
When a user is granted permissions individually, these permissions are associated with the user ID. To grant permissions collectively, the system administrator creates a group with certain rights, and the member users of that group acquire the group permissions. Removing a user from a group will remove all permission the user inherited from that group.
As said previously, permissions can be granted or restricted to specific users individually and collectively through groups. But the system also needs a policy for all others who are not associated users or groups. Thus when granting or removing permissions in Linux, we must specify 3 categories:
- User: Individual permissions.
- Group: Collective permissions.
- Others (also known as “World” ): Permission for undefined requesters.
In Linux, every user is associated with a group generated in the user’s account creation process.
You can check the groups a user belongs to by executing the groups command followed by the username, as shown in the screenshot below.
As you can see, the linuxhint user is a member of multiple groups, which will be explained later.
How to remove a user from a group in Linux using gpasswd:
To remove the user from one of those groups, you can invoke the gpasswd command with the –delete flag followed by the user to be removed and the group.
In the example below, the linuxhint user is removed from the group floppy.
As you can see, linuxhint isn’t part of the floppy group anymore.
How to remove a user from a group in Debian or Ubuntu with deluser:
On Debian-based Linux distributions, you can remove a user from a group using the deluser command followed by the user to be removed and the group. In the example below, the linuxhint user is removed from the group floppy.
Removing a user from a group in Linux by editing the /etc/group file:
Another way to remove a user from a group in Linux is to edit the /etc/group file. Open the /etc/group configuration file using a text editor, as shown in the example below (nano).
Find the group floppy; you can use the key combination CTRL+W to search “floppy” or the group you want a user to be removed from.
You’ll see the users belonging to the group, in my case, the linuxhint user. To remove the user from the group, remove the user from the group line.
In my case, I change the line of the floppy group from this:
Press CTRL+X to save changes and exit.
You can use the gpasswd to add users to groups too. Just replace the –delete flag with the –add a flag, as shown in the example below, in which the linuxhint user is added to the floppy group.
As you can see, the user linuxhint as added to group floppy.
You can also add users to groups using the usermod command with the -aG argument followed by the group and the user to be removed, as shown in the example below. The linuxhint user is removed from the floppy group.
Both ways explained above to add users to groups are correct.
Conclusion:
Learning how to manage a group is a basic requirement to deal with permissions. Groups is a Unix feature later adopted even by competitors like Microsoft Windows.
Users need to know how to restrict users’ access to certain groups, especially in operating systems in which initial users are granted privileges by default.
The gpasswd and /etc/group methods are convenient to keep in mind for being universal, while deluser remains a good option for users limited to Debian-based Linux distributions.
If you found this tutorial interesting, you may be interested in reading Setuid, setgid, and sticky bit explained.
I hope this tutorial explaining how to remove a user from a group in Linux was useful. Keep following Linux Hint for more Linux tips and tutorials.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
How to Remove User From Group in Linux [Quick Tip]
Learn how to remove a user from a specific group in Linux command line.
Group management and user account management is one of the regular tasks performed by a Linux sysadmin. Adding to and removing from groups is part of these tasks.
Suppose you are in a situation where you need to remove a user from a group. Take a practical example where you don’t want to give a user access to run docker containers. One way would be to remove this user from the docker group.
All the members in the docker group can use docker commands without sudo. Removing the user from the group would mean that the user would need sudo access to run the commands.
How to remove user from group
Now how do you remove a user from a group? There are two steps involved here:
- Find the groups of the user
- Add the user to all of its existing group except the ones you want to remove it from
Step 1: Find the groups of the user
This will list all the groups of the user.
[email protected]:~$ id -nG prakash prakash adm cdrom sudo dip plugdev lpadmin sambashare docker
Step 2: Removing user from the group
You can use the usermod command here with option G. With option -G, you specify which groups this user will belong to. If the user is currently a member of a group which is not listed, the user will be removed from the group.
sudo usermod -G group1,group2,group3 user_name
Do note that the group names must be separated by comma but there should be no whitespace between them.
sudo usermod -G prakash,adm,cdrom,sudo,dip,plugdev,lpadmin,sambashare prakash
Now if I display the groups, you can see that it is no longer a member of docker group.
[email protected]:~$ id -nG prakash prakash adm cdrom sudo dip plugdev lpadmin sambashare
I hope you like this quick little Linux tip on removing user from groups. Questions and suggestions are always welcome.
How to Add or Remove Linux User From Group
Linux is by default a multi-user system (meaning many users can connect to it simultaneously and work), thus Linux user management is one of the fundamental tasks of a system administrator, which includes everything from creating, updating, and deleting user accounts or user groups on a Linux system.
In this short quick article, you will learn how to add or remove a user from a group in a Linux system.
Check a User Group in Linux
To find out what group a user is in, just run the following groups command and provide the username (tecmint in this example) as an argument.
# groups tecmint tecmint : tecmint wheel
To find out the group of root user in Linux, just run the groups command without any argument.
# group root
Add a User to a Group in Linux
Before trying to add a user to a group, ensure that the user exists on the system. To add a user to a certain group, use the usermod command with the -a flag which tells the usermod to add a user to the supplementary group(s), and the -G option specifies the actual groups in the following format.
In this example, tecmint is the username and postgres is the group name:
# usermod -aG postgres tecmint # groups tecmint
Remove a User from a Group in Linux
To remove a user from a group, use the gpasswd command with the -d option as follows.
# gpasswd -d tecmint postgres # groups tecmint
Additionally, on Ubuntu and its derivatives, you can remove a user from a specific group using the deluser command as follows (where tecmint is the username and postgres is the group name).
$ sudo deluser tecmint postgres
For more information, see the man pages for each of the different commands we have used in this article.
$ man groups $ man usermod $ man gpasswd $ man deluser
You will also find the following user management guides very useful: