- How to Add or Remove a User from a Group in Linux
- Check a User Group in Linux
- Add a User to a Group in Linux
- Remove a User from a Group in Linux
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- 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
- Users and groups
- Contents
- Overview
- Permissions and ownership
- Shadow
- File list
- User management
- Example adding a user
- Example adding a system user
- Change a user’s login name or home directory
- Other examples of user management
- User database
- Automatic integrity checks
- Group management
- Group list
- User groups
- System groups
- Pre-systemd groups
- Unused groups
- Other tools related to these databases
How to Add or Remove a User from a Group in Linux
Linux is by default a multi-user system (meaning many users can connect to it simultaneously and work), thus user management is one of the fundamental tasks of a system administrator. User management 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 check a user group, just run the following groups command and provide the username (tecmint in this example) as an argument.
To check your own groups, just run the groups command without any argument.
Check a User Group in Linux
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:
Add User to Group in Linux
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.
Remove User from Group in Linux
Additionally, on Ubuntu and it’s derivative, 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).
For more information, see the man pages for each of the different commands we have used in this article.
You will also find the following user management guides very useful:
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
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!!
Источник
Users and groups
Users and groups are used on GNU/Linux for access control—that is, to control access to the system’s files, directories, and peripherals. Linux offers relatively simple/coarse access control mechanisms by default. For more advanced options, see ACL, Capabilities and PAM#Configuration How-Tos.
Contents
Overview
A user is anyone who uses a computer. In this case, we are describing the names which represent those users. It may be Mary or Bill, and they may use the names Dragonlady or Pirate in place of their real name. All that matters is that the computer has a name for each account it creates, and it is this name by which a person gains access to use the computer. Some system services also run using restricted or privileged user accounts.
Managing users is done for the purpose of security by limiting access in certain specific ways. The superuser (root) has complete access to the operating system and its configuration; it is intended for administrative use only. Unprivileged users can use the su and sudo programs for controlled privilege elevation.
Any individual may have more than one account as long as they use a different name for each account they create. Further, there are some reserved names which may not be used such as «root».
Users may be grouped together into a «group», and users may be added to an existing group to utilize the privileged access it grants.
Permissions and ownership
The UNIX operating system crystallizes a couple of unifying ideas and concepts that shaped its design, user interface, culture and evolution. One of the most important of these is probably the mantra: «everything is a file,» widely regarded as one of the defining points of UNIX. This key design principle consists of providing a unified paradigm for accessing a wide range of input/output resources: documents, directories, hard-drives, CD-ROMs, modems, keyboards, printers, monitors, terminals and even some inter-process and network communications. The trick is to provide a common abstraction for all of these resources, each of which the UNIX fathers called a «file.» Since every «file» is exposed through the same API, you can use the same set of basic commands to read/write to a disk, keyboard, document or network device.
A fundamental and very powerful, consistent abstraction provided in UNIX and compatible operating systems is the file abstraction. Many OS services and device interfaces are implemented to provide a file or file system metaphor to applications. This enables new uses for, and greatly increases the power of, existing applications — simple tools designed with specific uses in mind can, with UNIX file abstractions, be used in novel ways. A simple tool, such as cat, designed to read one or more files and output the contents to standard output, can be used to read from I/O devices through special device files, typically found under the /dev directory. On many systems, audio recording and playback can be done simply with the commands, » cat /dev/audio > myfile » and » cat myfile > /dev/audio ,» respectively.
Every file on a GNU/Linux system is owned by a user and a group. In addition, there are three types of access permissions: read, write, and execute. Different access permissions can be applied to a file’s owning user, owning group, and others (those without ownership). One can determine a file’s owners and permissions by viewing the long listing format of the ls command:
The first column displays the file’s permissions (for example, the file initramfs-linux.img has permissions -rw-r—r— ). The third and fourth columns display the file’s owning user and group, respectively. In this example, all files are owned by the root user and the root group.
In this example, the sf_Shared directory is owned by the root user and the vboxsf group. It is also possible to determine a file’s owners and permissions using the stat command:
Access permissions are displayed in three groups of characters, representing the permissions of the owning user, owning group, and others, respectively. For example, the characters -rw-r—r— indicate that the file’s owner has read and write permission, but not execute ( rw- ), whilst users belonging to the owning group and other users have only read permission ( r— and r— ). Meanwhile, the characters drwxrwx— indicate that the file’s owner and users belonging to the owning group all have read, write, and execute permissions ( rwx and rwx ), whilst other users are denied access ( — ). The first character represents the file’s type.
List files owned by a user or group with the find utility:
A file’s owning user and group can be changed with the chown (change owner) command. A file’s access permissions can be changed with the chmod (change mode) command.
Shadow
The user, group and password management tools on Arch Linux come from the shadow package, which is a dependency of the base meta package.
File list
File | Purpose |
---|---|
/etc/shadow | Secure user account information |
/etc/passwd | User account information |
/etc/gshadow | Contains the shadowed information for group accounts |
/etc/group | Defines the groups to which users belong |
User management
To list users currently logged on the system, the who command can be used. To list all existing user accounts including their properties stored in the user database, run passwd -Sa as root. See passwd(1) for the description of the output format.
To add a new user, use the useradd command:
-m / —create-home the user’s home directory is created as /home/username . The directory is populated by the files in the skeleton directory. The created files are owned by the new user. -G / —groups a comma separated list of supplementary groups which the user is also a member of. The default is for the user to belong only to the initial group. -s / —shell a path to the user’s login shell. Ensure the chosen shell is installed if choosing something other than Bash.
If an initial login group is specified by name or number, it must refer to an already existing group. If not specified, the behaviour of useradd will depend on the USERGROUPS_ENAB variable contained in /etc/login.defs . The default behaviour ( USERGROUPS_ENAB yes ) is to create a group with the same name as the username.
When the login shell is intended to be non-functional, for example when the user account is created for a specific service, /usr/bin/nologin may be specified in place of a regular shell to politely refuse a login (see nologin(8) ).
See useradd(8) for other supported options.
Example adding a user
To add a new user named archie , creating its home directory and otherwise using all the defaults in terms of groups, folder names, shell used and various other parameters:
Although it is not required to protect the newly created user archie with a password, it is highly recommended to do so:
The above useradd command will also automatically create a group called archie and makes this the default group for the user archie . Making each user have their own group (with the group name same as the user name) is the preferred way to add users.
You could also make the default group something else using the -g option, but note that, in multi-user systems, using a single default group (e.g. users ) for every user is not recommended. The reason is that typically, the method for facilitating shared write access for specific groups of users is setting user umask value to 002 , which means that the default group will by default always have write access to any file you create. See also User Private Groups. If a user must be a member of a specific group specify that group as a supplementary group when creating the user.
In the recommended scenario, where the default group has the same name as the user name, all files are by default writeable only for the user who created them. To allow write access to a specific group, shared files/folders can be made writeable by default for everyone in this group and the owning group can be automatically fixed to the group which owns the parent directory by setting the setgid bit on this directory:
Otherwise the file creator’s default group (usually the same as the user name) is used.
If a GID change is required temporarily you can also use the newgrp command to change the user’s default GID to another GID at runtime. For example, after executing newgrp groupname files created by the user will be associated with the groupname GID, without requiring a re-login. To change back to the default GID, execute newgrp without a groupname.
Example adding a system user
System users can be used to run processes/daemons under a different user, protecting (e.g. with chown) files and/or directories and more examples of computer hardening.
With the following command a system user without shell access and without a home directory is created (optionally append the -U parameter to create a group with the same name as the user, and add the user to this group):
If the system user requires a specific user and group ID, specify them with the -u / —uid and -g / —gid options when creating the user:
Change a user’s login name or home directory
To change a user’s home directory:
The -m option also automatically creates the new directory and moves the content there.
Make sure there is no trailing / on /my/old/home .
To change a user’s login name:
Changing a username is safe and easy when done properly, just use the usermod command. If the user is associated to a group with the same name, you can rename this with the groupmod command.
Alternatively, the /etc/passwd file can be edited directly, see #User database for an introduction to its format.
Also keep in mind the following notes:
- If you are using sudo make sure you update your /etc/sudoers to reflect the new username(s) (via the visudo command as root).
- Personal crontabs need to be adjusted by renaming the user’s file in /var/spool/cron from the old to the new name, and then opening crontab -e to change any relevant paths and have it adjust the file permissions accordingly.
- Wine’s personal folders/files’ contents in
/.local/share/applications/wine/Programs and possibly more need to be manually renamed/edited.
or $HOME variables for home directories.
Other examples of user management
To enter user information for the GECOS comment (e.g. the full user name), type:
(this way chfn runs in interactive mode).
Alternatively the GECOS comment can be set more liberally with:
To mark a user’s password as expired, requiring them to create a new password the first time they log in, type:
User accounts may be deleted with the userdel command:
The -r option specifies that the user’s home directory and mail spool should also be deleted.
To change the user’s login shell:
User database
Local user information is stored in the plain-text /etc/passwd file: each of its lines represents a user account, and has seven fields delimited by colons.
- account is the user name. This field can not be blank. Standard *NIX naming rules apply.
- password is the user password.
Broken down, this means: user jack , whose password is in /etc/shadow , whose UID is 1001 and whose primary group is 1003. Jack Smith is his full name and there is a comment associated to his account; his home directory is /home/jack and he is using Bash.
The pwck command can be used to verify the integrity of the user database. It can sort the user list by GID at the same time, which can be helpful for comparison:
Automatic integrity checks
Instead of running pwck / grpck manually, the systemd timer shadow.timer , which is part of, and is enabled by, installation of the shadow package, will start shadow.service daily. shadow.service will run pwck(8) and grpck(8) to verify the integrity of both password and group files.
If discrepancies are reported, group can be edited with the vigr(8) command and users with vipw(8) . This provides an extra margin of protection in that these commands lock the databases for editing. Note that the default text editor is vi, but an alternative editor will be used if the EDITOR environment variable is set, then that editor will be used instead.
Group management
/etc/group is the file that defines the groups on the system (see group(5) for details). There is also its companion gshadow which is rarely used. Its details are at gshadow(5) .
Display group membership with the groups command:
If user is omitted, the current user’s group names are displayed.
The id command provides additional detail, such as the user’s UID and associated GIDs:
To list all groups on the system:
Create new groups with the groupadd command:
Add users to a group with the gpasswd command (see FS#58262 regarding errors):
Alternatively, add a user to additional groups with usermod (replace additional_groups with a comma-separated list):
Modify an existing group with the groupmod command, e.g. to rename the old_group group to new_group :
To delete existing groups:
To remove users from a group:
The grpck command can be used to verify the integrity of the system’s group files.
Group list
This section explains the purpose of the essential groups from the filesystem package. There are many other groups, which will be created with correct GID when the relevant package is installed. See the main page for the software for details.
User groups
Non-root workstation/desktop users often need to be added to some of following groups to allow access to hardware peripherals and facilitate system administration:
Group | Affected files | Purpose |
---|---|---|
adm | Administration group, commonly used to give read access to protected logs. It has full read access to journal files. | |
ftp | /srv/ftp/ | Access to files served by FTP servers. |
games | /var/games | Access to some game software. |
http | /srv/http/ | Access to files served by HTTP servers. |
log | Access to log files in /var/log/ created by syslog-ng. | |
rfkill | /dev/rfkill | Right to control wireless devices power state (used by rfkill). |
sys | Right to administer printers in CUPS. | |
systemd-journal | /var/log/journal/* | Can be used to provide read-only access to the systemd logs, as an alternative to adm and wheel [1]. Otherwise, only user generated messages are displayed. |
uucp | /dev/ttyS3+ , /dev/tts/5+ , /dev/ttyUSB1+ , /dev/ttyACM1+ , /dev/rfcomm2+ | RS-232 serial ports and devices connected to them. |
wheel | Administration group, commonly used to give privileges to perform administrative actions. It has full read access to journal files and the right to administer printers in CUPS. Can also be used to give access to the sudo and su utilities (neither uses it by default). |
System groups
The following groups are used for system purposes, an assignment to users is only required for dedicated purposes:
Group | Affected files | Purpose |
---|---|---|
dbus | used internally by dbus | |
kmem | /dev/port , /dev/mem , /dev/kmem | |
locate | /usr/bin/locate , /var/lib/locate , /var/lib/mlocate , /var/lib/slocate | See Locate. |
lp | /dev/lp8* , /dev/parport9* | Access to parallel port devices (printers and others). |
/usr/bin/mail | ||
nobody | Unprivileged group. | |
proc | /proc/pid/ | A group authorized to learn processes information otherwise prohibited by hidepid= mount option of the proc file system. The group must be explicitly set with the gid= mount option. |
root | /* | Complete system administration and control (root, admin). |
smmsp | sendmail group. | |
tty | /dev/tty , /dev/vcc , /dev/vc , /dev/ptmx | |
utmp | /run/utmp , /var/log/btmp , /var/log/wtmp |
Pre-systemd groups
Before arch migrated to systemd, users had to be manually added to these groups in order to be able to access the corresponding devices. This way has been deprecated in favour of udev marking the devices with a uaccess tag and logind assigning the permissions to users dynamically via ACLs according to which session is currently active. Note that the session must not be broken for this to work (see General troubleshooting#Session permissions to check it).
There are some notable exceptions which require adding a user to some of these groups: for example if you want to allow users to access the device even when they are not logged in. However, note that adding users to the groups can even cause some functionality to break (for example, the audio group will break fast user switching and allows applications to block software mixing).
Group | Affected files | Purpose |
---|---|---|
audio | /dev/audio , /dev/snd/* , /dev/rtc0 | Direct access to sound hardware, for all sessions. It is still required to make ALSA and OSS work in remote sessions, see ALSA#User privileges. Also used in JACK to give users realtime processing permissions. |
disk | /dev/sd[a-zA-Z]*7* | Access to block devices not affected by other groups such as optical , floppy , and storage . |
floppy | /dev/fd8* | Access to floppy drives. |
input | /dev/input/event3* , /dev/input/mouse7* | Access to input devices. Introduced in systemd 215 [2]. |
kvm | /dev/kvm | Access to virtual machines using KVM. |
optical | /dev/sr8 , /dev/sg8 | Access to optical devices such as CD and DVD drives. |
scanner | /var/lock/sane | Access to scanner hardware. |
storage | /dev/st7*[lma]* , /dev/nst3*[lma]* | Used to gain access to removable drives such as USB hard drives, flash/jump drives, MP3 players; enables the user to mount storage devices.[3] Now solely for direct access to tapes if no custom udev rules is involved.[4][5][6][7] |
video | /dev/fb/0 , /dev/misc/agpgart | Access to video capture devices, 2D/3D hardware acceleration, framebuffer (X can be used without belonging to this group). |
Unused groups
The following groups are currently not used for any purpose:
Group | Affected files | Purpose |
---|---|---|
bin | none | Historical |
daemon | ||
lock | Used for lockfile access. Required by e.g. gnokii AUR . | |
mem | ||
network | Unused by default. Can be used e.g. for granting access to NetworkManager (see NetworkManager#Set up PolicyKit permissions). | |
power | ||
uuidd | ||
users | The primary group for users when user private groups are not used (generally not recommended), e.g. when creating users with USERGROUPS_ENAB no in /etc/login.defs or the -N / —no-user-group option of useradd. |
Other tools related to these databases
This article or section is a candidate for merging with #Shadow.
The factual accuracy of this article or section is disputed.
getent(1) can be used to read a particular record.
As warned in #User database, using specific utilities such as passwd and chfn , is a better way to change the databases. Nevertheless, there are times when editing them directly is looked after. For those times, vipw , vigr are provided. It is strongly recommended to use these tailored editors over using a general text editor as they lock the databases against concurrent editing. They also help prevent invalid entries and/or syntax errors. Note that Arch Linux prefers usage of specific tools, such as chage, for modifying the shadow database over using vipw -s and vigr -s from util-linux . See also FS#31414.
Источник