As human beings, when we discover a new object, our brain does a couple of things. One, we see the utility of the object. Two, we assign the object a group so that we know how to classify it moving forward. For example, if a child discovers a hammer, they generally are aware that it could be used to hit things. They then assign the hammer to a group. In some cases, the child labels the hammer as a tool and will only use it to accomplish a task. In others, the child will label the hammer a toy. However, the utility of the hammer is still apparent, so be sure to watch out for the latter child. This is why grouping matters.
Groups in Linux are important because users are present on the system for a defined utility. Grouping users by utility (or access) is a fantastic way to ensure that only admins have admin privileges and that general users can only access files that they are meant to. Users are broken down into three distinct classifications to make the grouping process a little easier:
- Superusers - responsible for the administration of the system. The root account is the chief superuser and has
UID 0. - System users - user accounts used by the system itself to run processes or daemons. Each of these users has its own files and resources. Ideally, users do not log in as a system user. The range of UIDs is
1-999. - Regular users - accounts that most of us use to accomplish our daily work. They are limited in their access to files and systems and must obtain sudoer permissions to perform administrative tasks. The UIDs start at
1000+.
If you would like further information on basic user and group concepts, check out Ken Hess' article User account management with UIDs and GIDs. For the rest of this piece, I focus on group creation, membership, and identification codes, as well as how to assign superuser privileges to a group.
Primary group
All users are assigned to a primary group by default. For local accounts, the primary GID is the same as the one listed in the /etc/passwd file. This means that the user name and the group name are the same. It also means that the user is the sole member of that group. For example:
[root@server ~]# useradd user01
[root@server ~]# su - user01
[user01@server ~]$ id
uid=1002(user01) gid=1002(user01) groups=1002(user01) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
This design's side effect is that it makes managing file permissions much simpler for the users and administrators. If you wish to collect users into a primary group for a common purpose, you need to create a group and then assign the desired users to it.
[ You might also like: Linux sysadmin basics: User account management ]
Group creation
For our demonstration, I am going to create the group demogroup. You can see from the output that the group has been created and assigned GID 1007:
[root@server ~]# groupadd demogroup
[root@server ~]# tail /etc/group
cockpit-wsinstance:x:975:
flatpak:x:974:
rngd:x:973:
admin:x:1001:
user01:x:1002:
user02:x:1003:
user03:x:1004:
user04:x:1005:
user05:x:1006:
demogroup:x:1007:
Now let's change the GID to 10007:
[tcarrigan@server ~]$ sudo groupmod -g 10007 demogroup
Output Omitted
[tcarrigan@server ~]$ sudo tail /etc/group
Output Omitted
demogroup:x:10007:
Use the groupmod -g GID groupname syntax to change any group's GID if needed. If you need to change a group's name, use groupmod -n NEWNAME oldname. Here is an example:
[tcarrigan@server ~]$ sudo groupmod -n usergroup demogroup
[tcarrigan@server ~]$ sudo tail /etc/group
Output Omitted
usergroup:x:10007:
Note that we renamed demogroup to usergroup. The group is still the same, as indicated by the GID 10007.
Adding users to a group
A group without members is like a forest without trees. So how do we add a few members? Easy to do.
[tcarrigan@server ~]$ sudo usermod -g usergroup user01
[tcarrigan@server ~]$ sudo usermod -g usergroup user02
[tcarrigan@server ~]$ sudo usermod -g usergroup user03
[tcarrigan@server ~]$ sudo usermod -g usergroup user04
[tcarrigan@server ~]$ sudo usermod -g usergroup user05
You can see the change in GID and group name from when we created user01 at the start of this exercise.
[user01@server ~]$ id
uid=1002(user01) gid=10007(usergroup) groups=10007(usergroup) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Supplementary group
Now, a user can also belong to a supplementary group. A supplementary group is just that—supplementary. A very popular use for supplementary groups is to assign administrative privileges via sudo.
For example, if user01 needs to change permissions for other users in usergroup, without assigning admin permissions to the entire group, we assign user01 to a supplementary group with admin permissions. On many systems, the wheel group is used for just such occasions.
To add wheel as a supplementary group to user01:
[tcarrigan@server ~]$ sudo usermod -aG wheel user01
[tcarrigan@server ~]$ su - user01
Output Omitted
[user01@server ~]$ id
uid=1002(user01) gid=10007(usergroup) groups=10007(usergroup),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
NOTE: The -a option forces usermod into append mode. If the -a option is not used, the user is removed from any other supplementary groups not listed in the -G option's list.
You can now see that user01 has a primary group (usergroup) and a supplementary group (wheel).
Sudo group config
What if we need to give admin privilege to the entirety of usergroup? To accomplish this, we need to create the /etc/sudoers.d/usergroup file:
[root@server ~]# echo "%usergroup ALL=(ALL) ALL" >> /etc/sudoers.d/usergroup
[root@server ~]# su - user02
[user02@server ~]$ sudo cat /etc/sudoers.d/usergroup
[sudo] password for user02:
%usergroup ALL=(ALL) ALL
You can see above that user02, a member of usergroup, now has admin privileges. For more information on admin privileges and the sudoers file, check out my previous article Linux command line basics: sudo.
[ Want to test your sysadmin skills? Take a skills assessment today. ]
Why should you care?
If you ask yourself the above question, think practically about the following concepts: Using groups in Linux is a fundamental part of the operating system and is even required to run various applications. Assigning users to easily-controlled groups is a great way to quickly increase security on your systems and manage multiple user accounts. As shown above, you can allow certain users to access admin privileges to get specific work finished without giving them root access to the system. If we can keep our users organized and productive, while ensuring that they only have access to what they need, we make our lives easier, and our user-to-admin relationships will be a bit less stressful. Your organization will benefit from these improvements from the ground up, whether they know it or not.
저자 소개
Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. Now that he has surfaced, he lives with his wife and son near Raleigh, where he worked as a data storage engineer before finding his way to the Red Hat team. He has written numerous technical documents, from military procedures to knowledgebase articles and even some training curricula. In his free time, he blends a passion for hiking, climbing, and bushcraft with video games and computer building. He is loves to read and enjoy a scotch or bourbon. Find him on Twitter or on LinkedIn.
유사한 검색 결과
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래