How can I change a user's default group in Linux?
usermod command will allow you to change a user's primary group, supplementary group or a number of other attributes. The
-g switch controls the primary group.
For your other questions...
If you specify a group,
groupname, that does not exist during the
useraddstage, you will receive an error - useradd: unknown group groupname
groupaddcommand creates new groups.
The group will remain if you remove all users contained within. You don't necessarily have to remove the empty group.
groupadd hilbert. Then move David's primary group using
usermod -g hilbert hilbert. (Please note that the first
hilbertis the group name and the second
hilbertis the username. This is important in cases, where you are moving a user to a group with a different name)
You may be complicating things a bit here, though. In many Linux distributions, a simple
useradd hilbert will create the user
hilbert and a group of the same name as the primary. I would add supplementary groups specified together using the
You need to read the
man usermod which explains what happens with the various options:
usermod -g hilder hilder
will replace your login group from 'faculty' to 'hilder', as long as the group 'hilder' exists. If it doesn't exist then you first need to create it with groupadd.
When you use the
-G option you should also use the
-a option to append new groups to the current list of supplementary groups that user 'hilder' belongs. Without the
-a option you will replace current supplementary groups with a new group set. Therefore use this cautiously.
To change a user's primary group in Linux:
usermod -g new_group user_name
- terminate all user_name's active sessions
To test your changes run
id and look at the value of
If the command runs without errors but the gid hasn't change you've missed the bold part of step 2.