chmod

修改文件或目录的权限

语法

chmod [OPTION]... MODE[,MODE]... FILE...

参数

参数说明示例级别
-R --recursive 递归修改目录下所有文件 chmod -R 755 dir/ 常用
755 所有者rwx,组和其他rx chmod 755 script.sh 常用
644 所有者rw,组和其他r chmod 644 config.txt 常用
+x 添加执行权限 chmod +x deploy.sh 常用
u+w 给所有者添加写权限 chmod u+w file.txt 进阶

示例

让脚本可执行

chmod +x deploy.sh
最常用的操作

标准文件权限

chmod 644 index.html
所有者读写,其他人只读

标准目录权限

chmod 755 public/
所有者全部,其他人读和执行

递归修改

chmod -R 755 /var/www/html/

移除所有人的写权限

chmod a-w important.conf
保护配置文件

常见错误

chmod: changing permissions of 'xxx': Operation not permitted 需要 sudo 或文件所有者权限

技巧

相关命令