权限管理命令
权限管理命令用于控制文件、目录的访问权限以及用户和组的管理。
命令列表
权限控制
用户切换
用户管理
组管理
扩展属性
权限基础知识
标准权限
Linux文件权限使用三组权限位:
- 所有者权限 (user): 文件/目录的所有者
- 组权限 (group): 文件/目录所属组的成员
- 其他权限 (other): 其他所有用户
每组权限包含三个权限位:
- r (read): 读权限 - 4
- w (write): 写权限 - 2
- x (execute): 执行权限 - 1
权限表示方法
bash
# 符号表示法
chmod u+rwx,g+rw,o+r filename
# 数字表示法
chmod 764 filename
# 常用权限组合
chmod 755 script.sh # 可执行文件
chmod 644 document.txt # 普通文件
chmod 600 private.key # 私密文件
chmod 755 directory/ # 目录权限1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
特殊权限
bash
# SUID (Set User ID) - 4000
chmod u+s executable
# SGID (Set Group ID) - 2000
chmod g+s executable
# Sticky Bit - 1000
chmod +t directory/1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
实用示例
批量权限管理
bash
# 递归修改目录权限
chmod -R 755 /var/www/html/
# 批量修改文件所有者
chown -R www-data:www-data /var/www/
# 查找并修改权限
find /path -type f -exec chmod 644 {} \;
find /path -type d -exec chmod 755 {} \;1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
用户组管理
bash
# 创建用户并指定组
useradd -g users -G wheel,docker newuser
# 将用户添加到组
usermod -aG sudo username
# 查看用户组信息
groups username
id username1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9