linux/用户管理.md
2025-03-19 10:43:13 +08:00

695 lines
16 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h2><center>用户管理</center></h2>
------
## 一:用户
### 1. 创建用户
```bash
# 创建用户(自动创建家目录 `/home/username`
[root@wxin ~]# useradd user01
[root@wxin ~]# id user01
uid=1001(user01) gid=1001(user01) =1001(user01)
# 创建用户并指定家目录和登录 Shell
[root@wxin ~]# useradd -m -d /usr/user02 -s /bin/bash user02
[root@wxin ~]# id user02
uid=1002(user02) gid=1002(user02) =1002(user02)
[root@wxin ~]# ll /usr/
drwx------. 3 user02 user02 78 3月 17 19:03 user02
# 创建用户并指定用户IDUID和主组IDGID
[root@wxin ~]# useradd -u 1005 user03
[root@wxin ~]# id user03
uid=1005(user03) gid=1005(user03) =1005(user03)
```
### 2. 设置/修改密码
```bash
# root修改其他用户密码
[root@wxin ~]# passwd user01
# 用户修改自己的密码
[user02@wxin ~]$ passwd
```
### 3. 删除用户
```bash
# 删除用户但保留家目录
[root@wxin ~]# userdel user02
# 删除用户及家目录
[root@wxin ~]# userdel -r user02
```
### 4. 修改用户属性
```bash
# 修改用户名
[root@wxin ~]# usermod -l user05 user01 # 旧用户名 新用户名
[root@wxin ~]# id user05
uid=1001(user05) gid=1001(user01) =1001(user01)
# 修改用户家目录(需手动迁移文件)
[root@wxin ~]# ll /home
drwx------. 3 user03 user03 78 3月 17 19:05 user03
[root@wxin ~]# usermod -d /usr/user3 -m user03
[root@wxin ~]# ll /usr
drwx------. 3 user03 user03 78 3月 17 19:05 user3
# 修改用户默认 Shell
[root@wxin ~]# usermod -s /sbin/nologin user01
```
### 5. 密码策略管理
修改密码策略
```bash
[root@wxin ~]# chage -l user01
最近一次密码修改时间 3月 17, 2025
密码过期时间 :从不
密码失效时间 :从不
帐户过期时间 :从不
两次改变密码之间相距的最小天数 0
两次改变密码之间相距的最大天数 99999
在密码过期之前警告的天数 7
[root@wxin ~]# chage -M 90 -W 10 user01
[root@wxin ~]# chage -l user01
最近一次密码修改时间 3月 17, 2025
密码过期时间 6月 15, 2025
密码失效时间 :从不
帐户过期时间 :从不
两次改变密码之间相距的最小天数 0
两次改变密码之间相距的最大天数 90
在密码过期之前警告的天数 10
# 查看密码策略
[root@wxin ~]# sudo chage -l user01
```
全局密码策略文件 /etc/login.defs
```shell
# 修改以下参数:
PASS_MAX_DAYS 90 # 密码最长有效期
PASS_MIN_DAYS 1 # 密码修改间隔
PASS_MIN_LEN 8 # 最小密码长度
PASS_WARN_AGE 7 # 过期前警告天数
```
### 6. 用户配置文件
/etc/passwd
```shell
格式username:x:UID:GID:描述:家目录:Shell
示例john:x:1001:1001:John Doe:/home/john:/bin/bash
```
/etc/shadow
```shell
john:$6$salt...:19101:0:99999:7:::
# 字段含义:用户名:加密密码:最后修改天数:最小天数:最大天数:警告天数:失效天数
```
/etc/group
```shell
格式groupname:x:GID:成员列表
developers:x:1002:user1,user2
```
## 二:用户组
### 1. 创建/删除组
```bash
# 创建组
[root@wxin ~]# groupadd hr
[root@wxin ~]# tail -1 /etc/group
hr:x:1006:
# 删除组
[root@wxin ~]# groupdel hr
[root@wxin ~]# grep hr /etc/group
```
### 2. 用户与组关联
```bash
# 将用户加入附加组(不影响主组)
[root@wxin ~]# usermod -aG hr user01
[root@wxin ~]# id user01
uid=1001(user01) gid=1001(user01) =1001(user01),1006(hr)
# 将用户移除组
[root@wxin ~]# gpasswd -d user01 hr
正在将用户“user01”从“hr”组中删除
[root@wxin ~]# id user01
uid=1001(user01) gid=1001(user01) =1001(user01)
# 修改用户的主组
[root@wxin ~]# usermod -g hr user03
[root@wxin ~]# id user03
uid=1005(user03) gid=1006(hr) =1006(hr)
# 查看用户所属组
[root@wxin ~]# groups user03
user03 : hr
```
## 三:用户权限管理
### 1. 基本权限 UGO
#### 权限对象
```shell
属主U
属组g
其他o
所有a(u+g+o)
```
#### 权限类型
```shell
读: r
写: w
执行: x
```
#### 查看权限
```bash
[root@wxin ~]# ll /root
-rw-r--r--. 1 root root 39168000 3月 17 13:01 etc.tar
- # 文件类型
rw- # 属主的权限
r-- # 属组的权限
r-- # 其他人的权限
. # 权限扩展
1 # 文件连接
root # 属主
root # 属组
39168000 # 文件大小
3月 17 13:01 # 文件最后修改时间
etc.tar # 文件名
```
#### 设置权限
**更改权限**
语法:
```shell
chmod 对象+附加值(+/-/=+权限类型 文件/目录
```
示例一:
```bash
# 编写程序
[root@wxin test]# vim file
echo "hello"
read -p "输入名字:" name
echo "$name"
# 添加执行权限
[root@wxin test]# chmod +x file
# 运行测试
[root@wxin test]# ./file
hello
输入名字zfile
zfile
# 去除权限
[root@wxin test]# chmod -x file
[root@wxin test]# ./file
bash: ./file: 权限不够
```
示例二:
```bash
[root@wxin test]# ll file
-rw-r--r--. 1 root root 57 3月 17 19:51 file
[root@wxin test]# chmod 777 file
[root@wxin test]# ll file
-rwxrwxrwx. 1 root root 57 3月 17 19:51 file
```
**更改属主**
语法:
```shell
chown 用户名.组名 文件
```
示例:
```bash
# 修改属主属组
[root@wxin test]# ll file
-rwxrwxrwx. 1 root root 57 3月 17 19:51 file
[root@wxin test]# chown user01.hr file
[root@wxin test]# ll file
-rwxrwxrwx. 1 user01 hr 57 3月 17 19:51 file
# 只修改属主
[root@wxin test]# chown user03 file
[root@wxin test]# ll file
-rwxrwxrwx. 1 user03 hr 57 3月 17 19:51 file
# 只修改属组
[root@wxin test]# chown .root file
[root@wxin test]# ll file
-rwxrwxrwx. 1 user03 root 57 3月 17 19:51 file
```
**更改属组**
语法:
```shell
chgrp [-R] 组名 文件
```
示例:
```bash
[root@wxin test]# ll file
-rwxrwxrwx. 1 user03 hr 57 3月 17 19:51 file
```
### 2. 基本权限 ACL
ACL文件权限管理设置不同用户不同的基本权限r、w、x。对象数量不同。
#### 管理命令
**查看 ACL 权限**
```bash
[root@wxin test]# getfacl /test/file
getfacl: Removing leading '/' from absolute path names
# file: test/file
# owner: user03
# group: hr
user::rwx
group::rwx
other::rwx
# 输出示例:
# user::rw-
# user:john:rwx → 用户 john 的独立权限
# group::r--
# group:dev:r-x → 组 dev 的独立权限
# mask::r-x
# other::r--
```
**设置 ACL 权限**
```bash
# 语法
setfacl -m <规则> 文件/目录
# 规则格式:
u:用户名:权限 # 为用户设置
g:组名:权限 # 为组设置
m::权限 # 设置 mask最大有效权限
```
**删除 ACL 权限**
```bash
# 删除特定用户/组的 ACL 规则
setfacl -x u:john filename
# 删除所有 ACL 规则(保留传统权限)
setfacl -b filename
```
#### 示例
**为特定用户添加权限**
```bash
# 允许用户 john 对文件有读写执行权限
setfacl -m u:john:rwx file.txt
# 验证
getfacl file.txt | grep "user:john"
```
**为组添加权限**
```bash
# 允许组 developers 对目录有读写权限
setfacl -Rm g:developers:rw dir/
# -R递归应用到目录内现有文件
# -m修改 ACL
```
**设置默认 ACL (继承权限)**
```bash
# 为目录设置默认 ACL新创建的文件自动继承权限
setfacl -dm g:developers:rwx dir/
# 新建文件测试
touch dir/newfile.txt
getfacl dir/newfile.txt | grep "group:developers"
```
**限制权限mask**
```bash
# 设置 mask 为 r-x用户/组的实际权限会被 mask 过滤)
setfacl -m m::rx file.txt
```
## 四:提权
### 1. su
#### 基本功能
**所用:**切换到另一个用户身份(默认为 root并在新会话中运行命令。
**语法:**
```bash
su [选项] [目标用户]
```
- 不指定用户时默认切换到 `root`
- 示例:`su -`(切换到 `root` 并加载环境变量)。
#### **特点**
- **需要目标用户的密码**:切换到 `root` 需要输入 `root` 的密码。
- **启动新 Shell**:默认开启一个新的子 Shell 会话,退出需输入 `exit`
- **环境变量**:使用 `su -` 会加载目标用户的环境变量(如 `root``.bash_profile`)。
#### **使用场景**
- 管理员需要长时间以 `root` 身份操作时(如系统维护)。
- 需要完全继承目标用户的环境配置。
### 2. sudo
#### 基本功能
**作用:**以其他用户(默认为 `root`)的身份执行单条命令,无需切换用户。
**语法:**
```bash
sudo [选项] 命令
```
#### 特点
- **需要当前用户的密码**:默认使用当前用户的密码(而非 `root` 密码)。
- **细粒度控制**:通过 `/etc/sudoers` 文件可配置用户允许执行的命令。
- **审计日志**:所有 `sudo` 操作会被记录到 `/var/log/auth.log`(或 `/var/log/secure`
#### 使用场景
- 普通用户临时执行需要特权的命令(如安装软件、修改配置文件)。
- 团队协作时,管理员按需授权特定命令。
#### sudo高级配置
**配置文件/etc/sudoers**
使用`visudo`命令编辑
```bash
sudo visudo
```
语法示例
```shell
# 允许用户 alice 以 root 执行所有命令
alice ALL=(ALL:ALL) ALL
# 允许用户 bob 以 root 执行特定命令(无密码)
bob ALL=(root) NOPASSWD: /usr/bin/apt, /usr/bin/systemctl
```
**用户组授权**
允许整个用户组使用`sudo`
```shell
%admin ALL=(ALL) ALL # 允许 admin 组的用户执行所有命令
```
**别名简化配置**
定义命令或用户别名
```shell
User_Alias ADMINS = alice, bob
Cmnd_Alias NETWORK = /sbin/ifconfig, /sbin/route
ADMINS ALL=(root) NETWORK
```
#### 安全实践
**禁用`su`的 root 登录**
修改`/etc/pam.d/su`,限制仅特定用户组可使用`su`
```shell
auth required pam_wheel.so use_uid group=admin
```
**配置`su`最小权限**
仅授权必要命令,避免开放 `ALL` 权限
```
# 允许用户重启服务,但不允许其他操作
charlie ALL=(root) /usr/bin/systemctl restart httpd
```
**禁用`root`远程登录**
在 SSH 配置中禁用 `root` 登录(`/etc/ssh/sshd_config`
```shell
PermitRootLogin no
```
## 五:特殊权限
### 1. 文件创建掩码
#### 作用
定义umask 是进程创建文件或目录时的默认权限过滤器,用于限制新文件的初始权限。
原理:通过掩码值屏蔽(减去)权限位,最终权限 = 默认权限 & (~umask)。
默认权限:
- 文件666 (即 rw-rw-rw-,实际受 umask 影响后可能为 644
- 目录777 (即 rwxrwxrwx实际受 umask 影响后可能为 755
#### 查看与设置 umask
```bash
# 查看当前 umask 值
[root@wxin test]# umask # 输出示例0022八进制
# 临时设置 umask仅当前 Shell 有效)
[root@wxin test]# umask 002 # 新文件权限666 → 664rw-rw-r--
# 新目录权限777 → 775rwxrwxr-x
# 永久设置 umask对用户全局生效
# 在 ~/.bashrc 或 /etc/profile 中添加:
[root@wxin test]# umask 022
```
#### 权限计算示例
```shell
默认文件权限666 (二进制 110 110 110
umask 022 (二进制 000 010 010)
实际权限666&~022 = 644 110 100 100 -> rw-r--r--
```
### 2. 文件属性 chattr
chattr 是一个用于修改文件或目录的扩展属性的命令,这些属性可以控制文件系统的行为(如防止文件被删除或修改)。
#### 基本语法
```
chattr [选项] [+/-/=属性] 文件或目录
```
- **+** :添加属性
- **-** :移除属性
- **=** :直接设置属性(覆盖原有属性)
#### 常用属性
| 属性 | 说明 |
| :--: | :----------------------------------------------------------: |
| a | **仅追加**:允许向文件追加内容,但不能修改或删除文件(适用于日志文件)。 |
| i | **不可变**:文件不能被修改、删除、重命名或创建硬链接(最高保护级别)。 |
| c | **压缩**:文件在写入时自动压缩,读取时自动解压(需文件系统支持,如 `ext2/3/4`)。 |
| s | **安全删除**:文件被删除时,其数据会被清零(防止恢复,需文件系统支持)。 |
| u | **恢复保护**:文件被删除后,其内容仍可恢复(需文件系统支持)。 |
| A | **不更新访问时间**:禁用文件的 `atime`(访问时间)更新,提升性能。 |
| S | **同步更新**:文件的修改会立即写入磁盘(类似 `sync` 挂载选项)。 |
#### 操作示例
**防止文件被修改或删除**
```bash
[root@wxin test]# chattr +i file
```
设置后,文件无法被修改、删除、重命名或创建硬链接(需 root 权限)。
**允许追加内容但禁止修改(适合于日志文件)**
```bash
[root@wxin test]# chattr +a /var/log/messages
```
允许追加日志内容,但不可直接编辑或删除文件。
**移除属性**
```bash
[root@wxin test]# chattr -i file # 移除不可变属性
[root@wxin test]# chattr -a /var/log/messages # 移除仅追加属性
```
**查看文件属性**
```bash
[root@wxin test]# lsattr file
----i----------- file
```
### 3. 特殊权限类型
#### 1. SUID
**作用**当文件被设置为SUID时执行该文件的用户会临时拥有文件所有者的权限而不是执行者的权限
**设置方法**
```bash
chmod u+s filename # 添加 SUID
chmod 4xxx filename # 用数字模式4表示SUIDxxx为基本权限如4755
示例:
[root@wxin ~]# ll showname.sh
-rwxr-xr-x. 1 root root 72 3月 19 09:26 showname.sh
[root@wxin ~]# chmod u+s showname.sh
[root@wxin ~]# ll showname.sh
-rwsr-xr-x. 1 root root 72 3月 19 09:26 showname.sh
```
**查看权限**
```bash
ls -l 文件名
```
**去除权限**
```bash
chmod u-s filename # 去除 SUID
示例:
[root@wxin ~]# ll showname.sh
-rwsr-xr-x. 1 root root 72 3月 19 09:26 showname.sh
[root@wxin ~]# chmod u-s showname.sh
[root@wxin ~]# ll showname.sh
-rwxr-xr-x. 1 root root 72 3月 19 09:26 showname.sh
```
#### 2. SGID
**作用**
- **对文件**:执行该文件的用户会临时拥有文件所属组的权限
- **对目录**:在该目录下创建的新文件/子目录,将继承目录的所属组(而非创建者的默认组)
**设置方法**
```bash
chmod g+s directory # 对目录添加 SGID
chmod 2xxx filename # 用数字模式2表示SGID如2775
示例:
[root@wxin ~]# useradd user01 -G group
[root@wxin ~]# mkdir /tmp/dir-sgid
[root@wxin ~]# chown .group /tmp/dir-sgid
[root@wxin ~]# chmod g=rwx /tmp/dir-sgid
[root@wxin ~]# ll -d /tmp/dir-sgid
drwxrwxr-x. 2 root group 34 3月 19 10:12 /tmp/dir-sgid
[root@wxin ~]# chmod g+s /tmp/dir-sgid
[root@wxin ~]# ll -d /tmp/dir-sgid
drwxrwsr-x. 2 root group 34 3月 19 10:12 /tmp/dir-sgid
[root@wxin ~]# su - user01
[user01@wxin ~]$ touch /tmp/dir-sgid/u2.txt
-rw-rw-r--. 1 user01 group 0 3月 19 10:12 u2.txt
```
#### 3. STICK
**作用**:主要用于共享目录,允许用户创建和删除自己的文件,但禁止删除其他用户的文件。
**设置方法**
```bash
chmod +t directory # 添加 Sticky Bit
chmod 1xxx directory # 用数字模式1表示Sticky Bit如1777
示例:
[root@wxin ~]# chmod +t /tmp/dir-sgid/
[root@wxin ~]# ll -d /tmp/dir-sgid/
drwxrwsr-t. 2 root group 34 3月 19 10:12 /tmp/dir-sgid/
[root@wxin ~]# su - user01
上一次登录:三 3月 19 10:17:03 CST 2025pts/0 上
[user01@wxin ~]$ ll /tmp/dir-sgid/
总用量 0
-rw-rw-r--. 1 user01 user01 0 3月 19 10:10 u1.txt
-rw-rw-r--. 1 user01 group 0 3月 19 10:12 u2.txt
[user01@wxin ~]$ rm -rf /tmp/dir-sgid/u1.txt
[user01@wxin ~]$ ll /tmp/dir-sgid/
总用量 0
-rw-rw-r--. 1 user01 group 0 3月 19 10:12 u2.txt
[user01@wxin ~]$ exit
登出
[root@wxin ~]# su - user03
[user03@wxin ~]$ rm -rf /tmp/dir-sgid/u2.txt
rm: 无法删除"/tmp/dir-sgid/u2.txt": 权限不够
```