In Linux system, using the usermod command to add users to the secondary group is: 1. Execute the sudo usermod -a -G group name username command to add, where -a means append to avoid overwriting the original secondary group; 2. Use groups username or grep group name /etc/group to verify whether the operation is successful; 3. Note that the modification only takes effect after the user logs in again, and the main group modification should use the -g parameter; 4. You can also manually edit the /etc/group file to add users, but be careful to avoid system abnormalities caused by format errors.
In Linux systems, adding users to a secondary group is a common operation, especially when managing permissions and sharing resources. The method to achieve this goal is not complicated, but you need to pay attention to how the commands are used and the system environment.
Use the usermod
command to add users to secondary groups
This is the most commonly used method for most mainstream Linux distributions such as Ubuntu, CentOS, and Debian.
-
Basic command format:
sudo usermod -a -G group name username
in:
-
-a
means "append", which means appending to an existing group, not a replacement. -
-G
means specifying the secondary group (note that it is capital G). - If
-a
is not added, other secondary groups that the user currently belongs to will be cleared!
-
For example:
Want to add user
alice
to thedevelopers
group:sudo usermod -a -G developers alice
Verification is successful:
You can use the following command to view all groups to which the user belongs:
groups alice
Or view the corresponding group information in the
/etc/group
file:grep developers /etc/group
Notes on modifying user groups
Although the operation is simple, some details are prone to errors or are ignored in actual use:
Administrator permissions are required :
sudo
permissions are usually required to executeusermod
.Effective time issue : After the user's group information is changed, it will only take effect after logging in again . In other words, what you see in the current terminal session is still the old group information.
How to deal with multiple groups? : Multiple groups can be added at once, separated by commas:
sudo usermod -a -G group1,group2 username
Don't misuse the
-g
parameter : that is used to modify the primary group, not the secondary group.
Alternative: Manually edit /etc/group
If you prefer to edit the configuration file directly, you can also manually modify the /etc/group
file to add users to the group.
Open the file:
sudo nano /etc/group
Find the group name you want to modify, for example:
developers:x:1001:bob
Append the username and become:
developers:x:1001:bob,alice
Save and exit.
?? This method is suitable for people who are familiar with text editing and system structure. It is prone to system abnormalities due to spelling errors or formatting problems , so it is recommended to use
usermod
first.
Basically that's it. Just remember the key points: use usermod -a -G
to add secondary groups, don't miss -a
, remember to let the user log in again after the modification.
The above is the detailed content of How to add a user to a secondary group?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

To correctly install the local RPM file and handle dependencies, you should first use dnf to install it directly, because it can automatically obtain the required dependencies from the configured repository; if the system does not support dnf, you can use yum's localinstall command instead; if the dependency cannot be resolved, you can manually download and install all related packages; finally, you can also forcefully ignore the dependency installation, but this method is not recommended. 1. Use sudodnfinstall./package-name.rpm to automatically resolve dependencies; 2. If there is no dnf, you can use sudoyumlocalinstall./package-name.rpm; 3. Force installation and execute sudorpm-ivh--nod

AminimalinstallofCentOSisalightweightsetupthatincludesonlyessentialcomponents,makingitidealforserversorsystemsrequiringfullcontrol.Itcontainscoreutilitieslikebash,yum/dnf,networkingtools,andsecuritypackages,whileexcludingdesktopenvironments,webserver

How to set a static IP address using nmcli on CentOS8 or 9? 1. First run the nmcliconnectionshow and ipa commands to view the current network interface and its configuration; 2. Use the nmcliconnectionmodify command to modify the connection configuration, specify parameters such as ipv4.methodmanual, ipv4.addresses (such as 192.168.1.100/24), ipv4.gateway (such as 192.168.1.1), and ipv4.dns (such as 8.8.8.8). 3. Run the nmcliconnectiondown and up commands to restart the connection to make the changes take effect, or

Installing and configuring fail2ban on CentOS is not complicated, it mainly includes the following steps: 1. Install fail2ban using yum; 2. Manually enable and start the service; 3. Create a jail.local file for custom configuration; 4. Set SSH defense rules, including enabling sshd, specifying the blocking time and retry times; 5. Configure firewalld as an action actuator; 6. Regularly check the blocking IP and logs. Fail2ban detects abnormal login behavior through monitoring logs and automatically blocks suspicious IPs. Its core mechanism relies on key parameters such as bantime (banned time), findtime (statistic window time) and maxretry (maximum failure number).

KernelCare and kpatch are both tools for implementing hot patches in the Linux kernel, but the applicable scenarios are different. 1. KernelCare is a commercial service that supports CentOS, RHEL, Ubuntu and Debian, automatically applies patches without restarting, and is suitable for hosting service providers and enterprise production environments; 2. kpatch is an open source tool developed by Red Hat. It is based on the ftrace framework and requires manual construction of patch modules. It is suitable for RHEL and compatible systems, and is suitable for organizations that need to finely control the patch process or use customized kernels. When choosing, automation requirements, system distribution, whether official support is required, and the degree of control over open source tools should be considered. Neither of them can fix all vulnerabilities, some still need to be restarted, and

How to add or remove a service in FirewallD? 1. Add a service: First use firewall-cmd-get-services to view available services, temporarily add --add-service=service name, and permanently add --permanent parameter; 2. Remove service: Use --remove-service=service name to temporarily remove, add --permanent permanently remove, and after modification, all need to perform --reload reload configuration; 3. Custom service: Use --new-service to create a service and edit the XML file to define the port, and then add it according to the standard service. Pay attention to distinguish between temporary and permanent settings during operation, and reload the firewall in time.

The method of installing MariaDB or MySQL to CentOS is as follows: 1. Install MariaDB: After updating the system, use yum to install mariadb-server, start the service and run the security initialization script; 2. Install MySQL: After adding the official source, then use yum to install mysql-community-server, start the service and view the log to get the temporary password, and then run the security initialization script. MariaDB is the default recommended option, suitable for development and testing environments; MySQL is suitable for scenarios with specific enterprise needs, the community version has limited functions, and the enterprise version requires a fee. Frequently asked questions include port conflicts, permission issues, and database status checks, which can be accessed through open firewall ports.

The easiest and most reliable way to get the custom program to start, run in the background and restart automatically is to create a systemd service. 1. Create a service file: Create a new file ending with .service in the /etc/systemd/system/ directory, such as myapp.service, and fill in the configuration content containing key parameters such as Description, After, ExecStart, WorkingDirectory, User and Restart; 2. Load and enable the service: execute the sudosystemctldaemon-reload load configuration, and use sudosystemctlstartmyap
