Linux add group to groups

How to Manage Users and Groups on Ubuntu 22.04

Linux is a multi-user and multi-tasking operating system. User and group management are the two most important tasks to be performed by Linux administrators.

In Linux, each user has their own login name and a home directory. Every user belongs to a primary group, and users can be added to multiple secondary groups. All users in the group will have the same group permission on files and folders. This makes it easier to provide permission for multiple users.

This tutorial will demonstrate how to manage users and groups in the Linux system.

A Quick Overview

The command-line tools to manage the users and groups in Linux are:

adduser / useradd : To add a user

addgroup / groupadd : To add a group

usermod : To modify a user account

deluser / userdel : To delete a user

delgroup / groupdel : To delete a group

passwd : To change the user’s password

We will cover the practical examples of all commands in this article. To follow the tutorial, you will need to switch to the root user or any user with sudo privileges.

1. Create a new user

You can add a new user to the system using the adduser command. The following command creates a new user henry in the system.

It will prompt you to enter the password for the new user and other user details.

adduser command to create a new user in linux

To verify the user, you can try to log in as a new user.

log in as another user in linux

2. Understanding the /etc/passwd file

The /etc/passwd is a plain text file that stores the user account information in Linux. You can use the cat command to view the content of /etc/passwd .

view the content of a etc passwd file in linux

Each user has one entry per line. The fields are separated by a colon : symbol and contains the following information.

username:password:UID:GID:GECOS:home_directory:shell

The new entries are saved at the end of a file. To find a user henry , you can see the last entries. Alternatively, you can use the grep command.

search for a specific user in a passwd file

3. Change the login name of a user

You can use the usermod command to change a user’s login name in Linux. This command renames the user henry to james .

$ sudo usermod -l james henry

usermod command to change the login name of a user

As you can see, the username is changed to james .

Читайте также:  Pixel editor for linux

4. Change the user ID of a user

By default, the system automatically sets the next available UID when creating a user. The usermod command with -u flag can be used to change the UID of a user.

The following command changes the user ID of james to 4567 .

$ sudo usermod -u 4567 james

usermod command to change the uid of a user in linux

5. Change the group of a user

The -g option with usermod command changes the primary group of a user. For example, to change the primary group of a user james to linuxwizardry , you can run this command.

$ sudo usermod -g linuxwizardry james

The specified group must already exist in the system.

usermod command to change the group of a user

In Linux, a user can have only one primary group. But you can assign a user to multiple secondary groups. The -G flag allows you to specify the secondary group for a user.

The following command adds a user james to the group ubuntu .

$ sudo usermod -G ubuntu james

usermod command to add a user to the secondary group

6. Add a user to the sudoers group

You can add a user to the sudoers group and provide sudo privileges to that user. This command adds a user james to the sudo group.

$ sudo usermod -aG sudo james

The -a option adds a user to the group without removing the current group.

Next, log in as a user james and run the sudo command to confirm.

add a user to the sudoers group

7. Change the password of a user

The passwd command is used to change the user’s password in Linux. The following command changes the password of a user james .

passwd command to change user password in linux

8. Delete a user

When the user account is not needed, you might want to delete it from the system. The userdel command helps to remove a user in Linux.

The below command deletes a user james from the system.

userdel command to delete a user in linux

9. Delete a home directory of a user

The usedel command without any flags only removes a user. It does not delete the home directory of a user in the /home directory.

To delete a user along with its home directory, you can use:

10. Add a new group

You can add a new group to the system using the groupadd or addgroup command. The following example creates a new group computer on the system.

addgroup command to add a group in linux

11. Understanding /etc/group file

The /etc/group file stores the group details in a list. Each entry contains the following group information for each group.

group_name:group_pwd:group_id:group_list

You can display the entries in /etc/group file with the cat command.

display the entries in etc group file in linux

12. Create a system group

If you need to add a new system group, you can use the -r flag with the groupadd command. This command creates a new system group sysmin .

groupadd command to add a system group in linux

Output:

13. Add a new group with specific GID

When creating a new group, the system assigns the next available group ID by default. You can change this behavior and specify a GID for a new group with the -g flag.

The following command creates a new group bank with a custom group ID 644 .

add a group with a specific id in linux

Output:

14. Remove a user from the group

Sometimes you might need to remove a user from the secondary groups. You can do it by specifying the username and group to the deluser command.

Читайте также:  Lotus note client linux

The below command removes a user rohan from the group ubuntu .

$ sudo deluser rohan ubuntu

removing a user from the group in linux

Output:

15. Delete a group

You can remove a group from the system using the delgroup or groupdel command.

To delete a group ubuntu , run the following command.

delgroup command to delete a group in linux

Output:

If the specified group is the primary group of any user, it cannot be deleted. You must first change the primary group of a user.

Conclusion

Managing users and groups is one of the essential skills for every Linux administrator. You have learned the different examples of user and group management commands in Linux. Now you know how to perform the tasks like creating new users and groups, adding users to groups, changing the username and password, deleting users and groups, and much more.

We hope you found this article helpful. Please let us know if you have any confusion about any examples in the comment section below.

Источник

How to Add a User to a Linux Group

In Linux, a group is a unit in which you can manage privileges for several users simultaneously. Linux groups allow you to manage multiple user permissions quickly and easily.

In this tutorial learn how user groups work in Linux, and how to add users to specific groups.

Tutorial on how to add a user to a group on Linux.

  • A system running Linux
  • A user account with sudo or root privileges
  • Access to a terminal window/command line (Ctrl-Alt-T, Ctrl-Alt-F2)

What is a User Group in Linux

In Linux, different users have different roles or responsibilities. Some users might need the ability to execute applications, while others are restricted from accessing specific files and folders.

Groups let you create categories of users with pre-set permissions. Instead of managing permissions for each user account, you can simply add a user to a group to grant the appropriate permissions.

Primary Group

The primary group is set to the logged-in user. Any files the user creates are automatically added to that group. A user can only belong to one primary group at a time. A primary group with the same name as the user is created, and any files created by the user are included in that group.

Secondary Groups

A user can belong to any number of secondary groups (including none). Secondary groups are created to manage individual files and software applications. Members of the group inherit the read, write, and execute privileges for that group.

Note: Refer to our in-depth guide on how to create users in Linux for more info on user management.

How to Create a User Group

1. To create a new group, enter the following:

2. Replace new_group with the name you want for your new group.

create a user group in linux

How to Add User to Group

Add an Existing User to an Existing Group

1. Use the adduser command to add a user to a group:

sudo adduser user_name new_group

add existing user to an existing group

2. Use the useradd command to add a user:

sudo useradd –G new_group user_name

3. You can also use the usermod command to add a user to a group:

sudo usermod –a –G group_name user_name

The usermod command uses the –append and –group options to append the user to a particular group. Without using –append , the user could be dropped from other groups.

Читайте также:  Linux what is super key

Add a User to Multiple Groups at Once

Use the usermod command to specify multiple groups to add to:

sudo usermod –a –G new_group,new_group2,new_group3 user_name

Create a User and Add to Group

1. This is useful for creating a new user on the fly for a specific software application. Enter the following:

sudo useradd –G new_group new_user

2. Next, assign a password to the new user:

Change a Users Primary Group

All previous commands have been used to manage the secondary groups a user belongs to. In most cases, a user’s primary group is the same as their username.

To change a users primary group, enter the command:

sudo usermod –g new_group user_name

The lower-case –g specifies the primary group. (Upper-case –G refers to a secondary group.) A user can only have one primary group, so the old primary group user_name won’t be primary anymore for this user.

How to Remove a User From a Group

The gpasswd tool is used for managing groups. To remove a user from a group:

sudo gpasswd –d user_name new_group

Note: The gpasswd tool can also be used for other administrative tasks such as defining group administrators and setting a password for access to group resources. Use the Linux man command man gpasswd for details.

Delete a Group

To delete a group, use the command:

example of deleting a user group in linux

How to List Groups in Linux

Linux comes with several different groups by default. Some of these, like the sudo group, can be used to grant permissions. Others are hidden, used for system tasks.

1. To view a list of groups on your system by displaying the /etc/groups file:

2. To display the groups that a user belongs to with the groups command:

find groups of logged-in user

3. The image above shows the groups that the logged-in user ‘sofija’ belongs to. You can display groups for a different user by specifying the username:

4. Another method to display the groups a user belongs to, including user ID (uid) and group ID (gid), is to use the id command:

Other Common Groups

There are a several common group names you might encounter in Linux:

  • sudo – A member of this group can use the sudo command to elevate their privileges
  • wheel – This is an older method of granting sudo-like privileges
  • cdrom – Allows the user to mount the optical drive
  • adm – Allows the user to monitor Linux system logs
  • lpadmin – Allows the user to configure printers
  • plugdev – Allows the user to access external storage devices

You should now have a good understanding of Linux groups and how to add and remove members from those groups. For more information on specific commands, you can enter the man command to display a manual in your terminal window.

Источник

Оцените статью
Adblock
detector