LOADING...

加载中请稍等...

loading

Linux每日一篇 - 15 - chmod

Linux权限管理神器!掌握chmod命令,轻松控制文件访问权限,让系统安全更有保障!

chmod命令是什么?

chmod是”Change Mode”的缩写,用于修改文件或目录的访问权限,是Linux系统安全的重要组成部分。

基本用法

# 使用数字模式修改权限
chmod 755 filename

# 使用符号模式修改权限
chmod u+x filename  # 给文件所有者添加执行权限

# 递归修改目录权限
chmod -R 755 directory/

# 复制权限(从其他文件)
chmod --reference=file1 file2

# 常见权限数字说明
# 755: rwxr-xr-x (所有者可读写执行,组和其他用户可读执行)
# 644: rw-r--r-- (所有者可读写,组和其他用户只读)
# 600: rw------- (只有所有者可读写)

实用技巧

# 为所有者添加执行权限
chmod u+x script.sh

# 移除其他用户的所有权限
chmod o= file.txt

# 为用户组添加写权限
chmod g+w directory/

# 递归设置目录权限
chmod -R 755 /path/to/directory/

# 仅修改目录权限(不包括文件)
find /path -type d -exec chmod 755 {} \;

# 仅修改文件权限(不包括目录)
find /path -type f -exec chmod 644 {} \;

# 设置特殊权限位
chmod u+s file  # 设置SUID
chmod g+s dir   # 设置SGID
chmod +t dir    # 设置粘滞位(sticky bit)

常用场景

# 让脚本可执行
chmod +x myscript.sh

# 设置安全的配置文件权限
chmod 600 /etc/myapp/config

# 设置Web目录权限
chmod -R 755 /var/www/html/

# 设置日志文件权限
chmod 644 /var/log/myapp.log

# 为用户组设置共享目录权限
chmod -R g+rw /shared/directory/
chmod g+s /shared/directory/  # 设置SGID继承

# 临时提升文件权限
chmod 666 sensitive_file  # 允许所有用户读写

关于我

全平台同名”汪多多是只猫”,专注分享实用技术教程,让你的IT学习之路更轻松!

关注我,每天一个Linux命令,轻松入门Linux系统!

头像
汪多多是只猫
失业的运维工程师
热爱开源与分享
微信公众号