命令行大全

userdel

瑞兹 发表于 2020-12-14 11:34浏览次数:

在类似Unix操作系统,userdel命令删除一个用户帐户和所有相关文件。

查看英文版

目录:

1 userdel 运行系统环境

2 userdel 描述

3 userdel 语法

4 userdel 例子

userdel 运行系统环境

Unix&Linux

userdel 描述

userdel是用于删除用户的低级实用程序。(它不是非常友好的用户。在大多数情况下,建议管理员使用友好的deluser实用程序。)

userdel命令修改系统帐户文件,删除引用该用户名的所有条目LOGIN。命名用户必须存在。

userdel is a low-level utility for removing users. (It is not very user-friendly. In most cases, administrators are advised to use the friendlier deluser utility instead.)

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

查看英文版

查看中文版

userdel 语法

userdel [options] LOGIN

选件

-f--force 即使该用户仍处于登录状态,此选项也将强制删除该用户帐户。即使另一个用户使用相同的主目录或不拥有该邮件假脱机,该选项也强制userdel删除该用户的主目录和邮件假脱机。由指定的用户。如果在/etc/login.defs中将USERGROUPS_ENAB定义为yes,并且存在与删除的用户同名的组,则即使该组仍是另一个用户的主要组,该组也将被删除。注意:此选项很危险,可能会使您的系统处于不一致状态。

-h--help 显示帮助消息,然后退出。
-r,--remove 用户主目录中的文件以及主目录本身和用户的邮件后台处理程序都将被删除。位于其他文件系统中的文件将必须手动搜索和删除。

邮件假脱机由login.defs文件中的MAIL_DIR变量定义。
-R CHROOT_DIR--root CHROOT_DIR 在chroot目录CHROOT_DIR中应用更改,并使用同一目录中的配置文件。
-Z,-- selinux-user 删除任何用于用户登录的SELinux(增强安全性的Linux)用户映射。

组态

/etc/login.defs中的以下配置变量更改了此工具的行为:

Name Type 描述
MAIL_DIR string 邮件假脱机目录。当邮箱的相应用户帐户被修改或删除时,需要用它来操作邮箱。如果未指定,则使用编译时默认值。
MAIL_FILE string 定义用户邮件假脱机文件相对于其主目录的位置。

MAX_MEMBERS

_PER_GROUP

number 每个组条目的最大成员数。当达到最大值时,将在/ etc / group中启动一个新的组条目(行)(具有相同的名称,相同的密码和相同的组ID)。

默认值为0,表示组中的成员数没有限制。

此功能(拆分组)允许限制组文件中的行长。确保NIS组的行不超过1024个字符非常有用。

如果需要强制执行此限制,则可以使用25

注意:并非所有工具(包括Shadow工具套件)都支持拆分组。除非确实需要,否则不应使用此变量。
USERDEL_CMD string 如果定义,则在删除用户时运行此命令。它应该删除要删除的用户所有cron / print作业等(作为第一个参数传递)。

不考虑脚本的返回码。

这是一个示例脚本,该脚本删除了用户的cronat和print作业:

#! /bin/sh# Check for the required argument.if [ $# != 1 ]; then
echo "Usage: $0 username" exit 1fi# 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/atjobsfind $AT_SPOOL_DIR -
name "[^.]*" -type f -user $1 -delete \;# Remove print jobs.lprm 
$1# All done.exit 0

USERGROUPS

_ENAB

boolean 如果设置为“ yes”,则userdel将删除该用户的组(如果该组中不再包含任何成员),并且useradd将默认创建一个带有用户名的组。

请注意,每个useraddusermoduserdel使用MAIL_DIRMAIL_FILE变量来创建,移动或删除用户的邮件假脱机。

档案

/ etc / group 组帐户信息。
/etc/login.defs 影子密码套件配置。
/ etc / passwd 用户帐户信息。
/ etc /shadow 安全的用户帐户信息。

退出状态

userdel将使用以下退出代码之一退出,具体取决于发生的情况:

0 成功。
1 无法更新密码文件。
2 无效的命令语法。
6 指定的用户不存在。
8 由于指定的用户当前登录,无法删除用户。
10 无法更新群组档案。
12 无法删除主目录。

笔记

如果有正在运行的进程属于该帐户,userdel将不允许您删除该帐户。在这种情况下,您可能必须终止这些进程或锁定用户的密码或帐户,然后再删除该帐户。该-f选项可以强制该帐户被删除。

您应该手动检查所有文件系统,以确保该用户没有文件拥有。

您不能在NIS客户端上删除任何NIS属性。这必须在NIS服务器上执行。

如果在/etc/login.defs中将USERGROUPS_ENAB定义为“是” ,则userdel将删除与用户同名的组。为了避免passwd数据库和组数据库中的不一致,userdel将检查该组是否未被用作另一个用户的主要组,并且只会发出警告而不删除该组。该-f选项可以强制该组删除。

userdel [options] LOGIN

Options

-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 a 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 CHROOT_DIR--root CHROOT_DIR Apply changes in the chroot directory CHROOT_DIR and use the configuration files from the same directory.
-Z--selinux-user Remove any SELinux (Security-Enhanced Linux) user mapping for the user's login.

Configuration

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

Name Type Description
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.
MAIL_FILE string Defines the location of the users mail spool files relatively to their home directory.

MAX_MEMBER

S_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 Group ID).

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 (including 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 cronat and print jobs:

#! /bin/sh# Check for the required argument.if [ $# != 1 ]; then 
echo "Usage: $0 username" exit 1fi# 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/atjobsfind $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.

Note that the MAIL_DIR and MAIL_FILE variables are used by each of useraddusermod, and userdel to create, move, or delete the user's mail spool.

Files

/etc/group Group account information.
/etc/login.defs Shadow password suite configuration.
/etc/passwd User account information.
/etc/shadow Secure user account information.

Exit Status

userdel will exit with one of the following exit codes, depending on what occurred:

0 Success.
1 Couldn't update the passwd file.
2 Invalid command syntax.
6 Specified user doesn't exist.
8 Couldn't delete user because the specified user is currently logged in.
10 Couldn't update group file.
12 Couldn't remove home directory.

Notes

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.defsuserdel 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.

查看英文版

查看中文版

userdel 例子

userdel -r username

删除用户username的帐户,并删除该用户的主目录和关联的邮件文件。

userdel -r username

Deletes the account of user username, and removes that user's home directory and associated mail files.

查看英文版

查看中文版

发表评论
评论列表
共0条
点击加载更多