- 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:
- 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 Remove Linux User from a Group
- Create Linux User
- Adding a User to a Group
- Find out the Groups a User belongs
- Removing User from a Group Using usermod
- Removing User from a Group Using gpasswd
- Removing User from a Group (Manually)
- Conclusion
- Linux deluser and delgroup commands
- Description
- To remove a normal user
- To remove a group
- To remove a user from a specific group
- Syntax
- Options
- Configuration
- Return values
- Files
- Examples
- Related commands
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.
Источник
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 Remove Linux User from a Group
A user can be a member of one primary and one or several secondary (supplementary) groups in Linux. The file ‘/etc/group’ defines group membership for each user in the Linux system.
In this tutorial, we will learn how to remove a user from a group in Linux. We will use two methods and also I will show how to manually remove the user from the group by deleting from ‘/etc/group’ file.
Create Linux User
For the purpose of this tutorial, we will create a user named ‘testuser’. When a new user is created, a new primary group with the same name for that user is created too.
We will create a new user by issuing the following command:
Now we can create a password for that user:
I used the same password as the username so I got a warning that the password shouldn’t contain the user name in some form.
Adding a User to a Group
First, we will create two new groups using groupadd command as follows:
Now we will add ‘testuser’ user to the above created two groups and also add to ‘root’ group using the following commands:
Ok, so now if we look at ‘/etc/group’ file and can see that ‘testuser’ is a member of all three groups.
Find out the Groups a User belongs
We can also use two alternative ways to check to which groups does a user belong as follows
As you can see the output is very similar and those commands accomplish the same thing.
Removing User from a Group Using usermod
We can remove a user from a group or several groups at once using usermod command. Using usermod you have to specify in which secondary groups you want to keep the user in. Let me explain with an example.
In order to remove user ‘testuser’ from ‘testgroup1’ and ‘testgroup2’ group run the below command ( ie leave testuser only in ‘root’ group and it’s primary ‘testuser’ group):
So in order to keep a user in more groups, you’ll need to mention group names separated by comma (,) like:
Removing User from a Group Using gpasswd
Another command that accomplishes similar results is gpasswd. We use this command to remove users from specified groups, unlike with usermod.
To remove a user from one specific group we can use gpasswd command:
Removing User from a Group (Manually)
We can also remove a user from a group by manually editing the file ‘/etc/group’. The effects of this method will apply to the user upon reboot.
You can use your favourite text editor to edit the ‘/etc/group’ file:
We will now manually edit the last two entries to delete testuser and remove it from testgroup1 and testgroup2 (edited file should look like this):
Changes will take place after reboot, and now the user has been removed from those two groups:
Conclusion
In this tutorial, we have learned how to remove a user from a group using usermod, gpasswd and also by manually deleting them from ‘/etc/group’ file.
These instructions can be used on any Linux system like Ubuntu, CentOS, Fedora and many others. If you have any questions, please let us know in the comments below.
Источник
Linux deluser and delgroup commands
On Linux operating systems, the deluser and delgroup commands remove a user or group from the system.
Description
deluser and delgroup remove users and groups from the system according to command line options and configuration information in /etc/deluser.conf and /etc/adduser.conf. They are friendlier front ends to the userdel and groupdel programs, removing the home directory as option or even all files on the system owned by the user to be removed, running a custom script, and other features. deluser and delgroup can run in one of three modes:
To remove a normal user
If called with one non-option argument and without the —group option, deluser removes a normal user.
By default, deluser removes the user without removing the home directory, the mail spool or any other files on the system owned by the user. Removing the home directory and mail spool can be achieved using the —remove-home option.
The —remove-all-files option removes all files on the system owned by the user. Note that if you activate both options —remove-home has no effect because all files including the home directory and mail spool are already covered by the —remove-all-files option.
If you want to backup all files before deleting them, you can activate the —backup option to create a file username.tar(.gz|.bz2) in the directory specified by the —backup-to option (defaulting to the current working directory). Both the remove and backup options can also be activated for default in the configuration file /etc/deluser.conf. See deluser.conf for details.
If you want to remove the root account (uid 0), then use the —force parameter; this prevents removing the root user by accident.
If the file /usr/local/sbin/deluser.local exists, it will be executed after the user account is removed to do any local cleanup. The arguments passed to deluser.local are: «username uid gid home-directory«.
To remove a group
If deluser is called with the —group option, or delgroup is called, a group will be removed.
Warning: The primary group of an existing user cannot be removed.
If the option —only-if-empty is given, the group won’t be removed if it has any members left.
To remove a user from a specific group
If called with two non-option arguments, deluser removes a user from a specific group.
Syntax
Options
—conf FILE | Use FILE instead of the default files /etc/deluser.conf and /etc/adduser.conf |
—group | Remove a group. This is the default action if the program is invoked as delgroup. |
—help | Display a help message, and exit. |
—quiet | Suppress progress messages. |
—system | Only delete if user/group is a system user/group. This avoids accidentally deleting non-system users/groups. Additionally, if the user does not exist, no error value is returned. |
—backup | Backup all files contained in the userhome and the mailspool file to a file named /$user.tar.bz2 or /$user.tar.gz. |
—backup-to | Place the backup files not in / but in the directory specified by this parameter. This implicitly sets —backup also. |
—remove-home | Remove the home directory of the user and its mailspool. If —backup is specified, the files are deleted after having performed the backup. |
—remove-all-files | Remove all files from the system owned by this user. Note: —remove-home does not have an effect any more. If —backup is specified, the files are deleted after having performed the backup. |
—version | Display version and Copyright information. |
Configuration
The file /etc/deluser.conf contains defaults for the programs deluser and delgroup. Each option takes the form «option = value«. Double or single quotes are allowed around the value. Comment lines must have a hash sign («#«) at the beginning of the line.
deluser and delgroup also read /etc/adduser.conf, see adduser.conf; settings in deluser.conf may overwrite settings made in adduser.conf.
The valid configuration options are:
REMOVE_HOME | Removes the home directory and mail spool of the user to be removed. Value may be 0 (don’t delete) or 1 (do delete). |
REMOVE_ALL_FILES | Removes all files on the system owned by the user to be removed. If this option is activated REMOVE_HOME has no effect. Values may be 0 or 1. |
BACKUP | If REMOVE_HOME or REMOVE_ALL_FILES is activated, all files are backed up before they are removed. The backup file that is created defaults to username.tar(.gz|.bz2) in the directory specified by the BACKUP_TO option. The compression method is chosen to the best that is available. Values may be 0 or 1. |
BACKUP_TO | If BACKUP is activated, BACKUP_TO specifies the directory to write the backup. Default is the current directory. |
NO_DEL_PATHS | A list of regular expressions, space-separated. All files to be deleted in course of deleting home directories or deleting files owned by the user to be deleted are checked against each of these regular expressions. If a match is detected, the file is not deleted. Defaults to a list of system directories, leaving only /home. In other words: By default, only files below /home belonging to that specific user are going to be deleted. |
ONLY_IF_EMPTY | Only delete a group if there are no user who belong to this group. Defaults to 0. |
EXCLUDE_FSTYPES | A regular expression that describes all file systems which should be excluded when looking for files of a user to be deleted. Defaults to «(proc|sysfs|usbfs|devpts|tmpfs|afs)«. |
Return values
0 | The action was successfully executed. |
1 | The user to delete was not a system account. No action was performed. |
2 | There is no such user. No action was performed. |
3 | There is no such group. No action was performed. |
4 | Internal error. No action was performed. |
5 | The group to delete is not empty. No action was performed. |
6 | The user does not belong to the specified group. No action was performed. |
7 | You cannot remove a user from its primary group. No action was performed. |
8 | The required perl package ‘perl modules’ is not installed. This package is required to perform the requested actions. No action was performed. |
9 | For removing the root account the parameter «—force» is required. No action was performed. |
Files
/etc/deluser.conf | Configuration file. |
Examples
Deletes the user account myuser.
Deletes the user account myuser, and removes that user’s home directory as well.
Deletes the user account myuser even if that user is currently logged in.
Deletes the user account myuser and backs up their files in a tar archive called myuser.tar, which is created in the directory /directory.
Deletes the group mygroup.
Related commands
adduser — Add a user to the system.
addgroup — Add a group to the system.
groupadd — Add a group to the system.
useradd — Add a user to the system.
Источник