- How To: Linux Delete / Remove User Account Using userdel
- Linux delete user command syntax
- userdel command examples
- A Note About /etc/login.defs File
- Complete example to remove user account from Linux
- See also:
- How to Add or Remove a User from a Group in Linux
- Check a User Group in Linux
- Add a User to a Group in Linux
- Remove a User from a Group in Linux
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- 10 practical examples to add or remove user from group in Linux
- Difference between Primary vs Supplementary Group
- Primary group:
- Supplementary (or Secondary) Group:
- 1. Create a new user and add to existing primary group
- 2. Create a new user and add to existing supplementary group
- 3. Create a new user and add to existing primary and supplementary group
- 4. Change primary group of existing user
- 5. Add user to Group (Supplementary or Secondary) using usermod
- 6. Add user to multiple groups (Supplementary or Secondary) using usermod
- 7. Add user to Group (Supplementary or Secondary) using gpasswd
- 8. Add multiple users to same group
- 9. Remove user from Group (Supplementary or Secondary)
- 10. Remove multiple users from supplementary group
- 11. Remove user from all Groups (Supplementary or Secondary)
- Related Posts
How To: Linux Delete / Remove User Account Using userdel
H ow do I remove a user’s access from my server? How do I delete a user account under Linux operating systems include home directory and running cron jobs?
You need to use the userdel command to delete a user account and related files from user account under Linux operating system. The userdel command must be run as root user on Linux.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | Linux |
Est. reading time | 4 minutes |
Linux delete user command syntax
The syntax is as follows to remove a user account on Linux.
userdel userName
userdel [options] userName
userdel -r userName
userdel command examples
Let us remove the user named vivek or account named vivek from the local Linux system / server / workstation, enter:
# userdel vivek
Next, delete the user’s home directory and mail spool pass the -r option to userdel for a user named ashish, enter:
# userdel -r ashish
Explains how to delete user account with home directory in Linux
A Note About /etc/login.defs File
Default values are taken from the information provided in the /etc/login.defs file for RHEL (Red Hat) based distros. Debian and Ubuntu Linux based system use /etc/deluser.conf file:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Complete example to remove user account from Linux
The following is recommend procedure to delete a user from the Linux server. First, lock user account, enter:
# passwd -l vivek
OR set the date on which the user account will be disabled (syntax is usermod —expiredate YYYY-MM-DD userNameHere ):
# usermod —expiredate 1 vivek
If user try to login, he or she will get the following message:
Next, backup files from /home/vivek to /nas/backup
# tar -zcvf /nas/backup/account/deleted/v/vivek.$uid.$now.tar.gz /home/vivek/
Please replace $uid, $now with actual UID and date/time. Tye userdel command will not allow you to remove an account if the user is currently logged in. You must kill any running processes which belong to an account that you are deleting, enter:
# pgrep -u vivek
# ps -fp $(pgrep -u vivek)
# killall -KILL -u vivek
Delete at jobs, enter
# find /var/spool/at/ -name «[^.]*» -type f -user vivek -delete
Remove cron jobs, enter:
# crontab -r -u vivek
Delete print jobs, enter:
# lprm vivek
To find all files owned by user vivek, enter:
# find / -user vivek -print
You can find file owned by a user called vivek and change its ownership as follows:
# find / -user vivek -exec chown newUserName:newGroupName <> \;
Finally, delete user account called vivek, enter:
# userdel -r vivek
Sample session:
Fig.01: Delete User Accounts with Home Directory and All Data In Linux
See also:
- Help: Old Employees Accessing The Linux Server.
- /etc/passwd – The basic attributes of users.
- /etc/shadow – The basic attributes of users password.
- /etc/group – The basic attributes of groups.
- Man pages – ps(1)
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to Add or Remove a User from a Group in Linux
Linux is by default a multi-user system (meaning many users can connect to it simultaneously and work), thus user management is one of the fundamental tasks of a system administrator. User management includes everything from creating, updating, and deleting user accounts or user groups on a Linux system.
In this short quick article, you will learn how to add or remove a user from a group in a Linux system.
Check a User Group in Linux
To check a user group, just run the following groups command and provide the username (tecmint in this example) as an argument.
To check your own groups, just run the groups command without any argument.
Check a User Group in Linux
Add a User to a Group in Linux
Before trying to add a user to a group, ensure that the user exists on the system. To add a user to a certain group, use the usermod command with the -a flag which tells the usermod to add a user to the supplementary group(s), and the -G option specifies the actual groups in the following format.
In this example, tecmint is the username and postgres is the group name:
Add User to Group in Linux
Remove a User from a Group in Linux
To remove a user from a group, use the gpasswd command with the -d option as follows.
Remove User from Group in Linux
Additionally, on Ubuntu and it’s derivative, you can remove a user from a specific group using the deluser command as follows (where tecmint is the username and postgres is the group name).
For more information, see the man pages for each of the different commands we have used in this article.
You will also find the following user management guides very useful:
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
10 practical examples to add or remove user from group in Linux
Table of Contents
In this article I will cover the below topics and share multiple practical examples cover these scenarios
- Different types of group in Linux
- Add user to group (single)
- Add user to multiple groups
- Change primary group of the user
- Remove user from group
So when you say Linux add user to group, which group do you mean? There are two types of group in Linux
- Primary Group
- Supplementary or Secondary Group
Difference between Primary vs Supplementary Group
Primary group:
- The group that is listed in the group membership field for a user in /etc/passwd .
For example here deepak is part of primary group with GID 1000 i.e. deepak (so primary group name is same as loginname)
- When we create user using useradd , depending upon USERGROUPS_ENAB variable in /etc/login.defs a primary group is created/assigned to user. If this variable is set to yes, a group will be created for the user, with the same name as her loginname . If the variable is set to no, useradd will set the primary group of the new user to the value specified by the GROUP variable in /etc/default/useradd , or 100 by default
- On Linux, every file and directory has a user owner and a group owner. Group ownership is set when files are created, and unless configured otherwise, it is set to the primary group of the user who creates the file.
- A user at a time can be part of only one primary group.
Supplementary (or Secondary) Group:
- A user can be part of multiple supplementary group
- A group that a user is a member of but which membership is not defined in the /etc/passwd file.
- When creating new files, the supplementary group will not automatically become the owner of those files.
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 «
Verify the group exists
Next I will create a new user » user1 » and add this user to » admin » group using useradd -g
Verify the primary group of user1
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
I will create a new user » user2 » and add this user to all these supplementary groups using useradd -G , , ..
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
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
Verify the new user group details
4. Change primary group of existing user
I have a user who is currently added to his own primary group
I will change the primary group of this user to admin using usermod
Verify the new primary group for user4
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
Verify the user details
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 )
Verify the user details
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
Verify the user details
Alternatively you can also user gpasswd -a
In this example I will add user4 to qa_team as supplementary group
Verify the user details
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
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
Verify the admin group details
9. Remove user from Group (Supplementary or Secondary)
Currently my user4 is part of three different supplementary groups
gpasswd is the best tool to remove user4 from qa_team group
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 )
Verify the user details
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
I will write a small script to remove all the 3 users from admin group
remove multiple users from same 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
To remove user from all supplementary groups, use:
Verify the user details
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.
Related Posts
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.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Источник