Hello readers, welcome to my blog on users and groups in Linux. As we know that Linux is a multi-user operating system, we’ll see how to add and configure the users and groups in Linux.
What does multi-user mean?
An operating system is considered as multi-user when it allows multiple users to access a computer. Also, Keeping security in mind, it does not allow a user to manipulate other user’s stuff including files, folders, preferences etc. Linux allow multiple users to use the computer simultaneously.
Adding Users in Linux
To manipulating the user accounts, you need to be a root user. To become one, you need to use the following command:
sudo --login --OR-- sudo -i
This will prompt you for the password. After successful login, the terminal
$ prompt will change to
# to indicate that you are logged in as a root user. You can also verify the same by using the
whoami command which shows the currently logged in user, which in our case would be
Now to add the user you can enter the following command:
useradd <new_username> --create-home
Whenever you run this command, it will create the user’s home directory as
/home/<username>. The user’s home directory is replicated from
/etc/skel directory. All the files present in this folder will be copied to new user’s home directory.
The user’s information is stored in the
/etc/passwd file. This file stores the user information in the following format:
Here’s the description of the above format:
- username: This field contains the username of the account.
- x: This field contains the password stored in
/etc/shadowin an encrypted format.
- UID and GID: These fields contains an unique integer number for the user and the primary group of that user.
- comment: This field include any specific information about the user. It can be provided by using the
--comment/-cflag while creating the user.
- home_directory: This include the path to the user’s home directory. By default, it is
/home/<username>. It can be changed by using the
--home-dir / -dwhile creating the user.
- default_shell: This field includes the path to the shell used by the user. We can specify any particular shell for the user using
--shell or -sflag while creating the user.
Deleting a user in linux
To delete a user, you can enter the following command:
The above command will permanently remove the user, but its home folder will still remain. To remove the home folder also, you can use the following command:
userdel --remove <username>
Groups in linux
Users on Linux systems are assigned to one or more groups for the following reasons:
- To share files or other resources with a small number of users
- Ease of user management
- Ease of user monitoring
- Group membership is a perfect solution for large Linux (UNIX) installation.
- Group membership gives you or your user special access to files and directories or devices which are permitted to that group
In Linux, every user is associated with 2 groups: primary group & secondary group
- Primary group: This group is the default group from which your account is associated when you log in. Usually, the name of the group is the same as the name of the user.
- Secondary group: This is any other group you are a member of other than your primary group.
Note: Each user can belong to exactly one primary group and zero or more secondary groups.
Adding a group in Linux
To add a group in linux, you need to enter the following command:
This will create a new group with a random group id (GID). To provide a GID of your own, you can specify the
-g flag as follows:
groupadd -g 350 <group_name>
To verify this, we can check the
/etc/group which contains the information of the Linux groups. The file stores the user information in the following format:
Here’s the description of the above format:
- group_name: This field contains the group name.
- x: This field contains the encrypted password.
- GID: This field contains the group id.
- group_user_list: This field contains the list of users which falls in this group
Add an Existing user to the group
To add an existing user to a single secondary group, use the
usermod command with
-G flag as follows:
usermod -G <group_name> <username>
Now, to add the user to multiple groups, we can provide multiple comma(,) separated group names as follows:
usermod -G <group1>,<group2>,<group3> <username>
Assign groups to new User
We can also assign primary and secondary groups while create a user by using the
-G flags. The
-g flag is used to define the primary group, whereas the
-G flag is used to provide a list of secondary groups as shown below:
useradd -g <primary_group> -G <s_group1>,<s_group2> <username>
Display User Groups
To display the user groups, simple use the
id command as follows:
id <username> Output: uid=xxxx(username) gid=xxxx(group_name) groups=xxx(group1),xxx(group2)
You can also view the groups associated with the user by using the
groups command as follows:
groups <username> Output: <primary_group> : <s_group1> <s_group2> <s_group3>
As you have reached the end, now you’ll be familiar with Linux users and groups. If you have any queries, feel free to contact me at email@example.com.
Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies