配置文件

passwd文件

group文件

组群和用户管理

仅root可以执行的命令

功能 针对组群 针对用户
创建 groupadd useradd
删除 groupdel userdel
修改 groupmod usermod

其他命令(所有用户都可以执行)

功能 命令
修改用户密码 passwd
查看用户信息 id
切换用户 su
以另一个用户的身份执行命令 sudo

常用命令详解和示例

创建组:选项和参数(groupadd -h)

Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix
      --extrausers              Use the extra users database

创建组:添加一个名为classmates的组

加一个叫classmates的组

groupadd classmates

用getent看看classmates组加成功了么有,要是grep查不到就不算成功

getent group | grep classmates

直接在系统配置文件里看看有没有classmates组也是可以的

grep classmates /etc/group

删除组:选项和参数(groupdel -h)

Usage: groupdel [options] GROUP

Options:
  -h, --help                    display this help message and exit
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -f, --force                   delete group even if it is the primary group of a user
      --extrausers              Use the extra users database

创建组:删除一个名为classmates的组

# 先不做了吧
groupdel classmates

创建用户:选项和参数(useradd -h)

Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
      --badnames                do not check for bad names
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
      --btrfs-subvolume-home    use BTRFS subvolume for home directory
  -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
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -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
      --extrausers              Use the extra users database
  # 注意:-p选项后面跟的是加密的密文,并非登录时输的密码

创建用户:常用参数和选项

-d, --home-dir HOME_DIR       home directory of the new account
-m, --create-home             create the user's home directory
-g, --gid GROUP               name or ID of the primary group of the new
                                    account
-s, --shell SHELL             login shell of the new account

创建用户:示例

在classmates组加一个叫做bingdwendwen的用户

useradd -m -g classmates bingdwendwen
useradd -m -g classmates -s /bin/bash bingdwendwen

删除用户:选项和参数(userdel -h)

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
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
      --extrausers              Use the extra users database
  -Z, --selinux-user            remove any SELinux user mapping for the user

删除用户:示例

删用户顺便把主目录一起删了

userdel -r bingdwendwen 

用id命令看看还在不在,屏幕上看到“no such user”就算没了

id bingdwendwen 

修改用户属性:选项和参数(usermod -h)

Usage: usermod [options] LOGIN

Options:
  -b, --badnames                allow bad names
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                the user from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
  -V, --del-subuids FIRST-LAST  remove range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

修改用户属性:示例

把bingdwendwen的默认shell改成bash

usermod -s /bin/bash bingdwendwen

修改密码

给bingdwendwen改个密码。如果是root来设置,那就输两遍新密码。如果是墩子自己改密码,就要输一下原来的旧密码,再输两遍新密码。

passwd bingdwendwen

批量添加用户

加了冰墩墩,也别忘了雪容融,还有南京砳砳,哈哈哈哈。写个for循环吧,因为不好passwd,这次换chpasswd来批量设置初始密码。这个命令的用法是:echo "user:password" | chpasswd

for name in shueyrhonrhon nanjinglele
do
    echo "$name is pending"
    useradd -m -g classmates -s /bin/bash $name
    echo "$name:beijing2022" | chpasswd
done