Linux – How to add a user to a group

linux

I am a newbie on linux. I followed an example from manpage I ran from linux platform in regards to how to create user/group and add a user to a group but when I ran id command output didn't reflect what I expected.

This is what I did.

  1. sudo su –
  2. groupadd profilers (created new group name profilers)
  3. adduser user1 (created new user name user1)
  4. passwd user1 (set password for user1 account)
  5. useradd -G profilers user1 (add user1 to group name profilers)
  6. id user1
  7. uid=526(user1) gid=527(user1) groups=527(user1)

I thought gid and groups would be profilers but not. I am not sure what is happening. Also I tried this command (useradd -g profilers user1) but output was same…

[update]

I tried with "usermod -a -G groupName userName" command for user1 account and this is output.

uid=526(user1) gid=527(user1) groups=527(user1),526(profilers)

Another question came to me, why gid is set to user1? I thought gid=group id so it should point to profilers (groupName).

Best Answer

The useradd command creates a new user or updates default new user information. It is not used to modify existing users. To add a user to another secondary group, you will need to user a command like the usermod command:

usermod -a -G profilers user1

As for why the gid is set to user1, this is because a default user's primary group is a group with the same name as the user. Each user has a single primary group and one to many secondary groups. Adding secondary groups will allow more access to folders, but changing the primary group has further reaching implications and should only be done for specific cases that require doing so.

To change a user's primary group, you can use this usermod command:

usermod -g profilers user1
Related Question