How do I add users to another users group?
Sebastian Wright
I have an user john and an user keith in Ubuntu server
I added these users using
sudo adduser John
sudo adduser KeithHow can I now add john to Keith's group?
I tried:
sudo usermod -aG Keith johnBut I get:
usermod: group 'Keith' does not existWhat do I need to do to resolve this issue?
3 Answers
You can use: usermod -a -G grouptoadd username
Since it is far less obvious than I thought why -a -G is ok while -aG is not I'll try to explain it:
The -G options takes either one value or a list of values. In case of one value the value may be given like: -GVALUE since there is no requirement to have a space between the option and the value. This is just common practice.
If you have a group called a on your system, then calling -Ga becomes ambiguous. Do you want the user to have only one group a? Are you calling -a and -G? Then is "a" a value to -G? Where is the rest of the value (to -G)? It is also common practice that any single-letter option can before any other single letter option. so tat -xy == -yz. But this is ambiguous when one of the options can take either a single value like VALUE or many values like VAL1,Val2,val4,etc.
So in order to make the command FAILSAFE and work properly for all input you have to separate -a from -G
From man usermod:
7Name
usermod - modify a user account
Synopsis
usermod [options] LOGIN
Description
The usermod command modifies the system account files to reflect the changes that are specified on the command line.
Options
The options which apply to the usermod command are:
-a, --append
Add the user to the supplementary group(s). Use only with the -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. The groups are subject to the same restrictions as the group given with the -g option.
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.
...
If you executed the commands exactly as you have shown in your question, you did not create the user Keith, but only the user John. To execute commands one after the other on the command line you need to separate them using ; or &&. Otherwise, only the first command will be run:
sudo adduser John sudo adduser Keith
adduser: Only one or two names allowed.If you did execute them correctly, be careful about capital letters. In your question you mention both "John" and "john". This should work:
sudo adduser keith
sudo adduser john
sudo usermod -aG keith john You can also edit the configuration files manually:
sudo nano /etc/password
sudo nano /etc/groupAssign the ID and other fields.