1. 涉及到的命令

adduser 用户名

useradd 选项 用户名
usermod 选项 用户名
userdel 选项 用户名

passwd 选项 用户名

groupadd 选项 用户组
groupmod 选项 用户组
groupdel 选项 用户组

2.用户账号管理

2.1 新建用户账号

注意新建用户最好用adduser,使用useradd,普通用户不会自动创建home目录,用户首次登录时,虽然能登录,但是还会报错:
Could not chdir to home directory /home/xxx: No such file or directory
/usr/bin/xauth: error in locking authority file /home/xxx/.Xauthority

而且很多命令不能用,如ll

推荐操作总结

addgroup groupname1 #作为主组
addgroup groupname2 #作为从组
adduser username #默认操作,主组是username,shell是/bin/bash
usermod -s /bin/bash -g groupname1 -G groupname2 username

2.1.1 adduser

adduser help文档

adduser [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
[--firstuid ID] [--lastuid ID] [--gecos GECOS] [--ingroup GROUP | --gid ID]
[--disabled-password] [--disabled-login] [--add_extra_groups]
[--encrypt-home] USER
   Add a normal user

adduser --system [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID]
[--gecos GECOS] [--group | --ingroup GROUP | --gid ID] [--disabled-password]
[--disabled-login] [--add_extra_groups] USER
   Add a system user

adduser --group [--gid ID] GROUP
addgroup [--gid ID] GROUP
   Add a user group

addgroup --system [--gid ID] GROUP
   Add a system group

adduser USER GROUP
   Add an existing user to an existing group

general options:
  --quiet | -q      don't give process information to stdout
  --force-badname   allow usernames which do not match the
                    NAME_REGEX[_SYSTEM] configuration variable
  --extrausers      uses extra users as the database
  --help | -h       usage message
  --version | -v    version number and copyright
  --conf | -c FILE  use FILE as configuration file

创建用户

adduser username1

结果如下图,需要输入密码,并再次输入确认,之后的啥Full Name,Room Number啥的可以不填直接enter,enter就是默认值,最好输入y确认就好了
在这里插入图片描述

添加已存在用户到已存在组
注意:这里是groupname是username1的从组

adduser username1 groupname

2.1.2 useradd

useradd help文档

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

最简单,按默认操作创建新用户

useradd username1

设置新用户home目录,默认目录就是/home/username

useradd -d /home/username1 username1

设置新用户登录的shell,shell意思是贝壳,就是说Linux系统内核的壳,用户默认的shell就是/bin/sh,bash是sh的增强版本,sh是bash的缩减版,具体区别参考:sh和bash

useradd -s /bin/bash username1

设置新用户分组,前提是username1之前没创建过。默认分组是用户名自身

useradd -g groupname1 -G groupname2,groupname3 username1

2.2 查看用户、用户组信息

2.2.1 查看当前用户所属组

第一个是当前所在组,其他的是属于的可选的组

groups

2.2.1 查看所有的组

cat /etc/group

2.2.1 查看所有的用户

cat /etc/passwd

/etc/passwd 是一个文本文件,其中包含了登录 Linux 系统所必需的每个用户的信息。它保存用户的有用信息,
包含7个字段:用户名、密码、用户 ID、群组 ID、用户 ID 信息、用户的家目录和 Shell 。

用户名 (magesh): 已创建用户的用户名,字符长度 1 个到 12 个字符。
密码(x):代表加密密码保存在 `/etc/shadow 文件中。
用户 ID(506):代表用户的 ID 号,每个用户都要有一个唯一的 ID 。UID 号为 0 的是为 root 用户保留的,UID 号 1 到 99 是为系统用户保留的,UID 号 100-999 是为系统账户和群组保留的。
群组 ID (507):代表群组的 ID 号,每个群组都要有一个唯一的 GID ,保存在 /etc/group文件中。
用户信息(2g Admin - Magesh M):代表描述字段,可以用来描述用户的信息(LCTT 译注:此处原文疑有误)。
家目录(/home/mageshm):代表用户的家目录。
Shell(/bin/bash):代表用户使用的 shell 类型。

2.3 修改用户账号

usermod help文档

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

2.3.1 修改用户分组

usermod -g groupname1 -G groupname2,groupname3 username1

2.3.2 修改用户名字账号名字

usermod -l newusrname username1

2.3.3 修改用户密码

修改自己账号密码
passwd
修改别人账号密码,需要sudo或者root
sudo passwd username

2.3.4 修改用户home目录

usermod -d /newhomepath username1

2.4 用户组

2.4.1 创建用户组

addgroup groupname
或者
adduser --group groupname
或者
groupadd groupname1

2.4.2 删除用户组

groupdel groupname1

2.4.3 用户组切换

如果一个用户同时属于多个用户组,那么用户可以在用户组之间切换,以便具有其他用户组的权限。
用户可以在登录后,使用命令newgrp切换到其他用户组,这个命令的参数就是目的用户组。例如:

newgrp groupname2

这条命令将当前用户切换到groupname2用户组,前提条件是groupname2用户组确实是该用户的主组或附加组

参考链接:

linux用户和用户组管理

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐