14

What is the difference between

adduser user_name group_name 

and

usermod -G -a user_name group_name 

At first glance they seems to do the same thing : add a user to a group.

Yaron
  • 12,828
  • 7
  • 42
  • 55
snoob dogg
  • 240
  • 1
  • 2
  • 10
  • `usermod -G -a user_name group_name` would be wrong either way. I think you'd want `usermod -G group_name -a user_name` or `usermod -a -G group_name user_name` instead. – Mismatch Apr 24 '23 at 14:21

2 Answers2

11

adduser and usermod are two different utilities which have in common the fact that both can add a user to a group.

According to man adduser

adduser is friendlier front ends to the low level tools like useradd, groupadd and usermod programs.

More info:

  • man adduser : adduser, addgroup - add a user or group to the system
  • man usermod : usermod - modify a user account
Yaron
  • 12,828
  • 7
  • 42
  • 55
  • ok so, the two commands I give do exactly the same thing ? – snoob dogg Mar 04 '18 at 15:12
  • @snoobdogg What is meant is that they are different. `usermod` cannot add a user to the system like `adduser` can. They just happen to share the ability to add a user to a group. – Terrance Mar 04 '18 at 15:14
  • ok I get it, but the two commands I give in my question do the same thing yes or no? – snoob dogg Mar 04 '18 at 15:17
  • @snoobdogg - you can add a user to a group using the two commands as you specify in your question. Those two commands differ in various other parameters. – Yaron Mar 04 '18 at 15:19
  • 1
    @snoobdogg Yes, they are the same commands. From the `adduser` man page: `If called with two non-option arguments, adduser will add an existing user to an existing group.` – Terrance Mar 04 '18 at 15:20
5

At first glance, yes.

At second glance, usermod -G -a user_name group_name is not correct.

The -G option should be followed by the group name(s).

$ sudo usermod -G -a nogroup muru
[sudo] password for muru:
usermod: group '-a' does not exist
$ sudo usermod -a -G muru nogroup
usermod: user 'nogroup' does not exist

The -a can come before -G, or after the group name(s), but not between -G and the group name(s).

As a side note, adduser itself uses gpasswd:

$ grep gpasswd $(which adduser)
    my $gpasswd = &which('gpasswd');
    &systemcall($gpasswd, '-a',$existing_user,$existing_group);
muru
  • 193,181
  • 53
  • 473
  • 722