Difference between "usermod -aG" and "usermod -G" options in linux?

In summary, the conversation discusses the differences between the "usermod -aG" and "usermod -G" options on CentOS 7, with the former adding a user to a secondary group and the latter replacing all secondary groups with a new one. An example scenario is provided to demonstrate the effects of each option.
Technology news on Phys.org
  • #2
Code:
$ id pbuk
uid=1001(pbuk) gid=1002(pbuk) groups=1002(pbuk)
$ # Adds project-a as a secondary group
$ usermod -aG project-a pbuk
$ id pbuk
uid=1001(pbuk) gid=1002(pbuk) groups=1002(pbuk),1003(project-a)
$ # Adds project-b as a secondary group
$ usermod -aG project-b pbuk
$ id pbuk
uid=1001(pbuk) gid=1002(pbuk) groups=1002(pbuk),1003(project-a),1004(project-b)
$ # Deletes all secondary groups then adds project-c as a secondary group
$ # (this is not often what you want to do)
$ usermod -G project-c pbuk
$ id pbuk
uid=1001(pbuk) gid=1002(pbuk) groups=1002(pbuk),1005(project-c)
 
  • #3
thanks for the example.
 

FAQ: Difference between "usermod -aG" and "usermod -G" options in linux?

What is the difference between "usermod -aG" and "usermod -G" options in Linux?

When using the "usermod -aG" option in Linux, it appends the user to the specified group without removing the user from any other groups. On the other hand, using the "usermod -G" option replaces the user's current group memberships with the specified group only.

Can "usermod -aG" and "usermod -G" options be used interchangeably?

No, "usermod -aG" and "usermod -G" options cannot be used interchangeably. The former appends the user to a group without removing them from other groups, while the latter replaces all current group memberships with the specified group.

Which option is recommended for adding a user to multiple groups in Linux?

If you need to add a user to multiple groups in Linux, it is recommended to use the "usermod -aG" option. This way, you can add the user to multiple groups without affecting their current group memberships.

What happens if a user is already a member of the group specified with "usermod -aG"?

If a user is already a member of the group specified with the "usermod -aG" option, the command will simply ignore the group addition request and the user's group memberships will remain unchanged.

Is it possible to remove a user from a specific group using "usermod -aG" or "usermod -G"?

No, neither the "usermod -aG" nor the "usermod -G" option is designed to remove a user from a specific group in Linux. To remove a user from a group, you would need to use a different command or manually edit the group membership settings.

Similar threads

Back
Top