7

I have one user, which has one primary group & one secondary group, like:

[ testuser Welcome ~ ]$ id

uid=2000(testuser) gid=2000(testuser) groups=2000(testuser),27(sudo),2001(testgroup)

Primary group is testuser & secondary group is testgroup.

Now, I try to add one more secondary group testgroup2 to that user with below command

usermod -G testgroup2 testuser

but it removed the previous secondary group (testgroup) & added the new one. My need is that previous secondary group should not be removed & the new secondary group should be added.

I saw in ubuntu, there is one option like -a which can append the group to user without removing the other group.

Please let me know is it also possible on suse linux or not...?

Thankx !!!

4
  • He is also in secondary group sudo. Jan 22 '15 at 18:42
  • The version of usermod in opensuse 13.1 has the -a option. What version do you have? Jan 22 '15 at 18:48
  • What version of suse or opensuse are you running? Jan 23 '15 at 2:28
  • SUSE Linux Enterprise Server 11 (x86_64) VERSION = 11 PATCHLEVEL = 3
    – joy87
    Jan 23 '15 at 5:28
4

In SLES11 SP3, the usermod command (from pwdutils 3.2.15) supports a -A option that will do what you want:

-A, --add-to-group group,...
    With this option a list of  groups can be specified, which the user should
    become a member of. Each group is separated from the next one only by a comma,
    without whitespace.

In SLES12 and OpenSUSE 13.1, the usermod command (from the more widely-used shadow-utils 4.1.5.1) supports a -a option to be used in conjunction with the -G option:

-a, --append
    Add the user to the supplemental group(s). Use only with -G option.

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
    A list of supplementary groups which the user is also a member of. Each group
    is separated from the next by a comma, with no intervening whitespace. 

    If the user is currently a member of a group which is not listed, the user
    will be removed from the group. This behaviour can be changed via the -a option,
    which appends the user to the current supplementary group list.
1
  • The second option, usermod -G groupName -a userName is the right one in Arch currently Aug 7 '15 at 2:54
4

If your usermod has no way to append (not even -A), try tacking on the current set of groups:

usermod -G "$(groups testuser | sed 's/.*: //;s/ /,/g'),testgroup2" testuser
1
  • CentOS 6.6 does nothing with usermod -a -G squid testuser . It states the -a, --append option in the man page but when I ran the command I stated it returned the response no changes which is what it had done. However your command worked, before I tried putting the -a after the group name and before the username. Oct 18 '15 at 23:23
1

This command makes you specify a list of all groups, it replaces the old ones.

 -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
           A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option.

Instead use adduser:

adduser [options] user group

adduser can be used to add a user, but also to add a user to a group.


or as @mark plotnick points out, use the -a option of usermod

1
  • thank you for your reply..but -a option is not available in suse linux, is there any other option to append the group without removing the old group.
    – joy87
    Jan 22 '15 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.