Linux remove group and user

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 .

Читайте также:  Linux mint x64 iso

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.

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.

Читайте также:  Examples for linux commands

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.

Источник

10 practical examples to add or remove user from group in Linux

Linux add user to group or remove user from group

1. Create a new user and add to existing primary group

  • By default when we create a new user, a new primary group is created by the same name as of the user.
  • But we can also use useradd to create a user and add this user to any existing group
  • So this group will not become the primary group of your new user

In this example I will create a new group » admin «

# getent group admin admin:x:1003:

Next I will create a new user » user1 » and add this user to » admin » group using useradd -g

Verify the primary group of user1

# id user1 uid=1003(user1) gid=1003(admin) groups=1003(admin)

2. Create a new user and add to existing supplementary group

We want to create a new user and add him/her to supplementary group (please NOTE, we will add the user to supplementary group and not the primary group here)

I have below list of groups on my Linux server

# egrep 'admin|devops|qa_team' /etc/group admin:x:1003: devops:x:1004: qa_team:x:1005:

I will create a new user » user2 » and add this user to all these supplementary groups using useradd -G ,,..

# useradd -G admin,devops,qa_team user2

Verify the supplementary groups.

Please NOTE that since we did not specified primary group using -g , a new group user2 is created and assigned as primary group to the user

# id user2 uid=1003(user2) gid=1006(user2) groups=1006(user2),1003(admin),1004(devops),1005(qa_team)

3. Create a new user and add to existing primary and supplementary group

Now we will combine both the above arguments i.e. -g to add primary_group and -G to add supplementary_group
In this example i will create user3 with primary_group as admin and with supplementary_group of devops and qa_team

# useradd -g admin -G devops,qa_team user3

Verify the new user group details

# id user3 uid=1003(user3) gid=1003(admin) groups=1003(admin),1004(devops),1005(qa_team)

4. Change primary group of existing user

I have a user who is currently added to his own primary group

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4)

I will change the primary group of this user to admin using usermod

Verify the new primary group for user4

# id user4 uid=1004(user4) gid=1003(admin) groups=1003(admin)

5. Add user to Group (Supplementary or Secondary) using usermod

  • To add user to group we can use use usermod or gpasswd command
  • We can add user to supplementary groups only
  • In this example I will add user4 to devops supplementary group

Syntax to add user to group: usermod -G

# id user4 uid=1004(user4) gid=1003(admin) groups=1003(admin),1004(devops)

6. Add user to multiple groups (Supplementary or Secondary) using usermod

We can also add user to multiple supplementary groups using syntax usermod -G ,,..
In this example I will add user4 to multiple supplementary groups ( devops and qa_team )

# usermod -G devops,qa_team user4
# id user4 uid=1004(user4) gid=1003(admin) groups=1003(admin),1004(devops),1005(qa_team)

7. Add user to Group (Supplementary or Secondary) using gpasswd

  • Similar to usermod we can also user gpasswd to add user to group
  • The syntax to add user to group is gpasswd -M

In this example I will add user4 to devops as supplementary group

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1004(devops)

Alternatively you can also user gpasswd -a

In this example I will add user4 to qa_team as supplementary group

# gpasswd -a user4 qa_team Adding user user4 to group qa_team
# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1005(qa_team)

8. Add multiple users to same group

We need to user gpasswd to add multiple users to same group
Currently I already have admin group which does not contain any users at the moment

# egrep admin /etc/group admin:x:1003:

The syntax to add multiple users to single group would be gpasswd -M ,,..

In this example I will add my existing users i.e. user1 , user2 and user3 to admin as supplementary group

# gpasswd -M user1,user2,user3 admin

Verify the admin group details

# egrep admin /etc/group admin:x:1003:user1,user2,user3

9. Remove user from Group (Supplementary or Secondary)

Currently my user4 is part of three different supplementary groups

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1003(admin),1004(devops),1005(qa_team)

gpasswd is the best tool to remove user4 from qa_team group

# gpasswd -d user4 qa_team Removing user user4 from group qa_team

We can also use usermod command to remove user from group. The problem with usermod is that you must define the complete list of supplementary group which the user is part of and only remove the group which you wat to remove the user from.

For example, my user4 is part of devops , admin and qa_team . So to remove user4 from qa_team we will re-add user to group devops and admin (not to qa_team )

# usermod -G devops,admin user4
# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4),1003(admin),1004(devops)

I would recommend using gpasswd to remove user from group

10. Remove multiple users from supplementary group

There is no single command to remove multiple users from single group but we can use a small script
Currently I have admin which has three users

# egrep admin /etc/group admin:x:1003:user1,user2,user3

I will write a small script to remove all the 3 users from admin group

remove user from group

11. Remove user from all Groups (Supplementary or Secondary)

  • We can use gpasswd to remove user from group
  • But if a user is part of multiple groups then you need to execute gpasswd multiple times
  • Or write a script to remove user from all the supplementary groups
  • Alternatively we can use usermod -G «»

Currently my user4 is part of multiple supplementary groups

# groups user4 user4 : user4 admin devops qa_team

To remove user from all supplementary groups, use:

# id user4 uid=1004(user4) gid=1006(user4) groups=1006(user4)
# groups user4 user4 : user4

Lastly I hope the steps from the article to add user to group, remove user from group and difference between primary group and supplementary group on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

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