How to Create a Group in Linux – Hivelocity Hosting
If your Linux distribution is supporting multiple users, it can often be easier to assign user permissions based on groups, rather than basing them on individual users. By creating groups with specific permissions, then assigning existing users to these groups, you can pass along the permissions of the group to any users within it. Read on to learn how to create groups in Linux and assign users to these new groupings.
Creating a Group in Linux
To create a new group in Linux, just follow these 4 steps:
-
First, only the root user or a user with sudo can create new groups. To create a new group, login as root user using your username and password.
-
After logging in, to create a new group, use the
groupadd
command. The command will look like:
groupadd
So, if you wanted to add a group called
NewGroup1
, you would use the command:
groupadd NewGroup1
*Note:
if the new group you are attempting to create already exists, Linux will return an error message informing you that the group already exists.
-
When completed successfully, the groupadd command will add data for your newly created group to the files
/etc/group
and
/etc/gshadow
.
-
When creating a new group, your system will automatically add a unique GID, an integer associated with the group, from the next available number on the list. If you’d like to manually specify the GID of your new group, you can do using the command:
groupadd -g ####
By replacing the “####” with a 4-digit integer, you can choose the specific GID associated with the group. For example, if you wanted to create a new group called
NewGroup2
and you want its GID to be
1073
, you can specify this using the command:
groupadd -g 1073 NewGroup2
*Note:
if the GID you selected is already in use by another group, an error message will be returned informing you of this.
Adding a User to a Group in Linux
Once you’ve created your new group, you can add existing users to this group using the command “usermod -a -G“. *Note: you must have sudo access to add users to groups.
The command for adding new users is as follows:
sudo usermod -a -G
So, if you wanted to add a user named User1 to a group named NewGroup1, the command would look like:
sudo usermod -a -G NewGroup1 User1
When entered successfully, no output will be displayed following the command. Only if an error is present, will an output be printed.
*Note: it is important to include the append option (-a), because without it, the command will also remove the designated user from all groups not included in the command.
You can also add a user to multiple groups at once by listing all the groups you’d like to add the user to before listing their username. So, if you wanted to add User1 to the groups NewGroup1, Group2, and Group3, you would use the command:
sudo usermod -a -G NewGroup1 Group2 Group3 User1
Removing a User from a Group
If you ever need to remove a user from a specific group, you can do so using the gpasswd command followed by the “-d” delete option. It should look like:
sudo gpasswd -d
So, if you wanted to remove User1 from NewGroup1, you could do so using the following command:
sudo gpasswd -d User1 NewGroup1
Deleting a Group in Linux
Occasionally, there may come a time when you’d rather delete an existing group. This can be done easily using the groupdel command followed by the name of the group you would like to delete.
sudo groupdel
So, if you wanted to delete the group NewGroup1 entirely, you could do so using the command:
sudo groupdel NewGroup1
And there you have it! Using these simple commands you can now easily create and delete groups as well as adding and removing users from these groups as needed.
Popular Links
Looking for more information on Linux? Search our Knowledge Base!
Interested in more articles about Operating Systems? Navigate to our Categories page using the bar on the left or check out these popular articles:
Popular tags within this category include: Linux, Windows, Apache, CentOS, Debian, Fedora, RedHat, and more.
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
Seeking a better Dedicated Server solution? In the market for Private Cloud or Colocation services? Check out Hivelocity’s extensive list of products for great deals and offers.
With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.
Unsure which of our services is best for your particular needs? Call or live chat with one of our sales agents today and see the difference Hivelocity can make for you.