The Wayback Machine - https://web.archive.org/web/20240826040556/https://www.geeksforgeeks.org/groupadd-command-in-linux-with-examples/
Open In App

How to Create a new group in Linux | groupadd command

Last Updated : 17 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In the Linux operating system, user management is a crucial aspect of system administration. One of the fundamental tasks is creating and managing user groups. Groups in Linux allow administrators to organize and control user access to various resources and files. The groupadd command is a powerful tool that facilitates the creation of new groups effortlessly. In this article, we will delve into the details of using the groupadd command to create a new group in Linux.

Understanding User Groups

Groups in Linux refer to the user groups. In Linux, there can be many users of a single system, (a normal user can take uid from 1000 to 60000, and one root user (uid 0) and 999 system users (uid 1 to 999)). In a scenario where there are many users, there might be some privileges that some users have and some don’t, and it becomes difficult to manage all the permissions at the individual user level. So, using groups, we can group together a number of users, and set privileges and permissions for the entire group. 

Before we dive into the practical aspects, let’s briefly understand the concept of user groups in Linux. A group is a collection of users with similar permissions or access levels. Groups make it easier to manage and assign permissions to multiple users at once, enhancing security and simplifying administrative tasks.

Syntax of the `groupadd` Command

The `groupadd` command follows a simple syntax:

groupadd [options] group_name

Here, `group_name` represents the name of the group you want to create. The command also allows you to specify various options to customize the group creation process.

How to create a new group in Linux

1. Open a terminal

Launch a terminal on your Linux system. This can usually be done by pressing `Ctrl + Alt + T` or searching for “Terminal” in the application menu.

2. Use the `groupadd` command:

To create a new group, type the following command in the terminal:

Example:  

If we want to create a group named “developers”, we can use the following command.

sudo groupadd developers 

Replace “mygroup” with the desired name for your new group. The sudo command is used to execute the groupadd command with administrative privileges.

3. Verify the creation of the new group:

Every new group created is registered in the file “/etc/group“. To verify that the group has been created, enter the command. 

sudo tail /etc/group
sudo tail /etc/group

sudo tail /etc/group

The file shows group information in the following format:  

group_name : password : group-id : list-of-members

Options Available in `groupadd` command

Options Description
-f, –force
  • This option forces the command to silently abort if the group with the given name already exists.
  • If used with the -g or –gid option and the specified group id already exists, the command forcefully ignores the given group id and assigns a new and unique group id.
-g GID, –gid GID
  • This option assigns a specific numeric group id to the newly created group.
  • The group id (GID) should be non-negative and unique, unless explicitly created to be non-unique using the -o or –non-unique option.
  • If not specified, the command assigns a default group id greater than any existing group id.
-h, –help
  • Displays a help message, providing information about the groupadd command and its available options.
  • Useful for quickly accessing command documentation.
-K KEY=VALUE, –key KEY=VALUE
  • Overrides the default values set in the /etc/login.defs file.
  • Multiple -K options can be specified.
  • Parameters like GID_MIN and GID_MAX, defined in /etc/login.defs, can be modified using this option.
-o, –non-unique
  • Permits adding a group with a non-unique group id (GID).
  • Useful when you want to create groups with duplicate GIDs.
-p PASSWORD, –password PASSWORD
  • Sets an encrypted password for the group.
  • The password, returned by crypt(3), is visible to users and is stored in the /etc/gshadow file.
  • By default, the password is disabled, and it is crucial to ensure it adheres to the system’s password policy.
-r, –system
  • Creates a system group.
  • System groups have numeric identifiers chosen within the SYS_GID_MIN-SYS_GID_MAX range, as defined in the /etc/login.defs file, instead of GID_MIN and GID_MAX.
-R CHROOT_DIR, –root CHROOT_DIR
  • Applies changes in the specified CHROOT_DIR directory and uses the configuration files from that directory.
  • Useful when managing groups within a chroot environment.

Pratical Examples to Create New Group in Linux Using Options Available

1. Using`-f, –force` option to Create New User.

Syntax:

groupadd -f developers
groupadd -f developers

groupadd -f developers

2. Specifying a Group ID to Create New User

If we want to assign a specific group id to the newly created group, use the -g option. 

Syntax:

sudo groupadd -g 1001 developers

This command will create a new group named “developer” with a GID of 1001. Specifying group ID is useful in case where we need to maintain consistency across systems.

3. Setting a Group Password

We can set an encrypted password for a group using the -p option.

Syntax:

sudo groupadd -p somepassword finance

This command creates a new group named “finance” and sets the password for the group as “somepassword”

4. Overriding Default Values

Suppose we want to modify the GID_MIN and GID_MAX values for a group creation. We can achieve this using the -K option

Syntax:

 groupadd -K GID_MIN=500 -K GID_MAX=700 tester
 groupadd -K GID_MIN=500 -K GID_MAX=700 tester

groupadd -K GID_MIN=500 -K GID_MAX=700 tester

This command will create a new group named “tester” and set the minimum GID to 500 and the maximum GID to 700. This allows us to have finer control over the range of valid group IDs for the created group.

5. To Create a new user into the group, the group is mentioned using -g option in the command useradd. 

Syntax:

sudo useradd -g developers new_user 

6. To add an existing user to a group, use the usermod command  

Syntax:

usermod -g developers existing_user  
usermod -g developers existing_user

usermod -g developers existing_user  

How to Create a new group in Linux | groupadd command – FAQs

How can I check if a group has been successfully created?

You can verify the successful creation of a group by checking the “/etc/group” file or by using the `getent` command.

For example: running `getent group group_name` should display information about the newly created group, including its name and Group ID (GID).

What is the difference between a regular group and a system group created with the groupadd command using the -r option?

The `-r` option in the `groupadd` command is used to create a system group. System groups typically have lower Group IDs (GID) and are reserved for system processes and services. Creating a system group is often recommended for enhanced system security and to prevent accidental modification or removal by regular users.

What happens if I try to create a group with a name that already exists?

If you attempt to create a group with a name that already exists, the groupadd command will typically return an error, stating that the group already exists. To avoid this, you may use the `-f` option to force the creation of the group, though caution should be exercised to prevent unintentional overwriting of existing groups.

How can I specify a Group ID (GID) when creating a new group with the groupadd command?

Yes, the `groupadd` command allows you to specify a Group ID using the `-g` option. This can be useful for maintaining consistency in user and group management, especially in environments with specific GID requirements.

What is the purpose of creating a new group in Linux using the groupadd command?

The primary purpose of creating a new group in Linux is to organize and manage users with similar permissions. Groups enable administrators to assign common access levels to multiple users, facilitating efficient resource management and enhancing system security.

Conclusion

In this article we have discussed `groupadd` command. By understanding and utilizing the options we have discussed in the `groupadd` command, we can effectively manage group IDs, create system groups, set group passwords, and customize default value according to our specific requirements and security policies.



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
How to Delete a Group in Linux | groupdel command
Group management is a crucial aspect of Linux system administration, and understanding how to create, modify, and delete groups is essential for maintaining a secure and organized environment. In this article, we will delve into the process of deleting a group in Linux using the 'groupdel' command. We will explore the command syntax, options, and p
3 min read
How to Create a Text File Using the Command Line in Linux
There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article. 1) touch commandThis is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The com
6 min read
How to Create Directory in Linux | mkdir Command
In Linux, the 'mkdir' command is like a magic wand for creating folders super easily. 'mkdir' stands for "make directory," and it helps you organize your computer stuff by creating folders with just one command. Whether you're making one folder or a bunch of them in a row, 'mkdir' is there to help you keep things neat and tidy on your computer. In
7 min read
How to Use ln Command to Create Symbolic Links in Linux
A symbolic link (symlink) is like a shortcut that points to a file or folder on Linux and other similar operating systems. Symlinks can be useful for organizing files and folders, or for making it easier to access files from different locations. In this guide, you'll learn how to create symbolic links in Linux using the 'ln' command. Use ln Command
5 min read
How to Create and Use Alias Command in Linux
Imagine you're lost in a maze of complicated Linux commands. You stumble upon a secret doorway marked "Alias," and inside you find shortcuts to all your favorite commands! That's what creating aliases is like. You get to make your own mini-commands for the long ones you use all the time, making things simpler and faster. This article guides you thr
6 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file without any content. The file created using the touch c
8 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
join Command in Linux
The join command in UNIX is a command line utility for joining lines of two files on a common field. Suppose you have two files and there is a need to combine these two files in a way that the output makes even more sense.For example, there could be a file containing names and the other containing ID's and the requirement is to combine both files i
7 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
fgrep command in Linux with examples
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as "^", "$", etc. Syntax: fgrep [options] [ -e pattern_list] [pattern] [file] Options with Description
3 min read
od command in Linux with example
od command in Linux is used to convert the content of input in different formats with octal format as the default format.This command is especially useful when debugging Linux scripts for unwanted changes or characters. If more than one file is specified, od command concatenates them in the listed order to form the input.It can display output in a
5 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
du Command in LINUX
While working on LINUX, there might come a situation when you want to transfer a set of files or the entire directory. In such a case, you might wanna know the disk space consumed by that particular directory or set of files. As you are dealing with LINUX, there exists a command line utility for this also which is du command that estimates and disp
9 min read
dmesg command in Linux for driver messages
dmesg command also called as "driver message" or "display message" is used to examine the kernel ring buffer and print the message buffer of kernel. The output of this command contains the messages produced by the device drivers. Usage of dmesg : When the computer boots up, there are lot of messages(log) generated during the system start-up. So you
5 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]... [FILE]... where, OPTION refers to the options compatib
2 min read
basename Command in Linux with examples
basename strips directory information and suffixes from file names i.e. it prints the file name NAME with any leading directory components removed. Using basename command : The basename command can be easily used by simply writing basename followed by the file name or the full pathname. Syntax of basename command : $basename NAME [SUFFIX] or $basen
3 min read
uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you h
8 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
for command in Linux with Examples
for command in Linux is used to repeatedly execute a set of command for every element present in the list. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; done Example: Options: help for : It displays help information.
1 min read
exit command in Linux with Examples
exit command in linux is used to exit the shell where it is currently running. It takes one more parameter as [N] and exits the shell with a return of status N. If n is not provided, then it simply returns the status of last command that is executed. Syntax: exit [n] Options for exit command - exit: Exit Without Parameter After pressing enter, the
1 min read