What option can be added to the Userdel command to remove the home directory of the target user?

If you manage an Ubuntu system, sooner or later, you'll need to add new users to the system or remove the existing ones.

Assuming that you are using an Ubuntu server, I'll discuss the command line methods here.

Below is the command to add a new user in Ubuntu:

sudo adduser USERNAME

Upon running this command, you will be asked for a few details. Once that information is provided correctly, a new user 'USERNAME' will be created.

And, to delete an existing user from Ubuntu, use this command:

sudo deluser USERNAME

After this command exits successfully, the user will be removed from the system. Their home directory will be untouched.

The standard Linux commands for adding and removing users are useradd and userdel respectively. However, the adduser and deluser commands in Ubuntu are more user-friendly. Read more on the difference between useradd and adduser commands.

Add a new user in Ubuntu

Adding a new user to an Ubuntu system can be easily achieved by the adduser command. It is actually a Perl script that uses the standard useradd command. It creates a user with a password and home directory by default.

The syntax for the adduser command is as follows:

sudo adduser USERNAME

As you can see, you need to be a sudoer or root to run this command.

When the adduser command is executed, the following things are done automatically:

  1. The newly created user will be given a unique UID (user ID)
  2. A new group will be created with the same name as the user
  3. The new group will be given a unique GID (group id)
  4. The user will be added to the new group
  5. An empty home directory for the user will be created
  6. Contents of /etc/skel will be copied to the user's home directory
  7. Additional information, if provided will be recorded

The contents of /etc/skel include the following files:

  • .bash_logout
  • .bashrc
  • .profile

Enough talk. Let us try creating a new user called 'pratham'.

$ sudo adduser pratham Adding user `pratham' ... Adding new group `pratham' (1001) ... Adding new user `pratham' (1001) with group `pratham' ... Creating home directory `/home/pratham' ... Copying files from `/etc/skel' ... New password: Retype new password: passwd: password updated successfully Changing the user information for pratham Enter the new value, or press ENTER for the default Full Name []: Pratham Patel Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] y

Once the details are provided, a new user will be created. Did you notice that I did not provide details like the room number, work phone, etc and the user was still created? Except for the user's password, everything else is optional.

The user password can be changed later as well. To check if the user was created/exists, you can look for their username in the /etc/passwd file like so:

$ grep pratham /etc/passwd pratham:x:1001:1001:,,,:/home/pratham:/bin/bash

Since the output contains the username I was looking for, the user was created.

Get started on DigitalOcean with a $100, 60-day credit for new users.

Deleting an existing user in Ubuntu

You can use deluser command to delete users in Ubuntu command line. It is also a Perl script and uses the userdel command underneath.

Below is the syntax for the deluser command:

sudo deluser USERNAME

Running this command does the following things:

  1. Delete the specified user
  2. If an empty group exists with the same as the user, delete it as well

The user's home directory is untouched.

Let us try deleting the user that was previously created.

$ sudo deluser pratham Removing user `pratham' ... Warning: group `pratham' has no more members. Done.

Now, if you check the /etc/passwd file, you will notice that the user does not exist.

$ grep pratham /etc/passwd

Additionally, if you wish to delete their home directory, use the --remove-home option.

$ sudo deluser --remove-home pratham Looking for files to backup/remove ... Removing files ... Removing user `pratham' ... Warning: group `pratham' has no more members. Done.

And with that, my user's home directory was removed, along with the user.

The deluser command has a few more useful options:

Option Description
--remove-home remove the users home directory and mail spool
--remove-all-files remove all files owned by user
--backup backup files (in current directory) before removing
--backup-to target directory for the backups
--system only remove if system user

More on it some other day.

Conclusion

Both adduser and deluser are not standard Linux commands. They are scripts that use the actual useradd and userdel commands underneath. However, creating as well as deleting users with the scripts is much easier with adduser and deluser.

If you use useradd, by default, it doesn't ask to create a password or home directory for the user. These are additional pain and the adduser takes good care of it.

Next, you may want to learn about changing users in Ubuntu.

I hope you like this tutorial on adding and removing users from the Ubuntu system in the command line.

Improve Article

Save Article

Like Article

userdel command in Linux system is used to delete a user account and related files. This command basically modifies the system account files, deleting all the entries which refer to the username LOGIN. It is a low-level utility for removing the users.

Syntax:

userdel [options] LOGIN

Options:

  • userdel -f: This option forces the removal of the specified user account. It doesn’t matter that the user is still logged in. It also forces the userdel to remove the user’s home directory and mail spool, even if another user is using the same home directory or even if the mail spool is not owned by the specified user.

    Example:

    sudo userdel -f neuser

    What option can be added to the Userdel command to remove the home directory of the target user?

  • userdel -r: Whenever we are deleting a user using this option then the files in the user’s home directory will be removed along with the home directory itself and the user’s mail spool. All the files located in other file systems will have to be searched for and deleted manually.

    Example:

    sudo userdel -r newuser2

    What option can be added to the Userdel command to remove the home directory of the target user?

  • userdel -h : This option display help message and exit.

    Example:

    userdel -h

    What option can be added to the Userdel command to remove the home directory of the target user?

  • userdel -R: This option apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.

    Example:

    sudo userdel -R newuser2

    What option can be added to the Userdel command to remove the home directory of the target user?

  • userdel -Z : This option remove any SELinux(Security-Enhanced Linux) user mapping for the user’s login.

    Example:

    sudo userdel -Z newuser2

    What option can be added to the Userdel command to remove the home directory of the target user?

  • userdel command with help option: The userdel command throws an error if no options, filename or arguments are passed. So, when we use the -h option, it gives the general syntax along with the various options that can be used with the userdel command.

    Example:

    What option can be added to the Userdel command to remove the home directory of the target user?

userdel - delete a user account and related files

userdel [options] LOGIN

The userdel command modifies the system account files, deleting all entries that refer to the user name LOGIN. The named user must exist.

The options which apply to the userdel command are:

-f, --force

This option forces the removal of the user account, even if the user is still logged in. It also forces userdel to remove the user's home directory and mail spool, even if another user uses the same home directory or if the mail spool is not owned by the specified user. If USERGROUPS_ENAB is defined to yes in /etc/login.defs and if a group exists with the same name as the deleted user, then this group will be removed, even if it is still the primary group of another user.

Note: This option is dangerous and may leave your system in an inconsistent state.

-h, --help

Display help message and exit.

-r, --remove

Files in the user's home directory will be removed along with the home directory itself and the user's mail spool. Files located in other file systems will have to be searched for and deleted manually.

The mail spool is defined by the MAIL_DIR variable in the login.defs file.

-R, --root CHROOT_DIR

Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory. Only absolute paths are supported.

-P, --prefix PREFIX_DIR

Apply changes in the PREFIX_DIR directory and use the configuration files from the PREFIX_DIR directory. This option does not chroot and is intended for preparing a cross-compilation target. Some limitations: NIS and LDAP users/groups are not verified. PAM authentication is using the host files. No SELINUX support.

-Z, --selinux-user

Remove any SELinux user mapping for the user's login.

The following configuration variables in /etc/login.defs change the behavior of this tool:

MAIL_DIR (string)

The mail spool directory. This is needed to manipulate the mailbox when its corresponding user account is modified or deleted. If not specified, a compile-time default is used. The parameter CREATE_MAIL_SPOOL in /etc/default/useradd determines whether the mail spool should be created.

MAIL_FILE (string)

Defines the location of the users mail spool files relatively to their home directory.

The MAIL_DIR and MAIL_FILE variables are used by useradd, usermod, and userdel to create, move, or delete the user's mail spool.

MAX_MEMBERS_PER_GROUP (number)

Maximum members per group entry. When the maximum is reached, a new group entry (line) is started in /etc/group (with the same name, same password, and same GID).

The default value is 0, meaning that there are no limits in the number of members in a group.

This feature (split group) permits to limit the length of lines in the group file. This is useful to make sure that lines for NIS groups are not larger than 1024 characters.

If you need to enforce such limit, you can use 25.

Note: split groups may not be supported by all tools (even in the Shadow toolsuite). You should not use this variable unless you really need it.

USERDEL_CMD (string)

If defined, this command is run when removing a user. It should remove any at/cron/print jobs etc. owned by the user to be removed (passed as the first argument).

The return code of the script is not taken into account.

Here is an example script, which removes the user's cron, at and print jobs:

#! /bin/sh # Check for the required argument. if [ $# != 1 ]; then echo "Usage: $0 username" exit 1 fi # Remove cron jobs. crontab -r -u $1 # Remove at jobs. # Note that it will remove any jobs owned by the same UID, # even if it was shared by a different username. AT_SPOOL_DIR=/var/spool/cron/atjobs find $AT_SPOOL_DIR -name "[^.]*" -type f -user $1 -delete \; # Remove print jobs. lprm $1 # All done. exit 0

USERGROUPS_ENAB (boolean)

If set to yes, userdel will remove the user's group if it contains no more members, and useradd will create by default a group with the name of the user.

/etc/group

Group account information.

/etc/login.defs

Shadow password suite configuration.

/etc/passwd

User account information.

/etc/shadow

Secure user account information.

/etc/shadow-maint/userdel-pre.d/*, /etc/shadow-maint/userdel-post.d/*

Run-part files to execute during user deletion. The environment variable ACTION will be populated with userdel and SUBJECT with the username. userdel-pre.d will be executed prior to any user deletion. userdel-post.d will execute after user deletion. If a script exits non-zero then execution will terminate.

/etc/subgid

Per user subordinate group IDs.

/etc/subuid

Per user subordinate user IDs.

The userdel command exits with the following values:

0

1

can't update password file

2

6

specified user doesn't exist

8

10

12

can't remove home directory

userdel will not allow you to remove an account if there are running processes which belong to this account. In that case, you may have to kill those processes or lock the user's password or account and remove the account later. The -f option can force the deletion of this account.

You should manually check all file systems to ensure that no files remain owned by this user.

You may not remove any NIS attributes on a NIS client. This must be performed on the NIS server.

If USERGROUPS_ENAB is defined to yes in /etc/login.defs, userdel will delete the group with the same name as the user. To avoid inconsistencies in the passwd and group databases, userdel will check that this group is not used as a primary group for another user, and will just warn without deleting the group otherwise. The -f option can force the deletion of this group.

chfn(1), chsh(1), passwd(1), login.defs(5), gpasswd(8), groupadd(8), groupdel(8), groupmod(8), subgid(5), subuid(5), useradd(8), usermod(8).