Linux 用户管理入门:搞懂 Users、Groups、root、`/etc/passwd` 和 `useradd`

Linux 用户管理入门:搞懂 Users、Groups、root、/etc/passwduseradd

在 Linux 学习过程中,用户管理是一个绕不开的核心主题。

不管你是刚开始接触服务器,还是准备学习权限控制、部署环境、Shell 运维,都会频繁遇到这些关键词:usergrouproot/etc/passwd/etc/shadowuseraddpasswd

这篇文章就结合初学者的学习路径,用一篇博客带你把 Linux 用户管理的基础知识梳理清楚。

文章目录

  • [Linux 用户管理入门:搞懂 Users、Groups、root、`/etc/passwd` 和 `useradd`](#Linux 用户管理入门:搞懂 Users、Groups、root、/etc/passwduseradd)
    • [一、为什么要学习 Linux 用户管理?](#一、为什么要学习 Linux 用户管理?)
    • 二、什么是用户和用户组?
    • [三、root 是什么?为什么它这么特殊?](#三、root 是什么?为什么它这么特殊?)
      • [root 与普通用户对比图](#root 与普通用户对比图)
      • [为什么不建议长期直接使用 root?](#为什么不建议长期直接使用 root?)
    • [四、`/etc/passwd` 是什么?](#四、/etc/passwd 是什么?)
      • [`/etc/passwd` 字段结构图](#/etc/passwd 字段结构图)
      • 重点说明
    • [五、`/etc/shadow` 是什么?](#五、/etc/shadow 是什么?)
    • [六、`/etc/group` 是什么?](#六、/etc/group 是什么?)
    • 七、用户管理最常用的命令有哪些?
    • 八、几个最实用的用户管理命令讲解
      • [1. `whoami`:我当前是谁?](#1. whoami:我当前是谁?)
      • [2. `id`:查看用户身份详情](#2. id:查看用户身份详情)
      • [3. `useradd`:创建用户](#3. useradd:创建用户)
      • [4. `passwd`:设置用户密码](#4. passwd:设置用户密码)
      • [5. `usermod`:修改用户](#5. usermod:修改用户)
      • [6. `userdel`:删除用户](#6. userdel:删除用户)
    • 九、一个完整的用户管理练习流程
    • 十、初学者最容易混淆的几个点
      • [1. 用户和用户组不是一回事](#1. 用户和用户组不是一回事)
      • [2. `/etc/passwd` 不是密码明文文件](#2. /etc/passwd 不是密码明文文件)
      • [3. root 权限高,不代表应该一直用 root](#3. root 权限高,不代表应该一直用 root)
      • [4. 删除用户前要先确认影响范围](#4. 删除用户前要先确认影响范围)
    • [十一、一张图总结 Linux 用户管理知识结构](#十一、一张图总结 Linux 用户管理知识结构)
    • 十二、总结

一、为什么要学习 Linux 用户管理?

很多初学者一开始会觉得:Linux 不就是敲命令吗,为什么还要专门学"用户管理"?

原因很简单:Linux 是一个多用户操作系统。

也就是说:

  • 不同用户可以登录同一台系统
  • 不同用户拥有不同权限
  • 不同用户可以属于不同用户组
  • 系统会根据用户身份决定"你能做什么、不能做什么"

所以,学会用户管理,实际上就是在学 Linux 的权限基础。

用户管理整体关系图

#mermaid-svg-b0ncDNd3GijsHgpH{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-b0ncDNd3GijsHgpH .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-b0ncDNd3GijsHgpH .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-b0ncDNd3GijsHgpH .error-icon{fill:#552222;}#mermaid-svg-b0ncDNd3GijsHgpH .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-b0ncDNd3GijsHgpH .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-b0ncDNd3GijsHgpH .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-b0ncDNd3GijsHgpH .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-b0ncDNd3GijsHgpH .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-b0ncDNd3GijsHgpH .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-b0ncDNd3GijsHgpH .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-b0ncDNd3GijsHgpH .marker{fill:#333333;stroke:#333333;}#mermaid-svg-b0ncDNd3GijsHgpH .marker.cross{stroke:#333333;}#mermaid-svg-b0ncDNd3GijsHgpH svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-b0ncDNd3GijsHgpH p{margin:0;}#mermaid-svg-b0ncDNd3GijsHgpH .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-b0ncDNd3GijsHgpH .cluster-label text{fill:#333;}#mermaid-svg-b0ncDNd3GijsHgpH .cluster-label span{color:#333;}#mermaid-svg-b0ncDNd3GijsHgpH .cluster-label span p{background-color:transparent;}#mermaid-svg-b0ncDNd3GijsHgpH .label text,#mermaid-svg-b0ncDNd3GijsHgpH span{fill:#333;color:#333;}#mermaid-svg-b0ncDNd3GijsHgpH .node rect,#mermaid-svg-b0ncDNd3GijsHgpH .node circle,#mermaid-svg-b0ncDNd3GijsHgpH .node ellipse,#mermaid-svg-b0ncDNd3GijsHgpH .node polygon,#mermaid-svg-b0ncDNd3GijsHgpH .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-b0ncDNd3GijsHgpH .rough-node .label text,#mermaid-svg-b0ncDNd3GijsHgpH .node .label text,#mermaid-svg-b0ncDNd3GijsHgpH .image-shape .label,#mermaid-svg-b0ncDNd3GijsHgpH .icon-shape .label{text-anchor:middle;}#mermaid-svg-b0ncDNd3GijsHgpH .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-b0ncDNd3GijsHgpH .rough-node .label,#mermaid-svg-b0ncDNd3GijsHgpH .node .label,#mermaid-svg-b0ncDNd3GijsHgpH .image-shape .label,#mermaid-svg-b0ncDNd3GijsHgpH .icon-shape .label{text-align:center;}#mermaid-svg-b0ncDNd3GijsHgpH .node.clickable{cursor:pointer;}#mermaid-svg-b0ncDNd3GijsHgpH .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-b0ncDNd3GijsHgpH .arrowheadPath{fill:#333333;}#mermaid-svg-b0ncDNd3GijsHgpH .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-b0ncDNd3GijsHgpH .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-b0ncDNd3GijsHgpH .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-b0ncDNd3GijsHgpH .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-b0ncDNd3GijsHgpH .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-b0ncDNd3GijsHgpH .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-b0ncDNd3GijsHgpH .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-b0ncDNd3GijsHgpH .cluster text{fill:#333;}#mermaid-svg-b0ncDNd3GijsHgpH .cluster span{color:#333;}#mermaid-svg-b0ncDNd3GijsHgpH div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-b0ncDNd3GijsHgpH .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-b0ncDNd3GijsHgpH rect.text{fill:none;stroke-width:0;}#mermaid-svg-b0ncDNd3GijsHgpH .icon-shape,#mermaid-svg-b0ncDNd3GijsHgpH .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-b0ncDNd3GijsHgpH .icon-shape p,#mermaid-svg-b0ncDNd3GijsHgpH .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-b0ncDNd3GijsHgpH .icon-shape .label rect,#mermaid-svg-b0ncDNd3GijsHgpH .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-b0ncDNd3GijsHgpH .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-b0ncDNd3GijsHgpH .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-b0ncDNd3GijsHgpH :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Linux 系统
用户 User
用户组 Group
普通用户
root 超级用户
受权限限制
拥有最高权限
用户信息文件
组信息文件


二、什么是用户和用户组?

在 Linux 中,用户用户组 是两个非常基础的概念。

1. 用户(User)

用户就是系统中的一个身份。

例如:

  • 你自己登录系统时使用的账号
  • 某个专门运行服务的账号
  • 系统自动创建的一些服务账号

每个用户通常都具有:

  • 用户名
  • 用户 ID,也就是 UID
  • 所属组 ID,也就是 GID
  • 家目录
  • 默认 Shell

2. 用户组(Group)

用户组可以理解为:一批用户的集合

Linux 通过用户组来统一管理权限,这样就不需要一个用户一个用户地单独设置。

比如:

  • 开发人员可以放在一个组里
  • 运维人员可以放在一个组里
  • 某些共享目录可以只允许某个组访问

用户与用户组关系图

#mermaid-svg-lFCjz8tNVn8s7Ebi{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-lFCjz8tNVn8s7Ebi .error-icon{fill:#552222;}#mermaid-svg-lFCjz8tNVn8s7Ebi .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-lFCjz8tNVn8s7Ebi .marker{fill:#333333;stroke:#333333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .marker.cross{stroke:#333333;}#mermaid-svg-lFCjz8tNVn8s7Ebi svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-lFCjz8tNVn8s7Ebi p{margin:0;}#mermaid-svg-lFCjz8tNVn8s7Ebi .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .cluster-label text{fill:#333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .cluster-label span{color:#333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .cluster-label span p{background-color:transparent;}#mermaid-svg-lFCjz8tNVn8s7Ebi .label text,#mermaid-svg-lFCjz8tNVn8s7Ebi span{fill:#333;color:#333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .node rect,#mermaid-svg-lFCjz8tNVn8s7Ebi .node circle,#mermaid-svg-lFCjz8tNVn8s7Ebi .node ellipse,#mermaid-svg-lFCjz8tNVn8s7Ebi .node polygon,#mermaid-svg-lFCjz8tNVn8s7Ebi .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-lFCjz8tNVn8s7Ebi .rough-node .label text,#mermaid-svg-lFCjz8tNVn8s7Ebi .node .label text,#mermaid-svg-lFCjz8tNVn8s7Ebi .image-shape .label,#mermaid-svg-lFCjz8tNVn8s7Ebi .icon-shape .label{text-anchor:middle;}#mermaid-svg-lFCjz8tNVn8s7Ebi .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-lFCjz8tNVn8s7Ebi .rough-node .label,#mermaid-svg-lFCjz8tNVn8s7Ebi .node .label,#mermaid-svg-lFCjz8tNVn8s7Ebi .image-shape .label,#mermaid-svg-lFCjz8tNVn8s7Ebi .icon-shape .label{text-align:center;}#mermaid-svg-lFCjz8tNVn8s7Ebi .node.clickable{cursor:pointer;}#mermaid-svg-lFCjz8tNVn8s7Ebi .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .arrowheadPath{fill:#333333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-lFCjz8tNVn8s7Ebi .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-lFCjz8tNVn8s7Ebi .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-lFCjz8tNVn8s7Ebi .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-lFCjz8tNVn8s7Ebi .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-lFCjz8tNVn8s7Ebi .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-lFCjz8tNVn8s7Ebi .cluster text{fill:#333;}#mermaid-svg-lFCjz8tNVn8s7Ebi .cluster span{color:#333;}#mermaid-svg-lFCjz8tNVn8s7Ebi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-lFCjz8tNVn8s7Ebi .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-lFCjz8tNVn8s7Ebi rect.text{fill:none;stroke-width:0;}#mermaid-svg-lFCjz8tNVn8s7Ebi .icon-shape,#mermaid-svg-lFCjz8tNVn8s7Ebi .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-lFCjz8tNVn8s7Ebi .icon-shape p,#mermaid-svg-lFCjz8tNVn8s7Ebi .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-lFCjz8tNVn8s7Ebi .icon-shape .label rect,#mermaid-svg-lFCjz8tNVn8s7Ebi .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-lFCjz8tNVn8s7Ebi .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-lFCjz8tNVn8s7Ebi .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-lFCjz8tNVn8s7Ebi :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户 tom
主组 developers
用户 alice
用户 bob
主组 ops
附加组 docker

简单理解就是:

  • 用户是"个人身份"
  • 用户组是"权限集合"

三、root 是什么?为什么它这么特殊?

在 Linux 中,root 是超级用户,也就是系统中权限最高的账号。

它的特点是:

  • 可以访问几乎所有文件
  • 可以创建、删除、修改用户
  • 可以安装、卸载软件
  • 可以修改系统配置
  • 可以管理服务和进程

你可以把 root 理解成 Linux 系统中的"管理员总开关"。

root 与普通用户对比图

#mermaid-svg-WQjaL2TNctReeufX{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-WQjaL2TNctReeufX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-WQjaL2TNctReeufX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-WQjaL2TNctReeufX .error-icon{fill:#552222;}#mermaid-svg-WQjaL2TNctReeufX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-WQjaL2TNctReeufX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-WQjaL2TNctReeufX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-WQjaL2TNctReeufX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-WQjaL2TNctReeufX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-WQjaL2TNctReeufX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-WQjaL2TNctReeufX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-WQjaL2TNctReeufX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-WQjaL2TNctReeufX .marker.cross{stroke:#333333;}#mermaid-svg-WQjaL2TNctReeufX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-WQjaL2TNctReeufX p{margin:0;}#mermaid-svg-WQjaL2TNctReeufX .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-WQjaL2TNctReeufX .cluster-label text{fill:#333;}#mermaid-svg-WQjaL2TNctReeufX .cluster-label span{color:#333;}#mermaid-svg-WQjaL2TNctReeufX .cluster-label span p{background-color:transparent;}#mermaid-svg-WQjaL2TNctReeufX .label text,#mermaid-svg-WQjaL2TNctReeufX span{fill:#333;color:#333;}#mermaid-svg-WQjaL2TNctReeufX .node rect,#mermaid-svg-WQjaL2TNctReeufX .node circle,#mermaid-svg-WQjaL2TNctReeufX .node ellipse,#mermaid-svg-WQjaL2TNctReeufX .node polygon,#mermaid-svg-WQjaL2TNctReeufX .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-WQjaL2TNctReeufX .rough-node .label text,#mermaid-svg-WQjaL2TNctReeufX .node .label text,#mermaid-svg-WQjaL2TNctReeufX .image-shape .label,#mermaid-svg-WQjaL2TNctReeufX .icon-shape .label{text-anchor:middle;}#mermaid-svg-WQjaL2TNctReeufX .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-WQjaL2TNctReeufX .rough-node .label,#mermaid-svg-WQjaL2TNctReeufX .node .label,#mermaid-svg-WQjaL2TNctReeufX .image-shape .label,#mermaid-svg-WQjaL2TNctReeufX .icon-shape .label{text-align:center;}#mermaid-svg-WQjaL2TNctReeufX .node.clickable{cursor:pointer;}#mermaid-svg-WQjaL2TNctReeufX .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-WQjaL2TNctReeufX .arrowheadPath{fill:#333333;}#mermaid-svg-WQjaL2TNctReeufX .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-WQjaL2TNctReeufX .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-WQjaL2TNctReeufX .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-WQjaL2TNctReeufX .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-WQjaL2TNctReeufX .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-WQjaL2TNctReeufX .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-WQjaL2TNctReeufX .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-WQjaL2TNctReeufX .cluster text{fill:#333;}#mermaid-svg-WQjaL2TNctReeufX .cluster span{color:#333;}#mermaid-svg-WQjaL2TNctReeufX div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-WQjaL2TNctReeufX .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-WQjaL2TNctReeufX rect.text{fill:none;stroke-width:0;}#mermaid-svg-WQjaL2TNctReeufX .icon-shape,#mermaid-svg-WQjaL2TNctReeufX .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-WQjaL2TNctReeufX .icon-shape p,#mermaid-svg-WQjaL2TNctReeufX .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-WQjaL2TNctReeufX .icon-shape .label rect,#mermaid-svg-WQjaL2TNctReeufX .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-WQjaL2TNctReeufX .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-WQjaL2TNctReeufX .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-WQjaL2TNctReeufX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Linux 用户
普通用户
root 用户
只能访问被授权资源
很多系统操作会被拒绝
可管理系统配置
可管理用户和权限
可访问大部分系统资源

为什么不建议长期直接使用 root?

虽然 root 很强大,但也很危险。

因为:

  • 一条错误命令就可能删掉关键文件
  • 一次误操作就可能导致服务异常
  • 安全风险更高

例如下面这种命令,一旦执行错误,后果会非常严重:

bash 复制代码
rm -rf /some/path

所以实际工作中,更推荐:

  • 平时使用普通用户
  • 需要管理员权限时,再通过 sudo 提权

四、/etc/passwd 是什么?

/etc/passwd 是 Linux 中非常重要的用户信息文件,用来保存系统中的用户基本信息。

你可以这样查看:

bash 复制代码
cat /etc/passwd

文件中的每一行通常代表一个用户,例如:

bash 复制代码
tom:x:1001:1001:Tom User:/home/tom:/bin/bash

这行内容一般可以拆成下面几个字段:

  • 用户名
  • 密码占位符
  • UID
  • GID
  • 用户描述信息
  • 家目录
  • 登录 Shell

/etc/passwd 字段结构图

#mermaid-svg-m8L6Qmd7sLJBGXSF{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-m8L6Qmd7sLJBGXSF .error-icon{fill:#552222;}#mermaid-svg-m8L6Qmd7sLJBGXSF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-m8L6Qmd7sLJBGXSF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .marker.cross{stroke:#333333;}#mermaid-svg-m8L6Qmd7sLJBGXSF svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-m8L6Qmd7sLJBGXSF p{margin:0;}#mermaid-svg-m8L6Qmd7sLJBGXSF .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .cluster-label text{fill:#333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .cluster-label span{color:#333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .cluster-label span p{background-color:transparent;}#mermaid-svg-m8L6Qmd7sLJBGXSF .label text,#mermaid-svg-m8L6Qmd7sLJBGXSF span{fill:#333;color:#333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .node rect,#mermaid-svg-m8L6Qmd7sLJBGXSF .node circle,#mermaid-svg-m8L6Qmd7sLJBGXSF .node ellipse,#mermaid-svg-m8L6Qmd7sLJBGXSF .node polygon,#mermaid-svg-m8L6Qmd7sLJBGXSF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-m8L6Qmd7sLJBGXSF .rough-node .label text,#mermaid-svg-m8L6Qmd7sLJBGXSF .node .label text,#mermaid-svg-m8L6Qmd7sLJBGXSF .image-shape .label,#mermaid-svg-m8L6Qmd7sLJBGXSF .icon-shape .label{text-anchor:middle;}#mermaid-svg-m8L6Qmd7sLJBGXSF .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-m8L6Qmd7sLJBGXSF .rough-node .label,#mermaid-svg-m8L6Qmd7sLJBGXSF .node .label,#mermaid-svg-m8L6Qmd7sLJBGXSF .image-shape .label,#mermaid-svg-m8L6Qmd7sLJBGXSF .icon-shape .label{text-align:center;}#mermaid-svg-m8L6Qmd7sLJBGXSF .node.clickable{cursor:pointer;}#mermaid-svg-m8L6Qmd7sLJBGXSF .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .arrowheadPath{fill:#333333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-m8L6Qmd7sLJBGXSF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-m8L6Qmd7sLJBGXSF .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-m8L6Qmd7sLJBGXSF .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-m8L6Qmd7sLJBGXSF .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-m8L6Qmd7sLJBGXSF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-m8L6Qmd7sLJBGXSF .cluster text{fill:#333;}#mermaid-svg-m8L6Qmd7sLJBGXSF .cluster span{color:#333;}#mermaid-svg-m8L6Qmd7sLJBGXSF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-m8L6Qmd7sLJBGXSF .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-m8L6Qmd7sLJBGXSF rect.text{fill:none;stroke-width:0;}#mermaid-svg-m8L6Qmd7sLJBGXSF .icon-shape,#mermaid-svg-m8L6Qmd7sLJBGXSF .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-m8L6Qmd7sLJBGXSF .icon-shape p,#mermaid-svg-m8L6Qmd7sLJBGXSF .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-m8L6Qmd7sLJBGXSF .icon-shape .label rect,#mermaid-svg-m8L6Qmd7sLJBGXSF .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-m8L6Qmd7sLJBGXSF .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-m8L6Qmd7sLJBGXSF .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-m8L6Qmd7sLJBGXSF :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户名
密码占位符
UID
GID
描述信息
家目录
登录 Shell

重点说明

现在很多 Linux 发行版中,/etc/passwd 已经不直接保存真实密码了。

你看到的通常是一个 x,表示真正的密码信息被放到了另一个更安全的文件里,也就是 /etc/shadow


五、/etc/shadow 是什么?

/etc/shadow 用来保存用户密码相关信息,是比 /etc/passwd 更敏感的系统文件。

你可以这样理解:

  • /etc/passwd:公开用户基本资料
  • /etc/shadow:保存密码摘要和密码策略信息

一般情况下,普通用户不能直接查看这个文件。

如果你尝试执行:

bash 复制代码
cat /etc/shadow

通常会提示权限不足,只有 root 或具备相应权限的用户才能访问。

/etc/shadow 的意义

它主要解决了一个安全问题:

如果所有人都能读取真实密码信息,那系统风险会非常大。

所以 Linux 把密码单独放在更受保护的文件里。

用户信息与密码信息分离图

渲染错误: Mermaid 渲染失败: Lexical error on line 2. Unrecognized text. ...息] --> B/etc/passwd A --> C[密码相关信息 -----------------------^


六、/etc/group 是什么?

/etc/group 是 Linux 中保存用户组信息的文件。

查看命令:

bash 复制代码
cat /etc/group

其中每一行通常表示一个组,例如:

bash 复制代码
developers:x:1002:tom,alice

这里一般包含:

  • 组名
  • 密码占位符
  • GID
  • 属于该组的成员列表

/etc/group 结构图

#mermaid-svg-O3NFqHwIiChWQda9{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-O3NFqHwIiChWQda9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-O3NFqHwIiChWQda9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-O3NFqHwIiChWQda9 .error-icon{fill:#552222;}#mermaid-svg-O3NFqHwIiChWQda9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-O3NFqHwIiChWQda9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-O3NFqHwIiChWQda9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-O3NFqHwIiChWQda9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-O3NFqHwIiChWQda9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-O3NFqHwIiChWQda9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-O3NFqHwIiChWQda9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-O3NFqHwIiChWQda9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-O3NFqHwIiChWQda9 .marker.cross{stroke:#333333;}#mermaid-svg-O3NFqHwIiChWQda9 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-O3NFqHwIiChWQda9 p{margin:0;}#mermaid-svg-O3NFqHwIiChWQda9 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-O3NFqHwIiChWQda9 .cluster-label text{fill:#333;}#mermaid-svg-O3NFqHwIiChWQda9 .cluster-label span{color:#333;}#mermaid-svg-O3NFqHwIiChWQda9 .cluster-label span p{background-color:transparent;}#mermaid-svg-O3NFqHwIiChWQda9 .label text,#mermaid-svg-O3NFqHwIiChWQda9 span{fill:#333;color:#333;}#mermaid-svg-O3NFqHwIiChWQda9 .node rect,#mermaid-svg-O3NFqHwIiChWQda9 .node circle,#mermaid-svg-O3NFqHwIiChWQda9 .node ellipse,#mermaid-svg-O3NFqHwIiChWQda9 .node polygon,#mermaid-svg-O3NFqHwIiChWQda9 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-O3NFqHwIiChWQda9 .rough-node .label text,#mermaid-svg-O3NFqHwIiChWQda9 .node .label text,#mermaid-svg-O3NFqHwIiChWQda9 .image-shape .label,#mermaid-svg-O3NFqHwIiChWQda9 .icon-shape .label{text-anchor:middle;}#mermaid-svg-O3NFqHwIiChWQda9 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-O3NFqHwIiChWQda9 .rough-node .label,#mermaid-svg-O3NFqHwIiChWQda9 .node .label,#mermaid-svg-O3NFqHwIiChWQda9 .image-shape .label,#mermaid-svg-O3NFqHwIiChWQda9 .icon-shape .label{text-align:center;}#mermaid-svg-O3NFqHwIiChWQda9 .node.clickable{cursor:pointer;}#mermaid-svg-O3NFqHwIiChWQda9 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-O3NFqHwIiChWQda9 .arrowheadPath{fill:#333333;}#mermaid-svg-O3NFqHwIiChWQda9 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-O3NFqHwIiChWQda9 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-O3NFqHwIiChWQda9 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-O3NFqHwIiChWQda9 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-O3NFqHwIiChWQda9 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-O3NFqHwIiChWQda9 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-O3NFqHwIiChWQda9 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-O3NFqHwIiChWQda9 .cluster text{fill:#333;}#mermaid-svg-O3NFqHwIiChWQda9 .cluster span{color:#333;}#mermaid-svg-O3NFqHwIiChWQda9 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-O3NFqHwIiChWQda9 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-O3NFqHwIiChWQda9 rect.text{fill:none;stroke-width:0;}#mermaid-svg-O3NFqHwIiChWQda9 .icon-shape,#mermaid-svg-O3NFqHwIiChWQda9 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-O3NFqHwIiChWQda9 .icon-shape p,#mermaid-svg-O3NFqHwIiChWQda9 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-O3NFqHwIiChWQda9 .icon-shape .label rect,#mermaid-svg-O3NFqHwIiChWQda9 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-O3NFqHwIiChWQda9 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-O3NFqHwIiChWQda9 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-O3NFqHwIiChWQda9 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 组名
密码占位符
GID
组成员列表

它有什么作用?

通过 /etc/group,系统可以知道:

  • 当前有哪些组
  • 每个组的编号是多少
  • 哪些用户属于某个组

这对文件权限控制非常关键。


七、用户管理最常用的命令有哪些?

学习 Linux 用户管理时,最常见的一批命令如下。

命令 作用 示例
whoami 查看当前用户 whoami
id 查看用户 UID、GID、所属组 id tom
useradd 创建新用户 sudo useradd tom
passwd 设置或修改密码 sudo passwd tom
usermod 修改用户属性 sudo usermod -aG docker tom
userdel 删除用户 sudo userdel tom
groupadd 创建用户组 sudo groupadd developers
groupdel 删除用户组 sudo groupdel developers
groups 查看用户所在组 groups tom
su 切换用户 su - tom
sudo 临时使用管理员权限 sudo apt update

八、几个最实用的用户管理命令讲解

1. whoami:我当前是谁?

bash 复制代码
whoami

这个命令会直接告诉你:当前终端正在使用哪个用户。


2. id:查看用户身份详情

bash 复制代码
id

或者查看指定用户:

bash 复制代码
id tom

它可以显示:

  • UID
  • GID
  • 所属组

这个命令非常适合排查权限问题。


3. useradd:创建用户

创建一个新用户:

bash 复制代码
sudo useradd tom

如果想顺便创建家目录,可以用:

bash 复制代码
sudo useradd -m tom

在很多发行版里,-m 非常常用,因为它会自动创建 /home/tom


4. passwd:设置用户密码

刚创建用户后,通常还要设置密码:

bash 复制代码
sudo passwd tom

系统会提示你输入两次密码。


5. usermod:修改用户

例如,把用户加入某个附加组:

bash 复制代码
sudo usermod -aG developers tom

这里:

  • -a 表示追加
  • -G 表示附加组

这条命令很常用于给用户增加 dockersudo 之类的组权限。


6. userdel:删除用户

删除用户:

bash 复制代码
sudo userdel tom

如果想连同家目录一起删除:

bash 复制代码
sudo userdel -r tom

这个命令要谨慎使用。


九、一个完整的用户管理练习流程

如果你是初学者,建议按下面的顺序练一遍。

操作示例

bash 复制代码
whoami
id
sudo groupadd developers
sudo useradd -m tom
sudo passwd tom
sudo usermod -aG developers tom
id tom
groups tom

这组命令完成了:

  • 查看当前用户
  • 查看当前身份信息
  • 创建一个组
  • 创建一个用户
  • 给用户设置密码
  • 把用户加入组
  • 再次检查用户和组关系

实战流程图

#mermaid-svg-w6sPkkGAVDUdmNNM{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-w6sPkkGAVDUdmNNM .error-icon{fill:#552222;}#mermaid-svg-w6sPkkGAVDUdmNNM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-w6sPkkGAVDUdmNNM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-w6sPkkGAVDUdmNNM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-w6sPkkGAVDUdmNNM .marker.cross{stroke:#333333;}#mermaid-svg-w6sPkkGAVDUdmNNM svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-w6sPkkGAVDUdmNNM p{margin:0;}#mermaid-svg-w6sPkkGAVDUdmNNM .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-w6sPkkGAVDUdmNNM .cluster-label text{fill:#333;}#mermaid-svg-w6sPkkGAVDUdmNNM .cluster-label span{color:#333;}#mermaid-svg-w6sPkkGAVDUdmNNM .cluster-label span p{background-color:transparent;}#mermaid-svg-w6sPkkGAVDUdmNNM .label text,#mermaid-svg-w6sPkkGAVDUdmNNM span{fill:#333;color:#333;}#mermaid-svg-w6sPkkGAVDUdmNNM .node rect,#mermaid-svg-w6sPkkGAVDUdmNNM .node circle,#mermaid-svg-w6sPkkGAVDUdmNNM .node ellipse,#mermaid-svg-w6sPkkGAVDUdmNNM .node polygon,#mermaid-svg-w6sPkkGAVDUdmNNM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-w6sPkkGAVDUdmNNM .rough-node .label text,#mermaid-svg-w6sPkkGAVDUdmNNM .node .label text,#mermaid-svg-w6sPkkGAVDUdmNNM .image-shape .label,#mermaid-svg-w6sPkkGAVDUdmNNM .icon-shape .label{text-anchor:middle;}#mermaid-svg-w6sPkkGAVDUdmNNM .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-w6sPkkGAVDUdmNNM .rough-node .label,#mermaid-svg-w6sPkkGAVDUdmNNM .node .label,#mermaid-svg-w6sPkkGAVDUdmNNM .image-shape .label,#mermaid-svg-w6sPkkGAVDUdmNNM .icon-shape .label{text-align:center;}#mermaid-svg-w6sPkkGAVDUdmNNM .node.clickable{cursor:pointer;}#mermaid-svg-w6sPkkGAVDUdmNNM .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-w6sPkkGAVDUdmNNM .arrowheadPath{fill:#333333;}#mermaid-svg-w6sPkkGAVDUdmNNM .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-w6sPkkGAVDUdmNNM .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-w6sPkkGAVDUdmNNM .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w6sPkkGAVDUdmNNM .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-w6sPkkGAVDUdmNNM .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w6sPkkGAVDUdmNNM .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-w6sPkkGAVDUdmNNM .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-w6sPkkGAVDUdmNNM .cluster text{fill:#333;}#mermaid-svg-w6sPkkGAVDUdmNNM .cluster span{color:#333;}#mermaid-svg-w6sPkkGAVDUdmNNM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-w6sPkkGAVDUdmNNM .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-w6sPkkGAVDUdmNNM rect.text{fill:none;stroke-width:0;}#mermaid-svg-w6sPkkGAVDUdmNNM .icon-shape,#mermaid-svg-w6sPkkGAVDUdmNNM .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w6sPkkGAVDUdmNNM .icon-shape p,#mermaid-svg-w6sPkkGAVDUdmNNM .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-w6sPkkGAVDUdmNNM .icon-shape .label rect,#mermaid-svg-w6sPkkGAVDUdmNNM .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w6sPkkGAVDUdmNNM .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-w6sPkkGAVDUdmNNM .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-w6sPkkGAVDUdmNNM :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 查看当前用户 whoami
查看身份信息 id
创建用户组 groupadd
创建用户 useradd -m
设置密码 passwd
加入组 usermod -aG
验证结果 id 和 groups


十、初学者最容易混淆的几个点

1. 用户和用户组不是一回事

很多人会把"用户"和"组"混为一谈。

你要记住:

  • 用户是具体账号
  • 组是权限集合

一个用户可以属于多个组。

2. /etc/passwd 不是密码明文文件

虽然文件名里有 passwd,但它通常不保存真正密码。

真正敏感的密码信息在 /etc/shadow

3. root 权限高,不代表应该一直用 root

root 的确方便,但风险也最高。开发、测试、日常操作尽量使用普通用户,必要时再 sudo

4. 删除用户前要先确认影响范围

执行:

bash 复制代码
sudo userdel -r tom

可能会连用户目录一起删掉,所以一定要确认后再操作。


十一、一张图总结 Linux 用户管理知识结构

#mermaid-svg-gxG4aT2pkHHzz82h{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-gxG4aT2pkHHzz82h .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-gxG4aT2pkHHzz82h .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-gxG4aT2pkHHzz82h .error-icon{fill:#552222;}#mermaid-svg-gxG4aT2pkHHzz82h .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gxG4aT2pkHHzz82h .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-gxG4aT2pkHHzz82h .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gxG4aT2pkHHzz82h .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gxG4aT2pkHHzz82h .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-gxG4aT2pkHHzz82h .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gxG4aT2pkHHzz82h .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gxG4aT2pkHHzz82h .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gxG4aT2pkHHzz82h .marker.cross{stroke:#333333;}#mermaid-svg-gxG4aT2pkHHzz82h svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gxG4aT2pkHHzz82h p{margin:0;}#mermaid-svg-gxG4aT2pkHHzz82h .edge{stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .section--1 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section--1 path,#mermaid-svg-gxG4aT2pkHHzz82h .section--1 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section--1 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section--1 text{fill:#ffffff;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth--1{stroke-width:17;}#mermaid-svg-gxG4aT2pkHHzz82h .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-0 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-0 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-0 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-0 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-0 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-0{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-0{stroke-width:14;}#mermaid-svg-gxG4aT2pkHHzz82h .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-1 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-1 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-1 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-1 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-1 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-1{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-1{stroke-width:11;}#mermaid-svg-gxG4aT2pkHHzz82h .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-2 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-2 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-2 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-2 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-2 text{fill:#ffffff;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-2{stroke-width:8;}#mermaid-svg-gxG4aT2pkHHzz82h .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-3 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-3 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-3 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-3 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-3 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-3{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-3{stroke-width:5;}#mermaid-svg-gxG4aT2pkHHzz82h .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-4 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-4 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-4 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-4 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-4 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-4{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-4{stroke-width:2;}#mermaid-svg-gxG4aT2pkHHzz82h .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-5 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-5 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-5 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-5 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-5 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-5{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-5{stroke-width:-1;}#mermaid-svg-gxG4aT2pkHHzz82h .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-6 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-6 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-6 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-6 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-6 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-6{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-6{stroke-width:-4;}#mermaid-svg-gxG4aT2pkHHzz82h .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-7 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-7 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-7 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-7 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-7 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-7{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-7{stroke-width:-7;}#mermaid-svg-gxG4aT2pkHHzz82h .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-8 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-8 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-8 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-8 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-8 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-8{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-8{stroke-width:-10;}#mermaid-svg-gxG4aT2pkHHzz82h .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-9 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-9 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-9 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-9 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-9 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-9{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-9{stroke-width:-13;}#mermaid-svg-gxG4aT2pkHHzz82h .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-10 rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-10 path,#mermaid-svg-gxG4aT2pkHHzz82h .section-10 circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-10 polygon,#mermaid-svg-gxG4aT2pkHHzz82h .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-10 text{fill:black;}#mermaid-svg-gxG4aT2pkHHzz82h .node-icon-10{font-size:40px;color:black;}#mermaid-svg-gxG4aT2pkHHzz82h .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .edge-depth-10{stroke-width:-16;}#mermaid-svg-gxG4aT2pkHHzz82h .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled,#mermaid-svg-gxG4aT2pkHHzz82h .disabled circle,#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:lightgray;}#mermaid-svg-gxG4aT2pkHHzz82h .disabled text{fill:#efefef;}#mermaid-svg-gxG4aT2pkHHzz82h .section-root rect,#mermaid-svg-gxG4aT2pkHHzz82h .section-root path,#mermaid-svg-gxG4aT2pkHHzz82h .section-root circle,#mermaid-svg-gxG4aT2pkHHzz82h .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-gxG4aT2pkHHzz82h .section-root text{fill:#ffffff;}#mermaid-svg-gxG4aT2pkHHzz82h .section-root span{color:#ffffff;}#mermaid-svg-gxG4aT2pkHHzz82h .section-2 span{color:#ffffff;}#mermaid-svg-gxG4aT2pkHHzz82h .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-gxG4aT2pkHHzz82h .edge{fill:none;}#mermaid-svg-gxG4aT2pkHHzz82h .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-gxG4aT2pkHHzz82h :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Linux 用户管理
用户 User
用户名
UID
家目录
登录 Shell
用户组 Group
GID
主组
附加组
特殊用户
root
系统服务账号
普通用户
关键文件
/etc/passwd
/etc/shadow
/etc/group
常用命令
whoami
id
useradd
passwd
usermod
userdel
sudo


十二、总结

Linux 用户管理并不只是"创建账号"这么简单,它本质上关系到整个系统的:

  • 身份识别
  • 权限控制
  • 资源访问
  • 安全管理

你只要先把下面这些内容搞明白,就已经打下了很好的基础:

  • 什么是用户和用户组
  • root 为什么特殊
  • /etc/passwd/etc/shadow/etc/group 分别做什么
  • useraddpasswdusermoduserdel 怎么用

后面继续学习 Linux 权限、文件属主、chmodchownsudoers 配置时,你会发现这些知识都会串起来。

如果你正在系统学习 Linux,建议把这篇文章收藏下来,后面配合实际命令练习一遍,理解会快很多。