本文档风哥主要介绍Linux权限管理相关知识,包括chmod命令数字法修改权限、chmod命令符号法修改权限、特殊权限设置、权限安全加固等内容,参考Red Hat Enterprise Linux 10官方文档,适合运维人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。更多视频教程www.fgedu.net.cn
Part01-基础概念与理论知识
1.1 Linux权限管理概念
Linux权限管理是系统安全的核心,通过权限控制用户对文件和目录的访问。每个文件和目录都有三组权限:所有者权限、所属组权限和其他用户权限。权限分为读(r)、写(w)、执行(x)三种基本类型。
- 访问控制:控制用户对文件的访问
- 安全隔离:保护敏感数据
- 权限管理:精细化权限控制
- 合规要求:满足安全合规要求
1.2 Linux权限类型
Linux权限分为基本权限和特殊权限:
- 基本权限:读(r=4)、写(w=2)、执行(x=1)
- 特殊权限:SUID(4)、SGID(2)、SBIT(1)
# 基本权限数字表示
r (read) = 4
w (write) = 2
x (execute) = 1
– (无权限) = 0
# 权限组合
rwx = 4+2+1 = 7
rw- = 4+2+0 = 6
r-x = 4+0+1 = 5
r– = 4+0+0 = 4
-wx = 0+2+1 = 3
-w- = 0+2+0 = 2
–x = 0+0+1 = 1
— = 0+0+0 = 0
# 三组权限表示
所有者权限 所属组权限 其他用户权限
rwx rwx rwx
7 7 7
# 常用权限组合
755 = rwxr-xr-x # 所有者全权限,组和其他用户读和执行
644 = rw-r–r– # 所有者读写,组和其他用户只读
700 = rwx—— # 所有者全权限,其他用户无权限
777 = rwxrwxrwx # 所有用户全权限(不安全)
# 特殊权限数字表示
SUID = 4 # 设置用户ID
SGID = 2 # 设置组ID
SBIT = 1 # 粘滞位
# 特殊权限示例
4755 = SUID + 755
2755 = SGID + 755
1755 = SBIT + 755
# 查看权限示例
# ls -l /etc/passwd
-rw-r–r–. 1 root root 1234 Apr 2 10:00 /etc/passwd
# 权限说明:
# – 文件类型(-普通文件,d目录,l链接)
# rw- 所有者权限(读写)
# r– 所属组权限(只读)
# r– 其他用户权限(只读)
# 1 硬链接数
# root 所有者
# root 所属组
# 1234 文件大小
# Apr 2 10:00 修改时间
# /etc/passwd 文件名
# 查看目录权限
# ls -ld /tmp
drwxrwxrwt. 10 root root 4096 Apr 2 10:00 /tmp
# 权限说明:
# d 目录类型
# rwx 所有者权限(读写执行)
# rwx 所属组权限(读写执行)
# rwt 其他用户权限(读写执行+粘滞位)
1.3 Linux权限查看方法
Linux权限查看的常用方法:
# 1. 使用ls -l查看详细权限
# ls -l file.txt
-rw-r–r–. 1 root root 1234 Apr 2 10:00 file.txt
# 2. 使用ls -ld查看目录权限
# ls -ld /home
drwxr-xr-x. 3 root root 17 Apr 2 10:00 /home
# 3. 使用stat查看详细权限信息
# stat file.txt
File: file.txt
Size: 1234 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 1234567 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2026-04-02 10:00:00.000000000 +0800
Modify: 2026-04-02 10:00:00.000000000 +0800
Change: 2026-04-02 10:00:00.000000000 +0800
Birth: 2026-04-02 10:00:00.000000000 +0800
# 4. 使用stat查看权限数字
# stat -c “%a %n” file.txt
644 file.txt
# 5. 使用stat查看权限字符
# stat -c “%A %n” file.txt
-rw-r–r– file.txt
# 6. 查看所有文件权限
# ls -l /etc/ | head -n 10
total 1234
-rw-r–r–. 1 root root 1234 Apr 2 10:00 passwd
-rw-r–r–. 1 root root 1234 Apr 2 10:00 group
-rw——-. 1 root root 1234 Apr 2 10:00 shadow
-rw——-. 1 root root 1234 Apr 2 10:00 gshadow
drwxr-xr-x. 2 root root 4096 Apr 2 10:00 ssh
drwxr-xr-x. 2 root root 4096 Apr 2 10:00 cron.d
# 7. 查看特殊权限文件
# find /usr -perm -4000 -ls | head -n 5
1234567 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/sudo
1234568 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/passwd
1234569 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/su
# 8. 查看SGID文件
# find /usr -perm -2000 -ls | head -n 5
1234570 12 -rwxr-sr-x 1 root mail 12345 Apr 2 10:00 /usr/bin/wall
1234571 12 -rwxr-sr-x 1 root tty 12345 Apr 2 10:00 /usr/bin/write
# 9. 查看SBIT目录
# find / -perm -1000 -type d -ls | head -n 5
1234572 4 drwxrwxrwt 1 root root 4096 Apr 2 10:00 /tmp
1234573 4 drwxrwxrwt 1 root root 4096 Apr 2 10:00 /var/tmp
# 10. 查看文件权限位
# getfacl file.txt
# file: file.txt
# owner: root
# group: root
user::rw-
group::r–
other::r–
Part02-生产环境规划与建议
2.1 权限规划原则
在生产环境中,权限规划应遵循最小权限原则:
# 1. 最小权限原则
– 只授予必要的权限
– 避免过度授权
– 定期审计权限
– 及时回收权限
# 2. 权限分层原则
– 系统文件:644/755
– 配置文件:600/640
– 敏感文件:600
– 脚本文件:700/750
– 目录权限:755/750
# 3. 权限规划示例
# 系统配置文件
/etc/passwd 644 root:root
/etc/shadow 600 root:root
/etc/group 644 root:root
/etc/gshadow 600 root:root
/etc/ssh/sshd_config 600 root:root
# 应用目录
/app/bin/ 755 app:app
/app/conf/ 750 app:app
/app/logs/ 775 app:app
/app/data/ 750 app:app
# 用户目录
/home/user/ 700 user:user
/home/user/.ssh/ 700 user:user
/home/user/.ssh/authorized_keys 600 user:user
# 临时目录
/tmp/ 1777 root:root
/var/tmp/ 1777 root:root
# 4. 权限矩阵示例
用户类型 文件权限 目录权限
所有者 rw- rwx
所属组 r– r-x
其他用户 — –x
# 5. 特殊权限使用场景
SUID:需要临时获得文件所有者权限的程序
SGID:需要继承目录组权限的共享目录
SBIT:需要保护用户文件的公共目录
2.2 权限安全建议
生产环境权限安全建议:
- 避免使用777权限
- 限制SUID/SGID程序
- 保护敏感文件权限
- 定期审计权限变更
- 使用ACL精细控制
- 启用SELinux增强安全
# 1. 检查777权限文件
# find / -perm 777 -type f -ls 2>/dev/null | head -n 10
(输出777权限的文件)
# 2. 检查可写文件
# find /etc -perm -002 -type f -ls
(输出其他用户可写的文件)
# 3. 检查SUID程序
# find /usr -perm -4000 -type f -ls
1234567 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/sudo
1234568 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/passwd
1234569 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/su
# 4. 检查SGID程序
# find /usr -perm -2000 -type f -ls
1234570 12 -rwxr-sr-x 1 root mail 12345 Apr 2 10:00 /usr/bin/wall
# 5. 检查无主文件
# find /home -nouser -o -nogroup -ls
(输出无主文件)
# 6. 检查可执行文件权限
# find /usr/bin -perm -111 -type f -ls | head -n 10
(输出可执行文件)
# 7. 检查敏感文件权限
# ls -l /etc/shadow /etc/gshadow /etc/ssh/sshd_config
-rw——-. 1 root root 1234 Apr 2 10:00 /etc/gshadow
-rw——-. 1 root root 1234 Apr 2 10:00 /etc/shadow
-rw——-. 1 root root root 1234 Apr 2 10:00 /etc/ssh/sshd_config
# 8. 修复敏感文件权限
# chmod 600 /etc/shadow
# chmod 600 /etc/gshadow
# chmod 600 /etc/ssh/sshd_config
# 9. 检查目录权限
# find /var -type d -perm -002 -ls
(输出其他用户可写的目录)
# 10. 检查隐藏文件权限
# find /home -name “.*” -type f -ls | head -n 10
(输出隐藏文件权限)
2.3 权限审计策略
定期审计权限是系统安全的重要环节:
# 1. 创建权限审计脚本
# cat > /tmp/permission_audit.sh << 'EOF'
#!/bin/bash
REPORT="/tmp/permission_audit_report.txt"
echo "========================================" > $REPORT
echo “Permission Audit Report – $(date)” >> $REPORT
echo “========================================” >> $REPORT
echo “” >> $REPORT
# 1. 检查777权限文件
echo “1. Files with 777 Permission” >> $REPORT
echo “—————————–” >> $REPORT
find / -perm 777 -type f -ls 2>/dev/null >> $REPORT
echo “” >> $REPORT
# 2. 检查可写文件
echo “2. World-Writable Files” >> $REPORT
echo “————————” >> $REPORT
find /etc -perm -002 -type f -ls 2>/dev/null >> $REPORT
echo “” >> $REPORT
# 3. 检查SUID程序
echo “3. SUID Programs” >> $REPORT
echo “—————–” >> $REPORT
find /usr -perm -4000 -type f -ls 2>/dev/null >> $REPORT
echo “” >> $REPORT
# 4. 检查SGID程序
echo “4. SGID Programs” >> $REPORT
echo “—————–” >> $REPORT
find /usr -perm -2000 -type f -ls 2>/dev/null >> $REPORT
echo “” >> $REPORT
# 5. 检查无主文件
echo “5. Unowned Files” >> $REPORT
echo “—————–” >> $REPORT
find /home -nouser -o -nogroup -ls 2>/dev/null >> $REPORT
echo “” >> $REPORT
# 6. 检查敏感文件权限
echo “6. Sensitive File Permissions” >> $REPORT
echo “——————————” >> $REPORT
ls -l /etc/shadow /etc/gshadow /etc/ssh/sshd_config >> $REPORT
echo “” >> $REPORT
# 7. 检查可写目录
echo “7. World-Writable Directories” >> $REPORT
echo “——————————” >> $REPORT
find /var -type d -perm -002 -ls 2>/dev/null >> $REPORT
echo “” >> $REPORT
# 8. 检查执行权限文件
echo “8. Executable Files in /tmp” >> $REPORT
echo “—————————” >> $REPORT
find /tmp -type f -perm -111 -ls 2>/dev/null >> $REPORT
echo “” >> $REPORT
echo “Audit completed. Report saved to $REPORT”
EOF
# 2. 执行审计脚本
# chmod +x /tmp/permission_audit.sh
# /tmp/permission_audit.sh
Audit completed. Report saved to /tmp/permission_audit_report.txt
# 3. 查看审计报告
# cat /tmp/permission_audit_report.txt
========================================
Permission Audit Report – Fri Apr 2 10:00:00 CST 2026
========================================
1. Files with 777 Permission
—————————–
2. World-Writable Files
————————
3. SUID Programs
—————–
1234567 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/sudo
1234568 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/passwd
1234569 12 -rwsr-xr-x 1 root root 12345 Apr 2 10:00 /usr/bin/su
4. SGID Programs
—————–
1234570 12 -rwxr-sr-x 1 root mail 12345 Apr 2 10:00 /usr/bin/wall
5. Unowned Files
—————–
6. Sensitive File Permissions
——————————
-rw——-. 1 root root 1234 Apr 2 10:00 /etc/gshadow
-rw——-. 1 root root 1234 Apr 2 10:00 /etc/shadow
-rw——-. 1 root root root 1234 Apr 2 10:00 /etc/ssh/sshd_config
7. World-Writable Directories
——————————
8. Executable Files in /tmp
—————————
Part03-生产环境项目实施方案
3.1 chmod数字法修改权限
chmod数字法使用数字表示权限,是最常用的权限修改方法。
# 1. 设置文件权限为644
# touch file.txt
# chmod 644 file.txt
# ls -l file.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 file.txt
# 2. 设置文件权限为755
# chmod 755 file.txt
# ls -l file.txt
-rwxr-xr-x. 1 root root 0 Apr 2 10:00 file.txt
# 3. 设置文件权限为600
# chmod 600 file.txt
# ls -l file.txt
-rw——-. 1 root root 0 Apr 2 10:00 file.txt
# 4. 设置文件权限为700
# chmod 700 file.txt
# ls -l file.txt
-rwx——. 1 root root 0 Apr 2 10:00 file.txt
# 5. 设置目录权限为755
# mkdir dir1
# chmod 755 dir1
# ls -ld dir1
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir1
# 6. 设置目录权限为775
# chmod 775 dir1
# ls -ld dir1
drwxrwxr-x. 2 root root 6 Apr 2 10:00 dir1
# 7. 设置SUID权限
# cp /bin/cat /tmp/mycat
# chmod 4755 /tmp/mycat
# ls -l /tmp/mycat
-rwsr-xr-x. 1 root root 12345 Apr 2 10:00 /tmp/mycat
# 8. 设置SGID权限
# mkdir /tmp/shared
# chmod 2775 /tmp/shared
# ls -ld /tmp/shared
drwxrwsr-x. 2 root root 6 Apr 2 10:00 /tmp/shared
# 9. 设置SBIT权限
# chmod 1777 /tmp/test
# ls -ld /tmp/test
drwxrwxrwt. 2 root root 6 Apr 2 10:00 /tmp/test
# 10. 递归修改目录权限
# mkdir -p /tmp/test/{dir1,dir2,dir3}
# touch /tmp/test/{file1,file2,file3}
# chmod -R 755 /tmp/test
# ls -lR /tmp/test
/tmp/test:
total 0
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir1
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir2
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir3
-rwxr-xr-x. 1 root root 0 Apr 2 10:00 file1
-rwxr-xr-x. 1 root root 0 Apr 2 10:00 file2
-rwxr-xr-x. 1 root root 0 Apr 2 10:00 file3
# 11. 批量修改文件权限
# touch /tmp/{file1,file2,file3}.txt
# chmod 644 /tmp/file*.txt
# ls -l /tmp/file*.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 /tmp/file1.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 /tmp/file2.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 /tmp/file3.txt
# 12. 修改多个文件权限
# chmod 755 file1.txt file2.txt file3.txt
# 13. 使用变量设置权限
# PERM=644
# chmod $PERM file.txt
# 14. 查看权限数字
# stat -c “%a %n” file.txt
644 file.txt
# 15. 复制权限
# chmod –reference=/etc/passwd file.txt
# ls -l file.txt /etc/passwd
-rw-r–r–. 1 root root 0 Apr 2 10:00 file.txt
-rw-r–r–. 1 root root 1234 Apr 2 10:00 /etc/passwd
3.2 chmod符号法修改权限
chmod符号法使用符号表示权限,更加直观易懂。
# 符号说明:
# u – 所有者(user)
# g – 所属组(group)
# o – 其他用户(other)
# a – 所有用户(all)
# + – 添加权限
# – – 移除权限
# = – 设置权限
# 1. 为所有者添加执行权限
# touch file.txt
# chmod u+x file.txt
# ls -l file.txt
-rwxr–r–. 1 root root 0 Apr 2 10:00 file.txt
# 2. 为所属组添加写权限
# chmod g+w file.txt
# ls -l file.txt
-rwxrw-r–. 1 root root 0 Apr 2 10:00 file.txt
# 3. 为其他用户移除读权限
# chmod o-r file.txt
# ls -l file.txt
-rwxrw—-. 1 root root 0 Apr 2 10:00 file.txt
# 4. 为所有用户添加执行权限
# chmod a+x file.txt
# ls -l file.txt
-rwxrwx–x. 1 root root 0 Apr 2 10:00 file.txt
# 5. 设置所有者权限为读写执行
# chmod u=rwx file.txt
# ls -l file.txt
-rwxrwx–x. 1 root root 0 Apr 2 10:00 file.txt
# 6. 设置所属组权限为读写
# chmod g=rw file.txt
# ls -l file.txt
-rwxrw—x. 1 root root 0 Apr 2 10:00 file.txt
# 7. 设置其他用户权限为只读
# chmod o=r file.txt
# ls -l file.txt
-rwxrw-r–. 1 root root 0 Apr 2 10:00 file.txt
# 8. 同时设置多组权限
# chmod u=rwx,g=rx,o=r file.txt
# ls -l file.txt
-rwxr-xr–. 1 root root 0 Apr 2 10:00 file.txt
# 9. 移除所有用户的执行权限
# chmod a-x file.txt
# ls -l file.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 file.txt
# 10. 添加SUID权限
# chmod u+s file.txt
# ls -l file.txt
-rwSr–r–. 1 root root 0 Apr 2 10:00 file.txt
# 11. 添加SGID权限
# chmod g+s file.txt
# ls -l file.txt
-rwSr-Sr–. 1 root root 0 Apr 2 10:00 file.txt
# 12. 添加SBIT权限
# chmod o+t dir1
# ls -ld dir1
drwxr-xr-t. 2 root root 6 Apr 2 10:00 dir1
# 13. 移除SUID权限
# chmod u-s file.txt
# ls -l file.txt
-rw-r-Sr–. 1 root root 0 Apr 2 10:00 file.txt
# 14. 移除SGID权限
# chmod g-s file.txt
# ls -l file.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 file.txt
# 15. 移除SBIT权限
# chmod o-t dir1
# ls -ld dir1
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir1
# 16. 递归修改目录权限
# mkdir -p /tmp/test/{dir1,dir2}
# touch /tmp/test/{file1,file2}
# chmod -R u+rwX,go+rX /tmp/test
# ls -lR /tmp/test
/tmp/test:
total 0
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir1
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir2
-rw-r–r–. 1 root root 0 Apr 2 10:00 file1
-rw-r–r–. 1 root root 0 Apr 2 10:00 file2
# 17. 使用X权限(只对目录或已有执行权限的文件添加执行权限)
# chmod a+X file.txt
# ls -l file.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 file.txt
# 对目录添加执行权限
# chmod a+X dir1
# ls -ld dir1
drwxr-xr-x. 2 root root 6 Apr 2 10:00 dir1
# 18. 复制权限
# chmod –reference=/etc/passwd file.txt
# ls -l file.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 file.txt
# 19. 批量修改权限
# find /tmp -name “*.txt” -exec chmod 644 {} \;
# 20. 组合使用数字和符号
# chmod 644 file.txt
# chmod u+x file.txt
# ls -l file.txt
-rwxr–r–. 1 root root 0 Apr 2 10:00 file.txt
3.3 chmod高级用法
chmod命令的高级用法和技巧。
# 1. 递归修改权限(目录和文件分开处理)
# mkdir -p /tmp/app/{bin,conf,logs,data}
# touch /tmp/app/bin/{app.sh,run.sh}
# touch /tmp/app/conf/{app.conf,db.conf}
# 目录设置为755
# find /tmp/app -type d -exec chmod 755 {} \;
# 文件设置为644
# find /tmp/app -type f -exec chmod 644 {} \;
# 脚本文件设置为755
# find /tmp/app/bin -type f -exec chmod 755 {} \;
# 配置文件设置为600
# find /tmp/app/conf -type f -exec chmod 600 {} \;
# 验证权限
# ls -lR /tmp/app
/tmp/app:
total 0
drwxr-xr-x. 2 root root 6 Apr 2 10:00 bin
drwxr-xr-x. 2 root root 6 Apr 2 10:00 conf
drwxr-xr-x. 2 root root 6 Apr 2 10:00 data
drwxr-xr-x. 2 root root 6 Apr 2 10:00 logs
/tmp/app/bin:
total 0
-rwxr-xr-x. 1 root root 0 Apr 2 10:00 app.sh
-rwxr-xr-x. 1 root root 0 Apr 2 10:00 run.sh
/tmp/app/conf:
total 0
-rw——-. 1 root root 0 Apr 2 10:00 app.conf
-rw——-. 1 root root 0 Apr 2 10:00 db.conf
# 2. 批量修改权限(使用xargs)
# find /tmp -name “*.log” | xargs chmod 644
# 3. 修改权限并保留特殊权限
# chmod 4755 /tmp/app/bin/app.sh
# ls -l /tmp/app/bin/app.sh
-rwsr-xr-x. 1 root root 0 Apr 2 10:00 /tmp/app/bin/app.sh
# 4. 使用ACL设置精细权限
# setfacl -m u:fgedu:rwx /tmp/app/conf/app.conf
# getfacl /tmp/app/conf/app.conf
# file: tmp/app/conf/app.conf
# owner: root
# group: root
user::rw-
user:fgedu:rwx
group::—
mask::rwx
other::—
# 5. 批量设置ACL权限
# setfacl -R -m g:developers:rwx /tmp/app
# 6. 移除ACL权限
# setfacl -b /tmp/app/conf/app.conf
# 7. 检查权限变更
# 使用auditd监控权限变更
# auditctl -w /etc/passwd -p wa -k passwd_changes
# 查看审计日志
# ausearch -k passwd_changes
# 8. 批量修复权限
# cat > /tmp/fix_permissions.sh << 'EOF'
#!/bin/bash
# 修复系统文件权限
chmod 644 /etc/passwd
chmod 600 /etc/shadow
chmod 644 /etc/group
chmod 600 /etc/gshadow
chmod 600 /etc/ssh/sshd_config
# 修复目录权限
find /var/log -type d -exec chmod 755 {} \;
find /var/log -type f -exec chmod 644 {} \;
# 修复临时目录权限
chmod 1777 /tmp
chmod 1777 /var/tmp
echo "Permissions fixed"
EOF
# 9. 权限模板应用
# cat > /tmp/apply_permission_template.sh << 'EOF'
#!/bin/bash
TEMPLATE_DIR="/tmp/template"
TARGET_DIR="/tmp/app"
# 应用权限模板
find $TEMPLATE_DIR -printf "%P\n" | while read file; do
if [ -e "$TARGET_DIR/$file" ]; then
chmod --reference="$TEMPLATE_DIR/$file" "$TARGET_DIR/$file"
echo "Applied permission to $TARGET_DIR/$file"
fi
done
EOF
# 10. 权限备份和恢复
# 备份权限
# getfacl -R /tmp/app > /tmp/app_permissions.acl
# 恢复权限
# setfacl –restore=/tmp/app_permissions.acl
Part04-生产案例与实战讲解
4.1 批量权限管理案例
在生产环境中,经常需要批量管理文件和目录权限。
# 1. 批量设置Web目录权限
# cat > /tmp/setup_web_permissions.sh << 'EOF'
#!/bin/bash
WEB_ROOT="/var/www/html"
# 设置目录权限
find $WEB_ROOT -type d -exec chmod 755 {} \;
# 设置文件权限
find $WEB_ROOT -type f -exec chmod 644 {} \;
# 设置上传目录权限
find $WEB_ROOT/uploads -type d -exec chmod 775 {} \;
find $WEB_ROOT/uploads -type f -exec chmod 664 {} \;
# 设置配置文件权限
find $WEB_ROOT -name "*.conf" -exec chmod 640 {} \;
# 设置脚本文件权限
find $WEB_ROOT -name "*.sh" -exec chmod 750 {} \;
find $WEB_ROOT -name "*.py" -exec chmod 644 {} \;
# 设置缓存目录权限
find $WEB_ROOT/cache -type d -exec chmod 775 {} \;
find $WEB_ROOT/cache -type f -exec chmod 664 {} \;
# 设置日志目录权限
find $WEB_ROOT/logs -type d -exec chmod 775 {} \;
find $WEB_ROOT/logs -type f -exec chmod 664 {} \;
echo "Web permissions configured"
EOF
# 2. 批量设置应用目录权限
# cat > /tmp/setup_app_permissions.sh << 'EOF'
#!/bin/bash
APP_ROOT="/opt/app"
# 创建目录结构
mkdir -p $APP_ROOT/{bin,conf,logs,data,temp}
# 设置所有者
chown -R app:app $APP_ROOT
# 设置目录权限
find $APP_ROOT -type d -exec chmod 755 {} \;
# 设置文件权限
find $APP_ROOT -type f -exec chmod 644 {} \;
# 设置脚本执行权限
find $APP_ROOT/bin -type f -exec chmod 755 {} \;
# 设置配置文件权限
find $APP_ROOT/conf -type f -exec chmod 600 {} \;
# 设置数据目录权限
chmod 750 $APP_ROOT/data
# 设置临时目录权限
chmod 750 $APP_ROOT/temp
# 设置日志目录权限
chmod 750 $APP_ROOT/logs
echo "App permissions configured"
EOF
# 3. 批量修复用户目录权限
# cat > /tmp/fix_home_permissions.sh << 'EOF'
#!/bin/bash
for user in $(awk -F: '$3 >= 1000 && $3 != 65534 {print $1}’ /etc/passwd); do
home_dir=$(awk -F: -v u=$user ‘$1 == u {print $6}’ /etc/passwd)
if [ -d “$home_dir” ]; then
# 设置家目录权限
chmod 700 $home_dir
# 设置SSH目录权限
if [ -d “$home_dir/.ssh” ]; then
chmod 700 $home_dir/.ssh
chmod 600 $home_dir/.ssh/authorized_keys
chmod 600 $home_dir/.ssh/id_rsa
chmod 644 $home_dir/.ssh/id_rsa.pub
chmod 644 $home_dir/.ssh/known_hosts
fi
# 设置其他敏感文件权限
if [ -f “$home_dir/.bash_history” ]; then
chmod 600 $home_dir/.bash_history
fi
echo “Fixed permissions for $user”
fi
done
EOF
# 4. 批量设置共享目录权限
# cat > /tmp/setup_shared_permissions.sh << 'EOF'
#!/bin/bash
SHARED_DIR="/shared"
# 创建共享目录
mkdir -p $SHARED_DIR/{projectA,projectB,projectC}
# 设置SGID权限(新文件继承组)
find $SHARED_DIR -type d -exec chmod 2775 {} \;
# 设置组所有权
chown -R root:developers $SHARED_DIR/projectA
chown -R root:ops $SHARED_DIR/projectB
chown -R root:dba $SHARED_DIR/projectC
# 设置文件权限
find $SHARED_DIR -type f -exec chmod 664 {} \;
echo "Shared permissions configured"
EOF
# 5. 批量设置临时文件清理权限
# cat > /tmp/cleanup_temp_permissions.sh << 'EOF'
#!/bin/bash
# 清理临时文件
find /tmp -type f -mtime +7 -exec rm -f {} \;
find /var/tmp -type f -mtime +30 -exec rm -f {} \;
# 修复临时目录权限
chmod 1777 /tmp
chmod 1777 /var/tmp
# 清理空目录
find /tmp -type d -empty -delete
find /var/tmp -type d -empty -delete
echo "Temp cleanup completed"
EOF
4.2 权限安全加固案例
对系统进行权限安全加固。
# 1. 系统文件权限加固
# cat > /tmp/harden_system_permissions.sh << 'EOF'
#!/bin/bash
echo "Hardening system permissions..."
# 修复关键系统文件权限
chmod 644 /etc/passwd
chmod 600 /etc/shadow
chmod 644 /etc/group
chmod 600 /etc/gshadow
chmod 644 /etc/services
chmod 600 /etc/ssh/sshd_config
chmod 600 /etc/ssh/ssh_config
# 修复系统目录权限
chmod 755 /bin /sbin /usr/bin /usr/sbin
chmod 755 /etc /var /usr
# 修复日志目录权限
chmod 755 /var/log
find /var/log -type f -exec chmod 640 {} \;
# 修复临时目录权限
chmod 1777 /tmp /var/tmp
# 移除不必要的SUID/SGID
chmod -s /usr/bin/wall
chmod -s /usr/bin/write
# 检查并修复
echo "System permissions hardened"
EOF
# 2. 检查并移除危险权限
# cat > /tmp/remove_dangerous_permissions.sh << 'EOF'
#!/bin/bash
# 查找并移除777权限
find / -perm 777 -type f -exec chmod 755 {} \; 2>/dev/null
# 查找并移除其他用户可写文件
find /etc -perm -002 -type f -exec chmod o-w {} \; 2>/dev/null
# 查找并移除其他用户可写目录
find /etc -perm -002 -type d -exec chmod o-w {} \; 2>/dev/null
# 查找并移除无主文件
find /home -nouser -exec chown root:root {} \; 2>/dev/null
find /home -nogroup -exec chgrp root {} \; 2>/dev/null
echo “Dangerous permissions removed”
EOF
# 3. 应用安全权限模板
# cat > /tmp/apply_security_template.sh << 'EOF'
#!/bin/bash
# 定义安全权限模板
declare -A PERMISSIONS=(
["/etc/passwd"]="644"
["/etc/shadow"]="600"
["/etc/group"]="644"
["/etc/gshadow"]="600"
["/etc/ssh/sshd_config"]="600"
["/etc/ssh/ssh_config"]="644"
["/etc/crontab"]="600"
["/etc/cron.d"]="700"
["/etc/cron.daily"]="700"
["/etc/cron.hourly"]="700"
["/etc/cron.monthly"]="700"
["/etc/cron.weekly"]="700"
)
for file in "${!PERMISSIONS[@]}"; do
if [ -e "$file" ]; then
chmod ${PERMISSIONS[$file]} "$file"
echo "Set ${PERMISSIONS[$file]} on $file"
fi
done
echo "Security template applied"
EOF
# 4. 检查SUID/SGID程序
# cat > /tmp/check_suid_sgid.sh << 'EOF'
#!/bin/bash
REPORT="/tmp/suid_sgid_report.txt"
echo "SUID/SGID Programs Report - $(date)" > $REPORT
echo “========================================” >> $REPORT
echo “” >> $REPORT
echo “SUID Programs:” >> $REPORT
find /usr -perm -4000 -type f -ls >> $REPORT
echo “” >> $REPORT
echo “SGID Programs:” >> $REPORT
find /usr -perm -2000 -type f -ls >> $REPORT
echo “” >> $REPORT
echo “SUID+SGID Programs:” >> $REPORT
find /usr -perm -6000 -type f -ls >> $REPORT
echo “Report saved to $REPORT”
EOF
# 5. 完整权限安全加固
# cat > /tmp/full_permission_hardening.sh << 'EOF'
#!/bin/bash
echo "Starting full permission hardening..."
# 1. 系统文件权限加固
./harden_system_permissions.sh
# 2. 移除危险权限
./remove_dangerous_permissions.sh
# 3. 应用安全模板
./apply_security_template.sh
# 4. 检查SUID/SGID
./check_suid_sgid.sh
echo "Full permission hardening completed"
EOF
# chmod +x /tmp/full_permission_hardening.sh
# /tmp/full_permission_hardening.sh
4.3 权限故障排查案例
权限相关故障的排查和解决。
# 案例1:用户无法访问文件
# 症状:用户提示”Permission denied”
# 排查步骤:
# 1. 检查文件权限
# ls -l file.txt
-rw——-. 1 root root 1234 Apr 2 10:00 file.txt
# 2. 检查用户身份
# id username
uid=1000(username) gid=1000(username) groups=1000(username)
# 3. 检查文件所有权
# ls -l file.txt | awk ‘{print $3, $4}’
root root
# 解决:修改文件所有权或权限
# chown username:username file.txt
# 或
# chmod 644 file.txt
# 案例2:用户无法执行脚本
# 症状:用户提示”Permission denied”
# 排查步骤:
# 1. 检查文件权限
# ls -l script.sh
-rw-r–r–. 1 root root 1234 Apr 2 10:00 script.sh
# 2. 检查文件类型
# file script.sh
script.sh: Bourne-Again shell script, ASCII text executable
# 解决:添加执行权限
# chmod +x script.sh
# ls -l script.sh
-rwxr-xr-x. 1 root root 1234 Apr 2 10:00 script.sh
# 案例3:用户无法访问目录
# 症状:用户提示”Permission denied”
# 排查步骤:
# 1. 检查目录权限
# ls -ld /app/data
drwxr-x—. 2 root app 4096 Apr 2 10:00 /app/data
# 2. 检查用户组
# id username
uid=1000(username) gid=1000(username) groups=1000(username)
# 解决:添加用户到组或修改目录权限
# gpasswd -a username app
# 或
# chmod 755 /app/data
# 案例4:SUID程序不生效
# 症状:SUID程序执行时权限不提升
# 排查步骤:
# 1. 检查SUID权限
# ls -l /usr/bin/sudo
-rwsr-xr-x. 1 root root 12345 Apr 2 10:00 /usr/bin/sudo
# 2. 检查SELinux上下文
# ls -lZ /usr/bin/sudo
-rwsr-xr-x. 1 root root system_u:object_r:sudo_exec_t:s0 12345 Apr 2 10:00 /usr/bin/sudo
# 3. 检查文件系统挂载选项
# mount | grep “on / ”
/dev/mapper/rhel-root on / type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
# 解决:检查是否在nosuid文件系统上
# 重新挂载文件系统
# mount -o remount,suid /
# 案例5:SGID目录不继承组
# 症状:新文件不继承目录组
# 排查步骤:
# 1. 检查目录权限
# ls -ld /shared
drwxrwsr-x. 2 root developers 4096 Apr 2 10:00 /shared
# 2. 检查新文件权限
# touch /shared/test.txt
# ls -l /shared/test.txt
-rw-r–r–. 1 username username 0 Apr 2 10:00 /shared/test.txt
# 解决:检查用户主组
# id username
uid=1000(username) gid=1000(username) groups=1000(username),1001(developers)
# SGID应该生效,检查文件系统
# mount | grep /shared
# 案例6:临时文件被删除
# 症状:/tmp下的文件被其他用户删除
# 排查步骤:
# 1. 检查/tmp权限
# ls -ld /tmp
drwxrwxrwt. 10 root root 4096 Apr 2 10:00 /tmp
# 2. 检查SBIT权限
# stat /tmp | grep “Access: (”
Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)
# SBIT已设置,检查文件权限
# ls -l /tmp/file.txt
-rw-r–r–. 1 username username 1234 Apr 2 10:00 /tmp/file.txt
# 解决:SBIT只防止删除其他用户的文件,检查是否是同一用户删除
# 案例7:ACL权限不生效
# 症状:ACL设置的权限不生效
# 排查步骤:
# 1. 检查ACL权限
# getfacl file.txt
# file: file.txt
# owner: root
# group: root
user::rw-
user:fgedu:rwx
group::r–
mask::rwx
other::r–
# 2. 检查文件系统是否支持ACL
# tune2fs -l /dev/mapper/rhel-root | grep “Default mount options”
Default mount options: user_xattr acl
# 3. 检查SELinux
# getenforce
Enforcing
# 解决:检查SELinux上下文
# ls -lZ file.txt
-rw-r–r–. 1 root root unconfined_u:object_r:admin_home_t:s0 1234 Apr 2 10:00 file.txt
# 案例8:权限修改失败
# 症状:chmod命令执行失败
# 排查步骤:
# 1. 检查文件系统是否只读
# mount | grep “on / ”
/dev/mapper/rhel-root on / type xfs (ro,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
# 解决:重新挂载为读写
# mount -o remount,rw /
# 案例9:特殊权限显示异常
# 症状:SUID/SGID显示为大写S
# 排查步骤:
# 1. 检查文件权限
# ls -l file.txt
-rwSr–r–. 1 root root 1234 Apr 2 10:00 file.txt
# 大写S表示SUID已设置但没有执行权限
# 解决:添加执行权限
# chmod u+x file.txt
# ls -l file.txt
-rwsr–r–. 1 root root 1234 Apr 2 10:00 file.txt
# 案例10:权限继承问题
# 症状:新创建的文件权限不符合预期
# 排查步骤:
# 1. 检查umask
# umask
0022
# 2. 检查默认权限
# touch test.txt
# ls -l test.txt
-rw-r–r–. 1 root root 0 Apr 2 10:00 test.txt
# 解决:修改umask
# umask 0002
# touch test2.txt
# ls -l test2.txt
-rw-rw-r–. 1 root root 0 Apr 2 10:00 test2.txt
Part05-风哥经验总结与分享
5.1 权限管理最佳实践
# 1. 权限设置原则
– 最小权限原则
– 避免使用777权限
– 定期审计权限
– 及时回收权限
– 使用ACL精细控制
# 2. 文件权限规范
– 配置文件:600/640
– 脚本文件:700/750
– 数据文件:600/640
– 日志文件:640/660
– 临时文件:600
# 3. 目录权限规范
– 系统目录:755
– 应用目录:755/750
– 数据目录:750/700
– 日志目录:750/770
– 临时目录:1777
# 4. 特殊权限使用
– SUID:仅用于必要程序
– SGID:用于共享目录
– SBIT:用于公共临时目录
# 5. 权限审计规范
– 定期检查777权限文件
– 定期检查SUID/SGID程序
– 定期检查可写文件
– 定期检查无主文件
– 定期检查敏感文件权限
5.2 常见问题与解决
# 问题1:权限过于宽松
# 解决:收紧权限
# chmod 644 file.txt
# chmod 755 directory
# 问题2:权限过于严格
# 解决:适当放宽权限
# chmod 640 file.txt
# chmod 750 directory
# 问题3:SUID程序安全风险
# 解决:移除不必要的SUID
# chmod -s /usr/bin/program
# 问题4:共享目录权限混乱
# 解决:使用SGID和ACL
# chmod 2775 /shared
# setfacl -R -m g:developers:rwx /shared
# 问题5:临时文件安全问题
# 解决:使用SBIT和适当权限
# chmod 1777 /tmp
# 在/tmp创建文件时设置适当权限
# install -m 600 /dev/null /tmp/sensitive_file
# 问题6:权限继承问题
# 解决:使用umask和SGID
# umask 002
# chmod 2775 /shared
# 问题7:ACL权限冲突
# 解决:理解ACL权限优先级
# ACL权限 > 组权限 > 其他用户权限
# 问题8:SELinux权限问题
# 解决:检查并修复SELinux上下文
# restorecon -Rv /path/to/file
# 问题9:权限修改失败
# 解决:检查文件系统和所有权
# mount -o remount,rw /
# chown user:group file
# 问题10:权限审计困难
# 解决:使用自动化脚本
# 定期运行权限审计脚本
# 建立权限变更日志
5.3 权限管理工具推荐
- 基本工具:chmod, chown, chgrp, umask
- ACL工具:setfacl, getfacl
- 特殊权限:find, stat
- 审计工具:auditctl, ausearch
- SELinux:chcon, restorecon, semanage
# 1. 查看权限详细信息
# stat file.txt
# stat -c “%a %A %U:%G %n” file.txt
644 -rw-r–r– root:root file.txt
# 2. 批量修改权限
# find /path -type f -exec chmod 644 {} \;
# find /path -type d -exec chmod 755 {} \;
# 3. 设置ACL权限
# setfacl -m u:username:rwx file.txt
# setfacl -m g:groupname:rx file.txt
# getfacl file.txt
# 4. 查找特殊权限文件
# find / -perm -4000 -type f -ls
# find / -perm -2000 -type f -ls
# find / -perm -1000 -type d -ls
# 5. 监控权限变更
# auditctl -w /etc/passwd -p wa -k passwd_changes
# ausearch -k passwd_changes
# 6. SELinux上下文管理
# ls -lZ file.txt
# chcon -t httpd_sys_content_t file.txt
# restorecon -v file.txt
# 7. 权限备份和恢复
# getfacl -R /path > permissions.acl
# setfacl –restore=permissions.acl
# 8. 权限比较
# diff <(getfacl file1.txt) <(getfacl file2.txt)
# 9. 权限模板
# install -m 644 -o root -g root template.conf /etc/app/app.conf
# 10. 权限验证
# test -r file.txt && echo "Readable"
# test -w file.txt && echo "Writable"
# test -x file.txt && echo "Executable"
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
