删除Red hat和CentOS中的用户

删除用户,但不删除用户的主目录和邮件假脱机

userdel username

删除用户及其主目录和邮件假脱机。使用选项-r

userdel -r username

创建用户基本命令

在redhat和CentOS中,使用下面的命令创建一个用户并设置密码

useradd username

passwd username

示例

[root@localhost ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel r on an m

[root@localhost ~]# 
[root@localhost ~]# useradd jack
[root@localhost ~]# 
[root@localhost ~]# grep jack /etc/passwd
jack:x:500:500::/home/jack:/bin/bash
[root@localhost ~]# 
[root@localhost ~]# grep jack /etc/group
jack:x:500:
[root@localhost ~]# 
[root@localhost ~]# id jack
uid=500(jack) gid=500(jack) groups=500(jack)
[root@localhost ~]# 
[root@localhost ~]# ls -ld /home/jack/
drwx------ 2 jack jack 4096 Jul 17 20:35 /home/jack/
[root@localhost ~]#
[root@localhost ~]# grep jack /etc/shadow
jack:!!:15903:0:99999:7:::
[root@localhost ~]#

当我们使用“useradd jack”命令创建用户时,系统会:

  1. 创建了相同的组名,这里新的组名是jack ,jack用户是jack组的成员。

  2. 在/home中创建了名为jack的用户的主目录,可以使用查看 ls -ld/home/jack

  3. 用户jack得到了一个默认的登录shell: /bin/bash,我们可以使用命令 grep jack /etc/passwd查看

  4. 在Red Hat和CentOS中,当创建第一个用户时,uid和gid以500开头(uid=用户id,gid=组id)

查看useradd和userdel的命令帮助

[root@localhost ~]# useradd --help
Usage: useradd [options] LOGIN

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

[root@localhost ~]#
[root@localhost ~]# userdel --help
Usage: userdel [options] LOGIN

Options:
  -f, --force                   force removal of files,
                                even if not owned by user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -Z, --selinux-user            remove SELinux user from SELinux user mapping

[root@localhost ~]#
在Red Hat和CentOS中如何创建和删除用户

在Red Hat和CentOS中创建和删除用户使用的是useradd和userdel命令。

查看useradd命令的默认选项

[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#
[root@localhost ~]# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#
  1. HOME:这是主目录的默认路径前缀。
  2. INACTIVE:密码到期后的天数,直到帐户永久停用为止。值为0将在密码过期后立即禁用帐户,值为-1将禁用此功能。如果如果未指定,useradd将使用/etc/default/useradd中的非活动变量指定的默认非活动期,默认情况下为-1.
  3. EXPIRE:用户账号被禁用的日期。日期以YYYY-MM-DD格式指定
  4. SHELL:用户登录SHELL。
  5. SKEL:SKEL目录内的内容将被复制到用户的主目录中。
  6. CREATE_MAIL_SPOOL:此处默认值为CREATE_MAIL_SPOOL=yes,表示将创建邮件假脱机目录。

我们创建第一个用户时,UID和GID是500,为什么?

因为在/etc/login.defs中定义了最小值:

[root@localhost ~]# grep GID_MIN /etc/login.defs
GID_MIN			  500
[root@localhost ~]# grep UID_MIN /etc/login.defs
UID_MIN			  500
[root@localhost ~]#

如何更改useradd命令的默认值

直接使用命令修改或者编辑配置文件

示例1 更改默认shell

[root@localhost ~]# useradd -D -s /bin/sh
You have new mail in /var/spool/mail/root
[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#

示例2 更改默认主目录

[root@localhost ~]# mkdir /new_home
[root@localhost ~]# useradd -D -b /new_home
[root@localhost ~]# 
[root@localhost ~]# useradd -D
GROUP=100
HOME=/new_home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
[root@localhost ~]#

示例3 编辑/etc/default/useradd 配置文件

vi /etc/default/useradd

# useradd defaults file
GROUP=100
HOME=/new_home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
日期:2019-08-20 17:58:08 来源:oir作者:oir