There are two type of group concept in linux :primary vs secondary groups,it is simple to create a new group:
sudo groupadd gname
Now i want to know the new created group gname
belongs to which type of group?Is there a way to inter-convert between them?
Create two new group which contain no user
sudo groupadd writers
sudo groupadd editers
Add new user.
sudo useradd -m mywriter -p yourpassword
sudo useradd -m myediter -p yourpassword
Add new user into group.
sudo usermod -a -G writers mywriter
sudo usermod -g editers myediter
Now we get 4 groups.
cat /etc/group | tail -4
writers:x:1007:mywriter
editers:x:1008:
mywriter:x:1009:
myediter:x:1010:
Enter into mywriter
$ touch /tmp/mywriter.txt
$ ls -al /tmp/mywriter.txt
-rw-r--r-- 1 mywriter mywriter 0 Aug 23 21:24 /tmp/mywriter.txt
Enter into myediter
$ touch /tmp/myedier.txt
$ ls -al /tmp/myediter.txt
-rw-r--r-- 1 myediter editers 0 Aug 23 21:27 /tmp/myediter.txt
For group1----mywriter:
id mywriter
uid=1009(mywriter) gid=1009(mywriter) groups=1009(mywriter),1007(writers)
It is a primary group mywriter
since uid=gid=1009.
For group2--myediter
id myediter
uid=1010(myediter) gid=1008(editers) groups=1008(editers)
It is a secondary group?
How to classify group writers
and editers
?
-g
option is the initial (or primary) group for the user, while groups added using the-G
option are supplementary (or secondary) groups for the user. I think the missing piece here isuseradd
's default behavior (its configuration depends on the system you are on): unless you tell it not to, it creates a group named after the new user and sets is as the initial (primary) group for that user. Here, you are leaving it unchanged for themywriter
user, while you are replacing it withediters
for themyediter
user. – fra-san Aug 24 '20 at 08:49