0%

Linux 之 killall 命令

.. _linux-beginner-killall:

.. note::
及时当勉励,岁月不待人。
陶渊明《杂诗·人生无根蒂》

命令概述

在Linux系统中,有许多命令可用于进程管理和控制。

其中一个常用的命令是killall,它允许用户通过进程名字来终止运行中的进程。

官方定义为:

killall – kill processes by name

killall命令用于向操作系统发送信号以终止指定进程。与kill命令不同,killall根据进程名字而不是进程ID来选择要终止的进程。这对于同时终止多个同名进程非常有用。

超级管理员可以kill掉任何进程。

基本语法

killall命令的基本语法如下:

1
$ killall [选项] 进程名

可以使用以下选项对killall命令进行调整:

  • -i:交互式模式,要求用户确认终止每个进程。
  • -e:精确匹配进程名,不匹配进程名的任何子串。
  • -s:指定要发送的信号类型,如-s HUP
  • -v:显示详细的终止进程的输出。

使用示例

终止单个进程

要终止单个进程,可以使用以下命令:

1
$ killall 进程名

比如:

1
$ killall firefox

这将终止所有名为firefox的进程。

终止多个进程

要同时终止多个同名进程,可以使用以下命令:

1
$ killall -r 进程名

示例:

1
$ killall -r chrome

这将终止所有以chrome为名的进程,包括chromechromium等等。

交互式模式

使用-i选项可以在终止每个进程之前要求用户确认。示例:

1
$ killall -i firefox

在执行此命令时,系统将逐个显示要终止的进程,并要求用户确认是否继续,这个对于不确定是否一定中止的优点用哟。

指定信号类型

可以使用-s选项来指定要发送的信号类型。示例:

1
$ killall -s HUP nginx

这将向所有名为nginx的进程发送HUP信号,以重新加载配置。

综上

killall命令是一个强大的进程管理工具,可帮助用户终止指定名称的进程。它简化了终止多个同名进程的操作,并提供了一些有用的选项,如交互式模式和指定信号类型。在日常的系统管理和故障排除中,killall是一个重要的工具,

所有可用的信号

使用 kill -l 命令列出所有可用信号。

1
2
$  kill -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS

其中最常用的信号为:

  • 1 (HUP):重新加载进程。
  • 9 (KILL):杀死一个进程。
  • 15 (TERM):正常停止一个进程。

几个实例

杀死进程

1
$ kill 12345

强制杀死进程

1
2
3
$ kill -KILL 123456
# 或者
$ kill -9 123456

那么如何kill某个用户的所有进程呢,比如用户为user,可以通过下面的命令执行:

1
2
3
$ kill -9 $(ps -ef | grep user) 
# 或者
$ kill -u user

SYNOPSIS
killall [-delmsvqz] [-help] [-I] [-u user] [-t tty] [-c procname] [-SIGNAL] [procname …]

 The options are as follows:

 -d                 Be more verbose about what will be done, but do not send any signal.  The total number of user processes and the real user ID is shown.  A list of the processes that
                    will be sent the signal will be printed, or a message indicating that no matching processes have been found.

 -e                 Use the effective user ID instead of the (default) real user ID for matching processes specified with the -u option.

 -help              Give a help on the command usage and exit.

 -I                 Request confirmation before attempting to signal each process.

 -l                 List the names of the available signals and exit, like in kill(1).

 -m                 Match the argument procname as a (case sensitive) regular expression against the names of processes found.  CAUTION!  This is dangerous, a single dot will match any
                    process running under the real UID of the caller.

 -v                 Be verbose about what will be done.

 -s                 Same as -v, but do not send any signal.

 -SIGNAL            Send a different signal instead of the default TERM.  The signal may be specified either as a name (with or without a leading “SIG”), or numerically.

 -u user            Limit potentially matching processes to those belonging to the specified user.

 -t tty             Limit potentially matching processes to those running on the specified tty.

 -c procname        Limit potentially matching processes to those matching the specified procname.

 -q                 Suppress error message if no processes are matched.

 -z                 Do not skip zombies.  This should not have any effect except to print a few error messages if there are zombie processes that match the specified pattern.

ALL PROCESSES
Sending a signal to all processes with the given UID is already supported by kill(1). So use kill(1) for this job (e.g. “kill -TERM -1” or as root “echo kill -TERM -1 | su -m
”).

IMPLEMENTATION NOTES
This FreeBSD implementation of killall has completely different semantics as compared to the traditional UNIX System V behavior of killall. The latter will kill all processes that the
current user is able to kill, and is intended to be used by the system shutdown process only.

EXIT STATUS
The killall utility exits 0 if some processes have been found and signalled successfully. Otherwise, a status of 1 will be returned.

EXAMPLES
Send SIGTERM to all firefox processes:

       killall firefox

 Send SIGTERM to firefox processes belonging to USER:

       killall -u ${USER} firefox

 Stop all firefox processes:

       killall -SIGSTOP firefox

 Resume firefox processes:

       killall -SIGCONT firefox

 Show what would be done to firefox processes, but do not actually signal them:

       killall -s firefox

 Send SIGTERM to all processes matching provided pattern (like vim and vimdiff):

       killall -m 'vim*'

DIAGNOSTICS
Diagnostic messages will only be printed if the -d flag is used.

SEE ALSO
kill(1), pkill(1), sysctl(3)

i++和++i的区别

i++:对i增加1,但返回的是原来的未增加的值;

++i:在i存储的值上增加1并向使用它的表达式“返回”新的、增加后的值;

Mark:在c++中应该优先使用++i。

基于Doxygen的C/C++注释原则

#define OST_TIMESTAMP DATE “ “ TIME

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

## 1. 文件头的标注

```c
/*****************************************************************************
* The ATAS Library *
* Copyright (C) 2010 Shaoguang Guo sgguo@shao.ac.cn *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 3 as *
* published by the Free Software Foundation. *
* *
* You should have received a copy of the GNU General Public License *
* along with SHAO. If not, see <http://www.gnu.org/licenses/>. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
* @file Example.h *
* @brief brief introduction *
* Details. *
* *
* @author Shaoguang Guo *
* @email sgguo@shao.ac.cn *
* @version 0.1(version) *
* @date 2010.07.16 *
* @license GNU General Public License (GPL) *
* *
*----------------------------------------------------------------------------*
* Remark : Description *
*----------------------------------------------------------------------------*
* Change History : < can omit if using VCS > *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2010/07/16 | 0.1 | Shaoguang Guo | Init the repo *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/

2.命名空间

1
2
3
4
5
6
7
/**
* @brief 命名空间的简单概述 \n(换行)
* 命名空间的详细概述
*/
namespace OST
{
}

3. 类、结构、枚举标注

1
2
3
4
5
6
7
/**
* @brief 类的简单概述 \n(换行)
* 类的详细概述
*/
class Example
{
};

枚举类型定义、结构体类型定义注释风格类似

1
2
3
4
5
6
7
8
9
/**
* @brief 简要说明文字
*/
typedef struct 结构体名字
{
成员1, /*!< 简要说明文字 */ or ///<说明, /**<说明 */ 如果不加<,则会认为是成员2的注释
成员2, /*!< 简要说明文字 */ or ///<说明, /**<说明 */
成员3, /*!< 简要说明文字 */ or ///<说明, /**<说明 */
}结构体别名;

4. 函数注释原则

1
2
3
4
5
6
7
8
9
/**
* @brief 函数简要说明-测试函数
* @param index 参数1
* @param t 参数2 @see CTest
*
* @return 返回说明
* -<em>false</em> fail
* -<em>true</em> succeed
*/
bool Test(int index, const CTest& t);

note:指定函数注意项事或重要的注解指令操作符
note格式如下:
        @note 简要说明

retval:指定函数返回值说明指令操作符。(注:更前面的return有点不同.这里是返回值说明)
retval格式如下:
        @retval 返回值 简要说明

pre:指定函数前置条件指令操作符
pre格式如下:
        @pre 简要说明

par:指定扩展性说明指令操作符讲。(它一般跟code、endcode一起使用 )
par格式如下:
      @par 扩展名字

code、endcode:指定
code、endcode格式如下:
        @code
            简要说明(内容)
        @endcode

see:指定参考信息。
see格式如下:
        @see 简要参考内容

deprecated:指定函数过时指令操作符。
deprecated格式如下:
      @deprecated 简要说明 

  调试Bug说明
    解决的bug说明,@bug
  警告说明 (warning)
    定义一些关于这个函数必须知道的事情,@warning
  备注说明 (remarks)
    定义一些关于这个函数的备注信息,@remarks
  将要完成的工作 (todo)
    说明哪些事情将在不久以后完成,@todo
  使用例子说明 (example)
    例子说明,@example example.cpp

5. 变量注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /// 简述
/** 详细描述. */
或者
//! 简述
//! 详细描述
//! 从这里开始
int m_variable_1; ///< 成员变量m_variable_1说明
int m_variable_2; ///< 成员变量m_variable_1说明

/**
* @brief 成员变量m_c简要说明
*
* 成员变量m_variable_3的详细说明,这里可以对变量进行
* 详细的说明和描述,具体方法和函数的标注是一样的
*/
bool m_variable_3;

如果变量需要详细说明的可已按照m_varibale_3的写法写,注意,m_variable_2和m_variable_3之间一定需要空行,否则会导致m_variable_2的简述消失

6. 模块标注

模块定义格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @defgroup 模块名 页的标题名 (模块名只能英文,这个可以随便取.在一个源文件里不能相同)
* @{ (跟c语言{一样起作用域功能)
*/
… 定义的内容 …
/** @} */

例:
/**
* @defgroup HenryWen Example.cpp
* @{
*/
… 定义的内容 …
/** @} */

7. 分组标注

分组定义格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @name 分组说明文字
* @{
*/
… 定义的内容 …
/** @} */

例:
/**
* @name PI常量
* @{
*/
#define PI 3.1415926737
/** @} */

/**
* @name 数组固定长度常量
* @{
*/
const int g_ARRAY_MAX = 1024;
/** @} */

sizeof和strlen的区别

sizeof函数返回的是变量声明后所占的内存数,不是实际长度;

sizeof是一个编译时执行的运算符,所以它不会导致额外的运行时开销。

strlen函数求的是字符串的实际长度;

不要告诉别人的passwd

passwd用于创建或者更新用户密码,是管理员必备的命令之一。

这个命令最终的实现是通过调用Linux-PAM 和Libuser API来实现的。

官方的定义为:

passwd - update user’s authentication tokens

使用的方法为:

1
$ passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username]

其中很常用的options为:

  • -S, --status:显示密码的状态信息
  • -d, --delete:删除用户密码,此时该用户将处于无密码状态

不太常用的options为:

  • --stdin:可以通过标准输入,亦可以为一个pipe
  • -l, --lock:锁定账号,不过也不是完全锁定,因为用户可以通过ssh key来继续访问
  • -u, --unlock:与上面的-l选项相反,属于解锁用户
  • -w, --warning DAYS:口令到期前通知用户,具备password lifetime的才支持

修改或更新密码

这个是最常用的用法,用于设置或者修改更新用户密码

1
2
3
4
5
$ sudo passwd user  		#设置用户user的密码
Enter new UNIX password: #输入新密码,输入的密码不显示
Retype new UNIX password: #再次输入确认密码
passwd: password updated successfully
# 此时设置成功

删除用户密码

1
2
$ sudo passwd -d user 
passwd: password expiry information changed.

此时用户处于无密码的状态,很类似最近说的,没有密码就是最安全的密码。

查看密码的状态

1
2
3
$ sudo passwd -S user
[sudo] password for oper:
user PS 2013-02-11 0 99999 7 -1 (Password set, SHA512 crypt.)

说到密码,有两个比较重要的原则

  1. 保护好你的密码,不写下来而是记在脑海里,定时修改;
  2. 选择一个很难猜的密码,而不是最容易被攻破的top密码;

linux中创建或更新用户密码

passwd用于创建或者更新用户密码,是管理员必备的命令之一。

这个命令最终的实现是通过调用Linux-PAM 和Libuser API来实现的。

官方的定义为:

passwd - update user’s authentication tokens

使用的方法为:

1
$ passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username]

其中很常用的options为:

  • -S, --status:显示密码的状态信息
  • -d, --delete:删除用户密码,此时该用户将处于无密码状态

不太常用的options为:

  • --stdin:可以通过标准输入,亦可以为一个pipe
  • -l, --lock:锁定账号,不过也不是完全锁定,因为用户可以通过ssh key来继续访问
  • -u, --unlock:与上面的-l选项相反,属于解锁用户
  • -w, --warning DAYS:口令到期前通知用户,具备password lifetime的才支持

修改或更新密码

这个是最常用的用法,用于设置或者修改更新用户密码

1
2
3
4
5
$ sudo passwd user  		#设置用户user的密码
Enter new UNIX password: #输入新密码,输入的密码不显示
Retype new UNIX password: #再次输入确认密码
passwd: password updated successfully
# 此时设置成功

删除用户密码

1
2
$ sudo passwd -d user 
passwd: password expiry information changed.

此时用户处于无密码的状态,很类似最近说的,没有密码就是最安全的密码。

查看密码的状态

1
2
3
$ sudo passwd -S user
[sudo] password for oper:
user PS 2013-02-11 0 99999 7 -1 (Password set, SHA512 crypt.)

说到密码,有两个比较重要的原则

  1. 保护好你的密码,不写下来而是记在脑海里,定时修改;
  2. 选择一个很难猜的密码,而不是最容易被攻破的top密码;

linux中创建新用户useradd

useradd用于创建或者更新用户账号信息,是管理员必备的命令之一。

官方的定义为:

useradd - create a new user or update default new user information

使用的方法为:

1
2
3
4
5
$ useradd [options] LOGIN

$ useradd -D

$ useradd -D [options]

在使用 -D 选项的时候,useradd 命令将使用系统默认、用户命令行指定的参数创建一个新的用户账户。依赖于命令行选项,useradd命令会更新系统文件或者创建用户的home目录并拷贝初始文件,这个除非相当专业,慎用

默认情况下,useradd会创建一个同名的group。

常用的一些选项为:

  • -c, --comment COMMENT :备注,通常会报错在passwd的备注栏中,一般为用户的全名。
  • -d, --home-dir HOME_DIR:指定用户登陆时候的HOME目录
  • -e, --expiredate EXPIRE_DATE:用户账户被禁用的日期,格式为: YYYY-MM-DD。如果不指定,将使用 /etc/default/useradd的值,或者默认取空不过期
  • -s, --shell SHELL:指定登陆后使用的shell,对于不同于默认设定的shell比较有用

默认添加用户

1
2
3
4
$ sudo useradd username

$ id username
uid=1001(username) gid=1001(username) groups=1001(username)

正常情况下,创建用户user,会自动在/home目录创建,通过id命令可以看到有同名的group也创建了。

加上备注

1
$ sudo useradd username -c "USER NAME"

通过这个参数可以设置用户的备注名或者昵称,可以在/etc/passwd中看到,这个对于用户管理而言很方便,而GUI登陆来说比较方便,会显示备注名。

设定登陆的目录

默认情况下创建的目录位于/home ,但是如果希望更改到,比如/home1,那么此时使用-d参数即可,如下:

1
$ sudo useradd  -d /home1/ username

更改默认的SHELL

有些用户可能对csh情有独钟,那么此时可以使用-s来更改,如下:

1
$ sudo useradd -s /usr/bin/csh username

目前默认均为bash。

设定失效日期

这个选项通常对于临时账户很有效,比如来了一个实习生,实习一个月就离开,此时2013-03-07,那么一个月以后失效的命令为:

1
$ sudo useradd username -e 2013-04-07

那么一个月以后,该账户将被禁用登陆。

linux中创建新用户useradd

useradd用于创建或者更新用户账号信息,是管理员必备的命令之一。

官方的定义为:

useradd - create a new user or update default new user information

使用的方法为:

1
2
3
4
5
$ useradd [options] LOGIN

$ useradd -D

$ useradd -D [options]

在使用 -D 选项的时候,useradd 命令将使用系统默认、用户命令行指定的参数创建一个新的用户账户。依赖于命令行选项,useradd命令会更新系统文件或者创建用户的home目录并拷贝初始文件,这个除非相当专业,慎用

默认情况下,useradd会创建一个同名的group。

常用的一些选项为:

  • -c, --comment COMMENT :备注,通常会报错在passwd的备注栏中,一般为用户的全名。
  • -d, --home-dir HOME_DIR:指定用户登陆时候的HOME目录
  • -e, --expiredate EXPIRE_DATE:用户账户被禁用的日期,格式为: YYYY-MM-DD。如果不指定,将使用 /etc/default/useradd的值,或者默认取空不过期
  • -s, --shell SHELL:指定登陆后使用的shell,对于不同于默认设定的shell比较有用

默认添加用户

1
2
3
4
$ sudo useradd username

$ id username
uid=1001(username) gid=1001(username) groups=1001(username)

正常情况下,创建用户user,会自动在/home目录创建,通过id命令可以看到有同名的group也创建了。

加上备注

1
$ sudo useradd username -c "USER NAME"

通过这个参数可以设置用户的备注名或者昵称,可以在/etc/passwd中看到,这个对于用户管理而言很方便,而GUI登陆来说比较方便,会显示备注名。

设定登陆的目录

默认情况下创建的目录位于/home ,但是如果希望更改到,比如/home1,那么此时使用-d参数即可,如下:

1
$ sudo useradd  -d /home1/ username

更改默认的SHELL

有些用户可能对csh情有独钟,那么此时可以使用-s来更改,如下:

1
$ sudo useradd -s /usr/bin/csh username

目前默认均为bash。

设定失效日期

这个选项通常对于临时账户很有效,比如来了一个实习生,实习一个月就离开,此时2013-03-07,那么一个月以后失效的命令为:

1
$ sudo useradd username -e 2013-04-07

那么一个月以后,该账户将被禁用登陆。

useradd 用户名

  • -u  指定用户uid
  • -g  指定用户所属主组
  • -G  指定用户所属附属组
  1. 使用useradd时,如果后面不添加任何参数选项,例如: #sudo useradd test创建出来的用户将是默认“三无”用户:一无Home Directory,二无密码,三无系统Shell。
  2. 使用adduser时,创建用户的过程更像是一种人机对话,系统会提示你输入各种信息,然后会根据这些信息帮你创建新用户。
  3. 所以,adduser更适合初级使用者,因为不用去记那些繁琐的参数选项,只要跟着系统的提示一步一步进行下去就行,缺点就是整个创建过程比较复杂而漫长;而useradd比较适合有些高阶经验的使用者,往往一行命令加参数就能解决很多问题,所以创建起来十分方便。

例1:

1
# useradd -d /usr/leo -m leo

此命令创建了一个用户leo -d和-m选项用来为登录名leo产生一个主目录/usr/leo(/usr为默认的用户主目录所在的父目录)。

例2:

1
# useradd -d /home/leo -s /usr/bin/bash -g leo -G admin,root leo

此命令新建了一个用户leo/bin/sh,他属于group用户组,同时又属于admin和root用户组,其中group用户组是其主组。
这里可能新建组:groupadd group 及 groupadd admin
增加用户账号就是在/etc/passwd文件中为新用户增加一条记录,同时更新其他系统文件,如/etc/shadow,/etc/group等。
Linux提供了集成的系统管理工具userconf,他能用来对用户账号进行统一管理。

注: 用户帐户本身在 /etc/passwd 中定义。Linux 系统包含一个 /etc/passwd 的同伴文件,叫做 /etc/shadow。该文件不像 /etc/passwd,只有对于 root 用户来说是可读的,并且包含加密的密码信息

OPTIONS
The options which apply to the useradd command are:

   -b, --base-dir BASE_DIR
       The default base directory for the system if -d HOME_DIR is not specified.  BASE_DIR is concatenated with the account name to define
       the home directory. If the -m option is not used, BASE_DIR must exist.

       If this option is not specified, useradd will use the base directory specified by the HOME variable in /etc/default/useradd, or
       /home by default.
       
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes






-D, –defaults
See below, the subsection “Changing the default values”.



-f, –inactive INACTIVE
The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as
the password has expired, and a value of -1 disables the feature.

       If not specified, useradd will use the default inactivity period specified by the INACTIVE variable in /etc/default/useradd, or -1
       by default.

   -g, --gid GROUP
       The group name or number of the user's initial login group. The group name must exist. A group number must refer to an already       existing group.

       If not specified, the behavior of useradd will depend on the USERGROUPS_ENAB variable in /etc/login.defs. If this variable is set to
       yes (or -U/--user-group is specified on the command line), a group will be created for the user, with the same name as her
       loginname. If the variable is set to no (or -N/--no-user-group is specified on the command line), 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.

   -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
       A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no
       intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for
       the user to belong only to the initial group.

   -k, --skel SKEL_DIR
       The skeleton directory, which contains files and directories to be copied in the user's home directory, when the home directory is        created by useradd.

       This option is only valid if the -m (or --create-home) option is specified.

       If this option is not set, the skeleton directory is defined by the SKEL variable in /etc/default/useradd or, by default, /etc/skel.

       If possible, the ACLs and extended attributes are copied.

   -K, --key KEY=VALUE
       Overrides /etc/login.defs defaults (UID_MIN, UID_MAX, UMASK, PASS_MAX_DAYS and others).

       Example: -K PASS_MAX_DAYS=-1 can be used when creating system account to turn off password aging, even though system account has no
       password at all. Multiple -K options can be specified, e.g.: -K UID_MIN=100  -K UID_MAX=499

   -l, --no-log-init
       Do not add the user to the lastlog and faillog databases.

       By default, the user's entries in the lastlog and faillog databases are reset to avoid reusing the entry from a previously deleted       user.

   -m, --create-home
       Create the user's home directory if it does not exist. The files and directories contained in the skeleton directory (which can be        defined with the -k option) will be copied to the home directory.

       By default, if this option is not specified and CREATE_HOME is not enabled, no home directories are created.

       The directory where the user's home directory is created must exist and have proper SELinux context and permissions. Otherwise the
       user's home directory cannot be created or accessed.

   -M, --no-create-home
       Do no create the user's home directory, even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes.

   -N, --no-user-group
       Do not create a group with the same name as the user, but add the user to the group specified by the -g option or by the GROUP       variable in /etc/default/useradd.

       The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in       /etc/login.defs.

   -o, --non-unique
       Allow the creation of a user account with a duplicate (non-unique) UID.

       This option is only valid in combination with the -u option.

   -p, --password PASSWORD
       The encrypted password, as returned by crypt(3). The default is to disable the password.

       Note: This option is not recommended because the password (or encrypted password) will be visible by users listing the processes.

       You should make sure the password respects the system's password policy.

   -r, --system
       Create a system account.

       System users will be created with no aging information in /etc/shadow, and their numeric identifiers are chosen in the       SYS_UID_MIN-SYS_UID_MAX range, defined in /etc/login.defs, instead of UID_MIN-UID_MAX (and their GID counterparts for the creation       of groups).

       Note that useradd will not create a home directory for such a user, regardless of the default setting in /etc/login.defs       (CREATE_HOME). You have to specify the -m options if you want a home directory for a system account to be created.

   -R, --root CHROOT_DIR
       Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.

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



-u, –uid UID
The numerical value of the user’s ID. This value must be unique, unless the -o option is used. The value must be non-negative. The
default is to use the smallest ID value greater than or equal to UID_MIN and greater than every other user.

       See also the -r option and the UID_MAX description.

   -U, --user-group
       Create a group with the same name as the user, and add the user to this group.

       The default behavior (if the -g, -N, and -U options are not specified) is defined by the USERGROUPS_ENAB variable in
       /etc/login.defs.

   -Z, --selinux-user SEUSER
       The SELinux user for the user's login. The default is to leave this field blank, which causes the system to select the default
       SELinux user.

Changing the default values
When invoked with only the -D option, useradd will display the current default values. When invoked with -D plus other options, useradd will update the default values for the specified options. Valid default-changing options are:

   -b, --base-dir BASE_DIR
       The path prefix for a new user's home directory. The user's name will be affixed to the end of BASE_DIR to form the new user's home
       directory name, if the -d option is not used when creating a new account.

       This option sets the HOME variable in /etc/default/useradd.

   -e, --expiredate EXPIRE_DATE
       The date on which the user account is disabled.

       This option sets the EXPIRE variable in /etc/default/useradd.

   -f, --inactive INACTIVE
       The number of days after a password has expired before the account will be disabled.

       This option sets the INACTIVE variable in /etc/default/useradd.

   -g, --gid GROUP
       The group name or ID for a new user's initial group (when the -N/--no-user-group is used or when the USERGROUPS_ENAB variable is set
       to no in /etc/login.defs). The named group must exist, and a numerical group ID must have an existing entry.

       This option sets the GROUP variable in /etc/default/useradd.

   -s, --shell SHELL
       The name of a new user's login shell.

       This option sets the SHELL variable in /etc/default/useradd.

NOTES
The system administrator is responsible for placing the default user files in the /etc/skel/ directory (or any other skeleton directory specified in /etc/default/useradd or on the command line).

CAVEATS
You may not add a user to a NIS or LDAP group. This must be performed on the corresponding server.

   Similarly, if the username already exists in an external user database such as NIS or LDAP, useradd will deny the user account creation   request.

   Usernames may contain only lower and upper case letters, digits, underscores, or dashes. They can end with a dollar sign. Dashes are not   allowed at the beginning of the username. Fully numeric usernames and usernames . or .. are also disallowed. It is not recommended to   use usernames beginning with . character as their home directories will be hidden in the ls output. In regular expression terms:
   [a-zA-Z0-9_.][a-zA-Z0-9_.-]*[$]?

   Usernames may only be up to 32 characters long.

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

   CREATE_HOME (boolean)
       Indicate if a home directory should be created by default for new users.

       This setting does not apply to system users, and can be overridden on the command line.

   GID_MAX (number), GID_MIN (number)
       Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers.

       The default value for GID_MIN (resp.  GID_MAX) is 1000 (resp. 60000).

   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.

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

   If MAIL_CHECK_ENAB is set to yes, they are also used to define the MAIL environment variable.

   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.

   PASS_MAX_DAYS (number)
       The maximum number of days a password may be used. If the password is older than this, a password change will be forced. If not
       specified, -1 will be assumed (which disables the restriction).

   PASS_MIN_DAYS (number)
       The minimum number of days allowed between password changes. Any password changes attempted sooner than this will be rejected. If
       not specified, -1 will be assumed (which disables the restriction).

   PASS_WARN_AGE (number)
       The number of days warning given before a password expires. A zero means warning is given only upon the day of expiration, a
       negative value means no warning is given. If not specified, no warning will be provided.

   SUB_GID_MIN (number), SUB_GID_MAX (number), SUB_GID_COUNT (number)
       If /etc/subuid exists, the commands useradd and newusers (unless the user already have subordinate group IDs) allocate SUB_GID_COUNT
       unused group IDs from the range SUB_GID_MIN to SUB_GID_MAX for each new user.

       The default values for SUB_GID_MIN, SUB_GID_MAX, SUB_GID_COUNT are respectively 100000, 600100000 and 65536.

   SUB_UID_MIN (number), SUB_UID_MAX (number), SUB_UID_COUNT (number)
       If /etc/subuid exists, the commands useradd and newusers (unless the user already have subordinate user IDs) allocate SUB_UID_COUNT
       unused user IDs from the range SUB_UID_MIN to SUB_UID_MAX for each new user.

       The default values for SUB_UID_MIN, SUB_UID_MAX, SUB_UID_COUNT are respectively 100000, 600100000 and 65536.

   SYS_GID_MAX (number), SYS_GID_MIN (number)
       Range of group IDs used for the creation of system groups by useradd, groupadd, or newusers.

       The default value for SYS_GID_MIN (resp.  SYS_GID_MAX) is 101 (resp.  GID_MIN-1).

   SYS_UID_MAX (number), SYS_UID_MIN (number)
       Range of user IDs used for the creation of system users by useradd or newusers.

       The default value for SYS_UID_MIN (resp.  SYS_UID_MAX) is 101 (resp.  UID_MIN-1).

   UID_MAX (number), UID_MIN (number)
       Range of user IDs used for the creation of regular users by useradd or newusers.

       The default value for UID_MIN (resp.  UID_MAX) is 1000 (resp. 60000).

   UMASK (number)
       The file mode creation mask is initialized to this value. If not specified, the mask will be initialized to 022.

       useradd and newusers use this mask to set the mode of the home directory they create

       It is also used by login to define users' initial umask. Note that this mask can be overridden by the user's GECOS line (if
       QUOTAS_ENAB is set) or by the specification of a limit with the K identifier in limits(5).

   USERGROUPS_ENAB (boolean)
       Enable setting of the umask group bits to be the same as owner bits (examples: 022 -> 002, 077 -> 007) for non-root users, if the
       uid is the same as gid, and username is the same as the primary group name.

       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.

FILES
/etc/passwd
User account information.

   /etc/shadow
       Secure user account information.

   /etc/group
       Group account information.

   /etc/gshadow
       Secure group account information.

   /etc/default/useradd
       Default values for account creation.

   /etc/skel/
       Directory containing default files.

   /etc/subgid
       Per user subordinate group IDs.

   /etc/subuid
       Per user subordinate user IDs.

   /etc/login.defs
       Shadow password suite configuration.

EXIT VALUES
The useradd command exits with the following values:

   0
       success

   1
       can't update password file

   2
       invalid command syntax

   3
       invalid argument to option

   4
       UID already in use (and no -o)

   6
       specified group doesn't exist

   9
       username already in use

   10
       can't update group file

   12
       can't create home directory

   14
       can't update SELinux user mapping

SEE ALSO
chfn(1), chsh(1), passwd(1), crypt(3), groupadd(8), groupdel(8), groupmod(8), login.defs(5), newusers(8), subgid(5),
subuid(5),userdel(8), usermod(8).

linux中删除用户userdel

userdel用于删除用户账号信息,是管理员必备的命令之一。

userdel将删除用户帐号与相关的文件。若不加参数,则仅仅删除用户帐号,账号的目录可能还会存在。

官方的定义为:

userdel - delete a user account and related files

使用的方法为:

1
$ userdel [options] LOGIN

其中LOGIN为将删除的用户名,需要确保其存在,不然会报错。

其中很常用的options为:

  • -r, --remove:删除用户登陆的目录以及目录中所有的文件,还有用户的邮件信息,在其他文件系统的文件可能需要手动删除。
  • -f, --force:这个选项强制删除用户账号,即便该用户仍在登陆。同时还会删除用户的home目录和mail信息。总之很彪悍的一个参数,可能会引起其他问题,慎用慎用,不用不用

默认使用

删除用户账号user,这个选项将把

1
$ sudo userdel username

彻底删除账号信息

1
$ sudo userdel -r username

-r参数将把用户的账号以及默认位于/home/username/的所有文件进行删除,谨慎操作,无法找回,除非确认该账号确实不再使用,并且文件确实不在具备价值。

如何确认是否成功?

userdel命令是有返回信息的,如果需要确认命令的执行情况,如下返回值:

  • 0 成功
  • 1 无法更新password文件
  • 2 无效的命令语法
  • 6 指定的用户不存在
  • 8 用户正在登陆
  • 10 无法更新group文件
  • 12 无法移除home目录

警告:如果一个用户还有程序在运行,userdel是不允许删除该账户的。此时可以通过kill掉改程序,或者使用-f来强制删除。

通常情况下,不要这么做。

linux中删除用户userdel

userdel用于删除用户账号信息,是管理员必备的命令之一。

userdel将删除用户帐号与相关的文件。若不加参数,则仅仅删除用户帐号,账号的目录可能还会存在。

官方的定义为:

userdel - delete a user account and related files

使用的方法为:

1
$ userdel [options] LOGIN

其中LOGIN为将删除的用户名,需要确保其存在,不然会报错。

其中很常用的options为:

  • -r, --remove:删除用户登陆的目录以及目录中所有的文件,还有用户的邮件信息,在其他文件系统的文件可能需要手动删除。
  • -f, --force:这个选项强制删除用户账号,即便该用户仍在登陆。同时还会删除用户的home目录和mail信息。总之很彪悍的一个参数,可能会引起其他问题,慎用慎用,不用不用

默认使用

删除用户账号user,这个选项将把

1
$ sudo userdel username

彻底删除账号信息

1
$ sudo userdel -r username

-r参数将把用户的账号以及默认位于/home/username/的所有文件进行删除,谨慎操作,无法找回,除非确认该账号确实不再使用,并且文件确实不在具备价值。

如何确认是否成功?

userdel命令是有返回信息的,如果需要确认命令的执行情况,如下返回值:

  • 0 成功
  • 1 无法更新password文件
  • 2 无效的命令语法
  • 6 指定的用户不存在
  • 8 用户正在登陆
  • 10 无法更新group文件
  • 12 无法移除home目录

警告:如果一个用户还有程序在运行,userdel是不允许删除该账户的。此时可以通过kill掉改程序,或者使用-f来强制删除。

通常情况下,不要这么做。

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