Being new to Linux administration, I’m a little confused about the following commands:
useradd usermod groupadd groupmod
I’ve just finished reading the user administration book in the Linux/Unix Administrator’s handbook, but some things are still a little hazy.
Basically useradd seems straight forward enough:
useradd -c "David Hilbert" -d /home/math/hilbert -g faculty -G famous -m -s /bin/sh hilbert
I can add “David Hilbert” with username hilbert , setting his default directory, shell, and groups. And I think that -g is his primary/default group and -G are his other groups.
So these are my next questions:
- Would this command still work if the groups
facultyandfamousdid not exist? Would it just create them? - If not, what command do I use to create new groups?
- If I remove the user
hilbertand there are no other users in those groups, will they still exist? Should I remove them? - After I run the
useraddcommand above, how do I remove David from thefamousgroup, and reassign his primary group tohilbertwhich does not yet exist?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The 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 theuseraddstage, you will receive an error – useradd: unknown group groupname -
The
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.
-
Create the
hilbertgroup viagroupadd hilbert. Then move David’s primary group usingusermod -g hilbert hilbert. (Please note that the firsthilbertis the group name and the secondhilbertis 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 -G switch.
Method 2
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.
Method 3
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 gid=
If the command runs without errors but the gid hasn’t change you’ve missed the bold part of step 2.
Method 4
answer #1 is good, still you could also choose to issue the following to add a new group:
# nano /etc/group
The downside of editing /etc/group directly is that you will have to come up with an unused GID (group ID number).
The following will allow you to change the primary group of a user.
# nano /etc/passwd
You locate the line of the user, and change the GID (group number, look it up in /etc/group), keep in mind that the syntax is as follows:
<name>:*:<UID>:<GID>:<comment>:<directory>:<shell>
There is no downside I can think of to this approach (provided you know the GID), it is what I frequently use for local users, mainly because I use many different UNIX systems with differing usermod, adduser, and useradd command options.
It is a bit more manual, however does the job. Note that you can use vi iso nano to edit the files, if you know vi.
Method 5
To permanently change the primary group of the user execute the:
- usermod -g groupname username command
Or you could use newgrp for small environment and temporary situation.
Create a new user bob and a new group dodo.
useradd bob
So in RedHat/Centos we have UID=(ID_number) bob; GID=(ID_number) bob as primary group by default
groupadd dodo
Add the user bob to the dodo group:
usermod -aG dodo bob -make sure to use "a"
Next use newgrp to change ownership:
newgrp dodo
Be careful of your account to have a password and to be member of the future primary group. And the newgrp opens a new shell; as long as the user stays in that shell, the primary group won’t change. To switch the primary group back to the original, you could leave the new shell by running the exit command.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0