Linux delete user and home directory

Linux delete user and home directory

NAME

deluser, delgroup - remove a user or group from the system

SYNOPSIS

deluser [options] [--force] [--remove-home] [--remove-all-files] [--backup] [--backup-to DIR] user deluser --group [options] group delgroup [options] [--only-if-empty] group deluser [options] user group COMMON OPTIONS [--quiet] [--system] [--help] [--version] [--conf FILE]

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 be run in one of three modes: Remove a normal user If called with one non-option argument and without the --group option, deluser will remove a normal user. By default, deluser will remove 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 will have 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 which will 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(5) for details. If you want to remove the root account (uid 0), then use the --force parameter; this may prevent to remove the root user by accident. If the file /usr/local/sbin/deluser.local exists, it will be executed after the user account has been removed in order to do any local cleanup. The arguments passed to deluser.local are: username uid gid home-directory 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. Remove a user from a specific group If called with two non-option arguments, deluser will remove a user from a specific group.

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 brief instructions. --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. This option is mainly for use in Debian package maintainer scripts. --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.

RETURN VALUE

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

SEE ALSO

deluser.conf(5), adduser(8), userdel(8), groupdel(8)
Copyright (C) 2000 Roland Bauerschmidt. Modifications (C) 2004 Marc Haber and Joerg Hoh. This manpage and the deluser program are based on adduser which is: Copyright (C) 1997, 1998, 1999 Guy Maor. Copyright (C) 1995 Ted Hajek, with a great deal borrowed from the original Debian adduser Copyright (C) 1994 Ian Murdock. deluser is free software; see the GNU General Public Licence version 2 or later for copying conditions. There is no warranty.

© 2019 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.

Читайте также:  Total network inventory linux

Источник

How to Delete User Accounts with Home Directory in Linux

In this tutorial, I am going to take your through steps you can use to delete a user’s account together with his/her home directory on a Linux system.

Delete User Accounts with Home Directory in Linux

To learn how to create user accounts and manage them on Linux systems, read the following articles from the links below:

As a System Administrator in Linux, you may have to remove users account at after sometime when a user account may become dormant for so long, or user may leave the organization or company or any other reasons.

When removing user accounts on a Linux system, it is also important to remove their home directory to free up space on the storage devices for new system users or other services.

Deleting/Removing a User Account with His/Her Home Directory

1. For demonstration purpose, first I will start by creating two user accounts on my system that is user tecmint and user linuxsay with their home directories /home/tecmint and /home/linusay respectively using adduser command.

# adduser tecmint # passwd tecmint # adduser linuxsay # passwd linuxsay

Create New User Accounts in Linux

From the screenshot above, I have used the adduser command to create user accounts on Linux. You can also use useradd command, both are same and does the same job.

2. Let’s now move further to see how to delete or remove user accounts in Linux using deluser (For Debian and it’s derivatives) and userdel (For RedHat/CentOS based systems) command.

The directives inside the configuration file for deluser and userdel commands determine how this it will handle all user files and directory when you run the command.

Читайте также:  Linux how to read serial port

Let us look at the configuration file for the deluser command which is /etc/deluser.conf on Debian derivatives such as Ubuntu, Kali, Mint and for RHEL/CentOS/Fedora users, you can view the /etc/login.defs files.

The values in the these configuration are default and can be changed as per your needs.

# vi /etc/deluser.conf [On Debian and its derivatives] # vi /etc/login.defs [On RedHat/CentOS based systems]

3. To delete a user with home directory, you can use the advanced way by following these steps on your Linux server machine. When users are logged on to the server, they use services and run different processes. It is important to note that user can only be deleted effectively when they are not logged on to the server.

Lock User Accounts in Linux

Start by locking the user account password so that there is no access for the user to the system. This will prevent a user from running processes on the system.

The passwd command including the –lock option can help you achieve this:

# passwd --lock tecmint Locking password for user tecmint. passwd: Success

Lock User Account Password in Linux

Find and Kill All Running Processes of User

Next find out all running processes of user account and kill them by determine the PIDs (Process IDs) of processes owned by the user using:

# pgrep -u tecmint 1947 1959 2091 2094 2095 2168 2175 2179 2183 2188 2190 2202 2207 2212 2214

Then you can list the processes interms of username, PIDs, PPIDs (Parent Process IDs), terminal used, process state, command path in a full formatting style with the help of following command as shown:

# ps -f --pid $(pgrep -u tecmint) UID PID PPID C STIME TTY STAT TIME CMD tecmint 1947 1 0 10:49 ? SLl 0:00 /usr/bin/gnome-keyring-daemon --daemonize --login tecmint 1959 1280 0 10:49 ? Ssl 0:00 mate-session tecmint 2091 1959 0 10:49 ? Ss 0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session tecmint 2094 1 0 10:49 ? S 0:00 /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session tecmint 2095 1 0 10:49 ? Ss 0:00 //bin/dbus-daemon --fork --print-pid 6 --print-address 9 --session tecmint 2168 1 0 10:49 ? Sl 0:00 /usr/lib/dconf/dconf-service tecmint 2175 1959 0 10:49 ? Sl 0:02 /usr/bin/mate-settings-daemon tecmint 2179 1959 0 10:49 ? Sl 0:47 marco tecmint 2183 1 0 10:49 ? Sl 0:00 /usr/lib/gvfs/gvfsd tecmint 2188 1959 0 10:49 ? Sl 0:00 mate-panel tecmint 2190 1 0 10:49 ? Sl 0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes tecmint 2202 1 0 10:49 ? S

Find All Running Processes of User

Once you find all the running processes of user, you can use the killall command to kill those running processes as shown.

The -9 is the signal number for the SIGKILL signal or use -KILL instead of -9 and -u defines username.

Note: In recent releases of RedHat/CentOS 7.x versions and Fedora 21+, you will get error message as:

-bash: killall: command not found

To fix such error, you need to install psmisc package as shown:

# yum install psmisc [On RedHat/CentOS 7.x] # dnf install psmisc [On Fedora 21+ versions]

Backup User Data Before Deleting

Next you can backup users files, this can be optional but it is recommended for future use when need arises to review user account details and files.

I have used the tar utilities to create a backup of users home directory as follows:

# tar jcvf /user-backups/tecmint-home-directory-backup.tar.bz2 /home/tecmint

Backup User Home Directory in Linux

Delete/Remove User Account and Files

Now you can safely remove user together with his/her home directory, to remove all user files on the system use the --remove-all-files option in the command below:

# deluser --remove-home tecmint [On Debian and its derivatives] # userdel --remove tecmint [On RedHat/CentOS based systems]

Delete User Account with Home Directory

Summary

That is all to do with removing user and their home directory from a Linux system. I believe the guide is easy enough to follow, but you can voice a concern or add more idea by leaving a comment.

Источник

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