In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to add users to the group in Linux. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Preface
The Linux group is the organizational unit used to manage user accounts in Linux. For each user and group in the Linux system, it has a unique numeric identification number. It is called user ID (UID) and group ID (GID). The main purpose of a group is to define a set of privileges for members of the group. They can all perform specific operations, but not others.
There are two types of default groups in Linux. Each user should have only one primary group primary group and any number of secondary group secondary group.
Primary group: when you created the user account, the primary group was added to the user. It is usually the name of the user. When you perform any action such as creating a new file (or directory), modifying a file, or executing a command, the primary group is applied to the user. The main group information for the user is stored in the / etc/passwd file.
Secondary group: it is called a secondary group. It allows user groups to perform specific actions in the same group member file. For example, if you want to allow a small number of users to run Apache (httpd) service commands, it would be a good fit.
You may be interested in the following articles related to user management.
Three ways to create a user account in Linux?
How to create bulk users in Linux?
How do I update / change a user's password using different methods in Linux?
It can be implemented in the following four ways.
Usermod: modify the system account file to reflect the changes specified on the command line.
Gpasswd: used to manage / etc/group and / etc/gshadow. Each group can have administrators, members, and passwords.
Shell script: allows the administrator to automate the required tasks.
Manually: we can manually add users to any group by editing the / etc/group file.
I assume you already have the groups and users required for this operation. In this example, we will use the following users and groups: user1, user2, user3, and the other groups are mygroup and mygroup1.
I want to check the user and group information before making any changes. See below for details.
I can see that the following users are associated with their own groups rather than with other groups.
# id user1uid=1008 (user1) gid=1008 (user1) groups=1008 (user1) # id user2uid=1009 (user2) gid=1009 (user2) groups=1009 (user2) # id user3uid=1010 (user3) gid=1010 (user3) groups=1010 (user3)
I can see that there are no associated users in this group.
# getent group mygroupmygroup:x:1012: # getent group mygroup1mygroup1:x:1013:
Method 1: use the usermod command
The usermod command modifies the system account file to reflect the changes specified on the command line.
How do I use the usermod command to add existing users to a secondary or additional group?
To add an existing user to a secondary group, use the usermod command with the-g option and the group name.
Syntax:
# usermod [- G] [GroupName] [UserName]
If the given user or group does not exist in the system, you will receive an error message. If you don't get any errors, the user has been added to the appropriate group.
# usermod-a Murg mygroup user1
Let me use the id command to view the output. Yes, it was added successfully.
# id user1uid=1008 (user1) gid=1008 (user1) groups=1008 (user1), 1012 (mygroup)
How do I use the usermod command to add existing users to multiple secondary or additional groups?
To add existing users to multiple secondary groups, use the usermod command with the-G option and the group name separated by commas.
Syntax:
# usermod [- G] [GroupName1,GroupName2] [UserName]
In this example, we will add user2 to mygroup and mygroup1.
# usermod-a Murg mygroup,mygroup1 user2
Let me use the id command to view the output. Yes, user2 has been successfully added to myGroup and myGroup1.
# id user2uid=1009 (user2) gid=1009 (user2) groups=1009 (user2), 1012 (mygroup), 1013 (mygroup1)
How to change the primary group of a user?
To change a user's primary group, use the usermod command with the-g option and the group name.
Syntax:
# usermod [- g] [GroupName] [UserName]
We have to use-g to change the user's main group.
# usermod-g mygroup user3
Let's look at the output. Yes, it has been changed successfully. Now, it shows that the main group of user3 is mygroup, not user3.
# id user3uid=1010 (user3) gid=1012 (mygroup) groups=1012 (mygroup)
Method 2: use the gpasswd command
The gpasswd command is used to manage / etc/group and / etc/gshadow. Each group can have administrators, members, and passwords.
How do I use the gpasswd command to add existing users to a secondary or additional group?
To add an existing user to a secondary group, use the gpasswd command with the-M option and the group name.
Syntax:
# gpasswd [- M] [UserName] [GroupName]
In this example, we will add user1 to the mygroup.
# gpasswd-M user1 mygroup
Let me use the id command to view the output. Yes, user1 has been successfully added to mygroup.
# id user1uid=1008 (user1) gid=1008 (user1) groups=1008 (user1), 1012 (mygroup)
How do I use the gpasswd command to add multiple users to a secondary or additional group?
To add multiple users to a secondary group, use the gpasswd command with the-M option and the group name.
Syntax:
# gpasswd [- M] [UserName1,UserName2] [GroupName]
In this example, we will add user2 and user3 to the mygroup1.
# gpasswd-M user2,user3 mygroup1
Let me use the getent command to view the output. Yes, user2 and user3 have been successfully added to myGroup1.
# getent group mygroup1mygroup1:x:1013:user2,user3
How do I remove a user from a group using the gpasswd command?
To remove a user from a group, use the gpasswd command with the-d option and the names of the user and group.
Syntax:
# gpasswd [- d] [UserName] [GroupName]
In this example, we will remove the user1 from the mygroup.
# gpasswd-d user1 mygroupRemoving user user1from group mygroup
Method 3: use Shell script
Based on the above example, I know that the usermod command does not have the ability to add multiple users to a group, which can be done through the gpasswd command. However, it will overwrite the existing users currently associated with the group.
For example, user1 is already associated with mygroup. If you use the gpasswd command to add user2 and user3 to mygroup, it will not take effect as expected, but will modify the group.
If you want to add multiple users to multiple groups, what is the solution?
There is no default option in either command to do this.
Therefore, we need to write a small shell script to achieve this.
How do I use the gpasswd command to add multiple users to a secondary or additional group?
If you want to use the gpasswd command to add multiple users to a secondary or additional group, create the following shell script.
Create a list of users. Each user should be on a separate line.
$cat user-lists.txtuser1user2user3
Use the following shell script to add multiple users to a single secondary group.
Vi group-update.sh #! / bin/bashfor user in `cat user- lists.txt`dousermod-a muri G mygroup $userdone
Sets the executable permissions for the group-update.sh file.
# chmod + group-update.sh
Finally, run the script to implement it.
# sh group-update.sh
Let me see the output using the getent command. Yes, user1, user2, and user3 have been successfully added to mygroup.
# getent group mygroupmygroup:x:1012:user1,user2,user3
How do I use the gpasswd command to add multiple users to multiple secondary or additional groups?
If you want to use the gpasswd command to add multiple users to multiple secondary or additional groups, create the following shell script.
Create a list of users. Each user should be on a separate line.
$cat user-lists.txtuser1user2user3
Create a list of groups. Each group should be on a separate line.
$cat group-lists.txtmygroupmygroup1
Use the following shell script to add multiple users to multiple secondary groups.
#! / bin/shfor user in `more group- lists.txt`dofor group in `more group- lists.txt`dousermod-a-G $group $userdone
Sets the executable permissions for the group-update-1.sh file.
# chmod + x group-update-1.sh
Finally, run the script to implement it.
# sh group-update-1.sh
Let me see the output using the getent command. Yes, user1, user2, and user3 have been successfully added to mygroup.
# getent group mygroupmygroup:x:1012:user1,user2,user3
In addition, user1, user2, and user3 have been successfully added to mygroup1.
# getent group mygroup1mygroup1:x:1013:user1,user2,user3
Method 4: manual method of adding users to a group in Linux
We can manually add users to any group by editing the / etc/group file.
Open the / etc/group file and search for the group name of the user you want to update. Finally, update the user to the appropriate group.
# vi / etc/group what is the Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.
The above is how to add users to the group in Linux. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.