在Linux系统中,用户权限管理是系统安全的核心。以下是Linux权限设置的详细分类和说明:
通过ls -l
可查看,包含三部分:
1. 用户(User):文件所有者
2. 组(Group):所属用户组
3. 其他(Other):其他用户
权限类型:
- r
(4):读
- w
(2):写
- x
(1):执行/进入目录
- -
:无权限
示例:
-rwxr-xr-- 1 user group 4096 Jan 1 10:00 file.txt
# 所有者:读写执行 | 组:读执行 | 其他:仅读
修改命令:
chmod u+x file.sh # 给所有者添加执行权限
chmod 755 script.sh # 数字格式设置(rwxr-xr-x)
chmod g-w /shared/ # 移除组的写权限
SUID (4)
/usr/bin/passwd
)chmod u+s file
或 chmod 4755 file
SGID (2)
chmod g+s dir
或 chmod 2770 dir
Sticky Bit (1)
/tmp
)chmod o+t dir
或 chmod 1777 dir
提供更细粒度的权限控制:
setfacl -m u:alice:rwx /project # 给alice添加rwx权限
setfacl -m g:dev:rx /app # 给dev组添加rx权限
getfacl /project # 查看ACL权限
用户操作:
useradd -m -s /bin/bash username # 创建用户
usermod -aG sudo username # 加入sudo组
passwd username # 修改密码
组操作:
groupadd dev_team # 创建组
gpasswd -a user dev_team # 添加用户到组
通过/etc/sudoers
配置(建议使用visudo
编辑):
username ALL=(ALL) NOPASSWD: ALL # 免密执行所有命令
%admin ALL=(ALL) ALL # 组权限设置
文件路径 | 作用 |
---|---|
/etc/passwd |
用户基本信息(UID、shell等) |
/etc/shadow |
用户密码(加密存储) |
/etc/group |
组信息 |
/etc/sudoers |
sudo权限配置 |
750
权限(如/etc
)700
或750
find / -perm -4000 -type f
(查找SUID文件)# 检查用户权限继承关系
groups username
id username
# 查看进程的有效权限
ps aux -e | grep process_name
掌握这些权限管理方法,可以灵活控制Linux系统的资源访问。根据实际场景组合使用这些技术(如ACL+特殊权限),能实现既安全又高效的权限配置。