Linux 用户&用户组管理

[TOC]

#

基本概念

  1. 用户(User)
  • Linux 是一个多用户系统,每个用户有唯一的 用户名UID(User ID)
    • root(超级用户,UID=0):拥有系统最高权限。
    • 系统用户:系统运行时需要的用户(如 nobody, daemon),UID 通常小于 1000。
    • 普通用户:一般 UID 从 1000 开始(在现代 Linux 系统中),最大 60000。
  1. 用户组(Group)
  • 每个用户都属于至少一个组(即 主组)。
  • 一个用户还可以加入多个附加组(即 附属组)。
  • 每个组由 组名GID(Group ID) 唯一标识。
  1. 用户与组的关系
  • 每个文件或目录都有 所属用户所属组
  • 权限管理(rwx)基于 所属用户、所属组、其他用户 三类身份来控制。

相关配置文件

Linux 的用户和组信息存储在文本配置文件中:

  1. /etc/passwd
    存储用户基本信息(账号、UID、GID、主目录、登录 shell)。
    cat /etc/passwd查看用户信息,示例:
1
2
root:x:0:0:root:/root:/bin/bash
lsf:x:1000:1000:LSFUser:/home/lsf:/bin/bash

各字段含义:

  • 用户名
  • 密码占位符(x,实际在 /etc/shadow;早期 Linux 系统中直接存储密码哈希,但为提升安全性,现在此字段仅作为 “占位符”,实际密码哈希已迁移至 /etc/shadow,仅 root 可读取。)
  • UID(系统内部识别用户的数字编号,系统不依赖用户名,而是通过 UID 判断用户身份及权限。)
    • 超级用户 UID:固定为 0(仅 root 用户,拥有系统所有权限,任何 UID 为 0 的用户都会被视为超级用户,即使用户名不是 root)。
    • 系统用户 UID:通常为 1-999(用于运行系统服务,如 bin(UID=1)、daemon(UID=2)、www-data(UID=33,Web 服务用户),这类用户无登录权限,家目录也非普通用户目录)。
    • 普通用户 UID:通常从 1000 开始(由管理员创建,用于日常登录和操作,权限受限制)。

使用id username可以查看用户 UID 以及所属用户组。

  • GID(用户初始组(Primary Group) 的数字编号,初始组是用户登录时默认所属的组,其权限会自动应用于用户创建的文件 / 目录,文件的默认组就是用户的初始组。)
  • 用户描述(可选,字段名称源于早期 Unix 系统的历史遗留,无实际技术含义,仅作注释。)
  • 用户主目录(用户登录系统后默认进入的目录,也是用户存储个人文件、配置的专属目录(普通用户对家目录拥有读写执行权限,其他用户通常无权限。)
  • 用户默认登录 Shell(默认启动的命令行解释器,决定用户使用的命令行环境。)
  1. /etc/shadow
    存储用户密码和密码策略(加密形式),只有 root 可读。
1
2
root:*:19769:0:99999:7:::
moon:$6$...:20301:0:99999:7:::

各个字段含义:

  • 用户名(moon)
  • 密码($6$…,存储用户密码的哈希值, 表示使用 SHA-512 算法加密。)
  • 最后一次修改密码的日期(20301,为从1970年1月1日以来的天数,比如在 2025 年 8 月 1 日修改过密码,那么这里就是 20301。)
  • 密码有效期最少天数(0,限制用户 上次修改密码后,至少需间隔多少天才能再次修改密码。为 0 或者空表示没有显示,可以随时修改密码,用于防止用户频繁改密码规避安全策略。)
  • 密码有效期最多的天数(99999,强制用户 上次修改密码后,最多可使用多少天,超过此天数后密码失效,用户必须重新设置密码才能登录。99999 表示密码永不过期,约等于 273 年,是默认配置)
  • 密码修改警告期(7,在 “密码有效期最多天数” 到期前,提前多少天向用户发送 密码即将过期的警告,提醒用户及时修改。)
  • 密码禁用期(NULL,也叫 “密码过期宽限期”,指 “密码有效期最多天数” 到期后,用户仍可登录的 宽限天数;超过宽限天数后,账号会被自动锁定。)
  • 账号过期日期(NULL,指定账号的 绝对失效日期,无论密码是否过期,超过此日期后账号无法登录,常用于临时账号,如实习员工账号。)
  • 保留字段(NULL,预留字段,目前无实际功能,为未来 Linux 系统扩展账号安全策略预留空间。)

以上字段均可以通过命令修改。

  1. /etc/group
    存储组信息。
1
2
3
4
5
6
7
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
sudo:x:27:moon
www-data:x:33:
moon:x:1000:

各个字段含义:

  • 组名
  • 组密码(存储用户组的密码哈希值,用于控制 “非组内用户临时加入该组” 的权限,实际场景中极少使用,因组权限通常通过用户归属管理,x 表示组密码已存储,但实际哈希值保存在 /etc/gshadow文件中;x 仅为占位符,而非真实密码。)
  • 组 ID(系统组 0~999,普通组 1000~)
  • 组内成员(存储属于该组的用户列表,这些用户的 “附加组” 包含此组,主组需看 /etc/passwd 配置。)可以通过usermod -ag <组名> <用户名>向组内添加成员。(如果该字段为空表示该组无 “附加成员”,但可能有用户的 “主组” 是此组,需查看 /etc/passwd 确认。)
  1. /etc/gshadow
    存储组密码和组管理员信息。
1
2
3
4
5
root:*::
daemon:*::
bin:*::
sys:*::
moon:!::
  • 组名(关联用户组身份,与 /etc/group 组名一致,唯一不重复,符合组名命名规范。)
  • 组密码哈希(验证非管理员临时加入组的身份,!/* 表示禁止设密码,空表示无密码。)
  • 组管理员(授权用户管理组成员(无需 root 权限),空表示无组管理员,多用户用逗号分隔。)
  • 组成员(记录组的附加成员列表 空表示无附加成员,多用户用逗号分隔。)

用于&用户组管理

用户管理

  1. 创建用户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 创建用户
useradd -m -s /bin/bash <username>

# 2. 创建带过期时间的用户
useradd -m -e 2023-12-31 -c "Temporary User" <username>

# 3. 创建系统用户 无登录shell
useradd -r -s /sbin/nologin -c "Web Server" apache

# 4. 创建用户并加入指定用户组
useradd -m -g developers -G sudo,devops <username>

# 设置用户登陆密码
passwd <username>
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
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 指定用户基准目录,当不使用 -d 指定指定具体目录时,会创建 BASE_DIR/<username>
new account
--btrfs-subvolume-home use BTRFS subvolume for home directory
-c, --comment COMMENT GECOS field of the new account 设置用户的注释信息(GECOS 字段)
-d, --home-dir HOME_DIR home directory of the new account 指定用户目录
-D, --defaults print or change default useradd configuration 单独使用 useradd -D 会显示当前默认值;结合其他选项可修改默认配置
-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 自动创建用户目录 /home/<username>
-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 允许创建具有重复 UID 的用户(通常 UID 是唯一的,此选项用于特殊情况下创建共享 UID 的用户)
(non-unique) UID
-p, --password PASSWORD encrypted password of the new account 设置用户密码 (需要提供的是已加密的密码字符串,而不是明文)
-r, --system create a system account 创建系统账号(UID通常小于1000,用于运行服务)
-R, --root CHROOT_DIR directory to chroot into 在指定的 chroot 目录中创建用户
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-s, --shell SHELL login shell of the new account 指定用户登录shell
-u, --uid UID user ID of the new account 指定 UID (唯一,除非 -o)
-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
  1. 删除用户
1
userdel -f -r <username>
1
2
3
4
5
6
7
8
9
10
11
12
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 在指定的 chroot 目录中执行删除操作
-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

  1. 修改用户
1
2
3
4
5
usermod -l newname oldname    # 修改用户名
usermod -g groupname user # 修改主组
usermod -G group1,group2 user # 添加附属组
usermod -d /new/home user # 修改 home 目录
usermod -s /bin/zsh user # 修改登录 shell
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
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
  1. 查看用户信息
1
2
3
4
id username        # 显示 UID、GID、组信息
groups username # 显示用户所属组
whoami # 当前登录用户
getent passwd user # 从系统数据库查询用户信息

getent 是 Linux 系统中用于查询系统管理数据库的工具,而 passwd 是其支持的核心数据库之一(对应 /etc/passwd 文件及系统其他用户数据库)。getent passwd <username> 命令的核心作用是:查询指定用户名(**<username>**)的用户账号详细信息,输出格式与 /etc/passwd 文件的记录格式完全一致。

  • 查询范围:不仅会读取 /etc/passwd 静态文件,还会自动查询系统配置的其他用户数据库(如 NIS、LDAP、sssd 等集中式认证数据库),覆盖所有系统可识别的用户。

  • 输出格式:返回一条以冒号(:)分隔的字符串,包含该用户的 7 个核心属性(与 /etc/passwd 字段完全对应),比直接查看 /etc/passwd 更高效(无需手动搜索)。

1
2
1zqqx@VM-16-9-ubuntu:~$ getent passwd 1zqqx
1zqqx:x:1002:1003::/home/1zqqx:/bin/bash

用户组管理

  1. 添加组
1
groupadd groupname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
  1. 删除组
1
groupdel groupname
1
2
3
4
5
6
7
8
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
  1. 修改组
1
2
groupmod -n newgroup oldgroup   # 修改组名
groupmod -g 2000 groupname # 修改 GID
1
2
3
4
5
6
7
8
9
10
11
Usage: groupmod [options] GROUP

Options:
-g, --gid GID change the group ID to GID
-h, --help display this help message and exit
-n, --new-name NEW_GROUP change the name to NEW_GROUP
-o, --non-unique allow to use a duplicate (non-unique) GID
-p, --password PASSWORD change the password to this (encrypted)
PASSWORD
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
  1. 将用户加入组gpasswd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 设置组密码(设置后,非组成员可以通过 newgrp 组名 命令并输入组密码临时加入该组,组密码使用场景较少,通常不建议设置)
gpasswd <gname>

# 添加组管理员
gpasswd -A u1,u2,... <gname>

# 移除
gpasswd -dA u1,u2,... <gname>

# 删除组成员
gpasswd -d u1 <gname>

# 设置组的成员列表,会覆盖原有成员
gpasswd -M u1,u2,... <gname>

# 锁定/解锁 组
# 锁定组后,不允许添加新成员或更改组信息(锁定操作会在 /etc/gshadow 的组密码字段前添加 !)
gpasswd -l 组名 # 锁定组
gpasswd -u 组名 # 解锁组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Usage: gpasswd [option] GROUP

Options:
-a, --add USER add USER to GROUP
-d, --delete USER remove USER from GROUP
-h, --help display this help message and exit
-Q, --root CHROOT_DIR directory to chroot into
-r, --remove-password remove the GROUP's password
-R, --restrict restrict access to GROUP to its members
-M, --members USER,... set the list of members of GROUP
--extrausers use the extra users database
-A, --administrators ADMIN,...
set the list of administrators for GROUP
Except for the -A and -M options, the options cannot be combined.

示例:

  1. 创建一个组并添加成员:

groupadd dev

gpasswd -a alice dev

gpasswd -a bob dev

  1. 设置组管理员并让其管理成员:

gpasswd -A alice dev 设 alice 为 dev 组管理员,之后 alice 可以执行:

gpasswd -a charlie dev无需 root 权限

  1. 查看组信息确认修改:

getent group dev

getent gshadow dev


Linux 用户&用户组管理
https://1zqqx.github.io/2025/09/14/Linux-用户-用户组管理/
作者
1zqqx
发布于
2025年9月14日
许可协议