chmod命令用来改变文件或者目录的权限,只有文件的属主和超级用户才能够执行这个命令

格式:

chmod [option] [mode] [file]

>常用参数选项 -R : 递归修改目录以及子目录下面的所有文件权限

>模式有两种格式,一种采用字母方式的表达式,另外一种是数字

1,首先需要了解文件的权限和属主和属组。

ghostwu@dev:~/linux/chown$ ls -l
total
-rw-rw-r-- ghostwu ghostwu 5月 : test.txt

-rw-rw-r-- 这个就是文件的权限, 除去第一位, 一共有9位组成

第一位表示文件类型,- 表示这个是一个常规文件

后面9位,每3位一组.  第一个3位rw-表示属主权限, 第二个3位rw-表示属组权限,第三个3位r--表示其他用户权限,

后面有2个ghostwu,  第一个ghostwu, 表示属主, 也就是这个test.txt文件的拥有者是ghostwu

第二个ghostwu,表示属组,也就是这个test.txt文件可以被ghostwu这个组的用户 rw-( 可读,可写)

2,修改文件权限

>增加权限( + )

ghostwu@dev:~/linux/chown$ chmod a+x test.txt
ghostwu@dev:~/linux/chown$ ls
test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-rwxrwxr-x ghostwu ghostwu 5月 : test.txt

a等价于 用户(u)+组(g)+其他组( o )。 a+x 就是给用户,组,其他组都加上x(可执行)权限

>去掉权限( - )

ghostwu@dev:~/linux/chown$ chmod a-x test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-rw-rw-r-- ghostwu ghostwu 5月 : test.txt

>设置权限( = )

ghostwu@dev:~/linux/chown$ chmod a=r-- test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-r--r--r-- ghostwu ghostwu 5月 : test.txt

>给属主加上w( 可写 ), x( 可执行 ) 权限

ghostwu@dev:~/linux/chown$ chmod u+wx test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-rwxr--r-- ghostwu ghostwu 5月 : test.txt

>给组加上wx权限

ghostwu@dev:~/linux/chown$ ls -l
total
-rwxr--r-- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ chmod g+wx test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-rwxrwxr-- ghostwu ghostwu 5月 : test.txt

>给其他组加上wx权限

ghostwu@dev:~/linux/chown$ ls -l
total
-rwxrwxr-- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ chmod o+wx test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-rwxrwxrwx ghostwu ghostwu 5月 : test.txt

>r( 4 ), w( 2 ), x( 1 )

ghostwu@dev:~/linux/chown$ ls -l
total
-rwxrwxrwx ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ chmod test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-r--r--r-- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-r--r--r-- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ chmod test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-rwxr-xr-x ghostwu ghostwu 5月 : test.txt

权限详解:

一、普通文件

可读r: 读取/阅读文件内容的权限

ghostwu@dev:~/linux/chown$ ls -l
total
-rwxr-xr-x ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ chmod test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
---------- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ cat test.txt
cat: test.txt: Permission denied
ghostwu@dev:~/linux/chown$ chmod test.txt
ghostwu@dev:~/linux/chown$ ls -l
total
-r-------- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ cat test.txt
this is a test file

可写(w):具有新增,修改文件内容的权限

ghostwu@dev:~/linux/chown$ ls -l test.txt
-r-------- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ echo 'aaa' > test.txt
bash: test.txt: Permission denied
ghostwu@dev:~/linux/chown$ chmod u+w test.txt
ghostwu@dev:~/linux/chown$ ls -l test.txt
-rw------- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ echo 'ghostwu' >> 'test.txt'
ghostwu@dev:~/linux/chown$ cat test.txt
this is a test file
ghostwu

可执行( x )

1,文件本身要用x权限

ghostwu@dev:~/linux/chown$ ls -l test.txt
-rw------- ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ ./test.txt
bash: ./test.txt: Permission denied
ghostwu@dev:~/linux/chown$ chmod u+x test.txt
ghostwu@dev:~/linux/chown$ ls -l test.txt
-rwx------ ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ ./test.txt
./test.txt: line : this: command not found
./test.txt: line : ghostwu: command not found
ghostwu@dev:~/linux/chown$ echo 'ls /' > test.sh
ghostwu@dev:~/linux/chown$ ls -l
total
-rw-rw-r-- ghostwu ghostwu 5月 : test.sh
-rwx------ ghostwu ghostwu 5月 : test.txt
ghostwu@dev:~/linux/chown$ ./test.sh
bash: ./test.sh: Permission denied
ghostwu@dev:~/linux/chown$ chmod u+x test.sh
ghostwu@dev:~/linux/chown$ ls -l test.sh
-rwxrw-r-- ghostwu ghostwu 5月 : test.sh
ghostwu@dev:~/linux/chown$ ./test.sh
bin dev initrd.img lost+found opt run srv usr
boot etc lib media proc sbin sys var
cdrom home lib64 mnt root snap tmp vmlinuz

 普通用户需要拥有r权限, 然后x权限 才能执行

ghostwu@dev:~/linux/chown$ ls -l test.sh
-rwxrw-r-- ghostwu ghostwu 5月 : test.sh
ghostwu@dev:~/linux/chown$ chmod u-r test.sh
ghostwu@dev:~/linux/chown$ ls -l test.
ls: cannot access 'test.': No such file or directory
ghostwu@dev:~/linux/chown$ ls -l test.sh
--wxrw-r-- ghostwu ghostwu 5月 : test.sh
ghostwu@dev:~/linux/chown$ ./test.sh
bash: ./test.sh: Permission denied

root用户不需要r权限,只要有x权限就能执行

root@dev:/home/ghostwu/linux/chown# ls -l test.sh
--wxrw-r-- ghostwu ghostwu 5月 : test.sh
root@dev:/home/ghostwu/linux/chown# ./test.sh
bin dev initrd.img lost+found opt run srv usr
boot etc lib media proc sbin sys var
cdrom home lib64 mnt root snap tmp vmlinuz
root@dev:/home/ghostwu/linux/chown# chmod a-x test.sh
root@dev:/home/ghostwu/linux/chown# ls -l test.sh
--w-rw-r-- ghostwu ghostwu 5月 : test.sh
root@dev:/home/ghostwu/linux/chown# ./test.sh
-su: ./test.sh: Permission denied
root@dev:/home/ghostwu/linux/chown# chmod o+x test.sh
root@dev:/home/ghostwu/linux/chown# ls -l test.sh
--w-rw-r-x ghostwu ghostwu 5月 : test.sh
root@dev:/home/ghostwu/linux/chown# ./test.sh
bin dev initrd.img lost+found opt run srv usr
boot etc lib media proc sbin sys var
cdrom home lib64 mnt root snap tmp vmlinuz

二、目录权限

可读r: 具有浏览目录下面文件及其子目录的权限,即:ls 目录

ghostwu@dev:~/linux$ ls -l
total
drwxrwxr-x ghostwu ghostwu 5月 : chown
drwxr-xrwx root root 5月 : cp
drwxrwxr-x ghostwu ghostwu 5月 : rename
ghostwu@dev:~/linux$ ls chown
test.sh test.txt
ghostwu@dev:~/linux$ chmod u-r chown
ghostwu@dev:~/linux$ ls -l chown
ls: cannot open directory 'chown': Permission denied
ghostwu@dev:~/linux$ ls -l
total
d-wxrwxr-x ghostwu ghostwu 5月 : chown

没有x权限,不能cd切换到目录

ghostwu@dev:~/linux$ ls -l
total
d-wxrwxr-x ghostwu ghostwu 5月 : chown
drwxr-xrwx root root 5月 : cp
drwxrwxr-x ghostwu ghostwu 5月 : rename
ghostwu@dev:~/linux$ cd chown
ghostwu@dev:~/linux/chown$ ls
ls: cannot open directory '.': Permission denied
ghostwu@dev:~/linux/chown$ cd ..
ghostwu@dev:~/linux$ ls -l
total
d-wxrwxr-x ghostwu ghostwu 5月 : chown
drwxr-xrwx root root 5月 : cp
drwxrwxr-x ghostwu ghostwu 5月 : rename
ghostwu@dev:~/linux$ chmod u-x chown
ghostwu@dev:~/linux$ ls -l
total
d-w-rwxr-x ghostwu ghostwu 5月 : chown
drwxr-xrwx root root 5月 : cp
drwxrwxr-x ghostwu ghostwu 5月 : rename
ghostwu@dev:~/linux$ cd chown
-su: cd: chown: Permission denied

w: 具有增加,删除或者修改目录内文件名的权限,需要x权限配合

ghostwu@dev:~/linux$ ls -l
total
d-w-rwxr-x ghostwu ghostwu 5月 : chown
drwxr-xrwx root root 5月 : cp
drwxrwxr-x ghostwu ghostwu 5月 : rename
ghostwu@dev:~/linux$ cd chown
-su: cd: chown: Permission denied
ghostwu@dev:~/linux$ chmod u+x chown
ghostwu@dev:~/linux$ ls -l
total
d-wxrwxr-x ghostwu ghostwu 5月 : chown
drwxr-xrwx root root 5月 : cp
drwxrwxr-x ghostwu ghostwu 5月 : rename
ghostwu@dev:~/linux$ cd chown
ghostwu@dev:~/linux/chown$ ls -l
ls: cannot open directory '.': Permission denied
ghostwu@dev:~/linux/chown$ touch a.txt
ghostwu@dev:~/linux/chown$ ls -l .
ls: cannot open directory '.': Permission denied
ghostwu@dev:~/linux/chown$ ls -l a.txt
-rw-rw-r-- ghostwu ghostwu 5月 : a.txt

如果父目录没有w权限,是不能删除目录下面的文件的

ghostwu@dev:~/linux/chown$ ls -l a.txt
-rw-rw-r-- ghostwu ghostwu 5月 : a.txt
ghostwu@dev:~/linux/chown$ rm -f a.txt
ghostwu@dev:~/linux/chown$ ls -l a.txt
ls: cannot access 'a.txt': No such file or directory
ghostwu@dev:~/linux/chown$ touch a.txt
ghostwu@dev:~/linux/chown$ cd ..
ghostwu@dev:~/linux$ ls -l
total
d-wxrwxr-x ghostwu ghostwu 5月 : chown
drwxr-xrwx root root 5月 : cp
drwxrwxr-x ghostwu ghostwu 5月 : rename
ghostwu@dev:~/linux$ chmod u-w chown
ghostwu@dev:~/linux$ ls -l chown/a.txt
-rw-rw-r-- ghostwu ghostwu 5月 : chown/a.txt
ghostwu@dev:~/linux$ rm -f chown/a.txt
rm: cannot remove 'chown/a.txt': Permission denied

x: 具有进入目录的权限:如cd dir

>没有r无法列表

>没有w无法新建文件

最新文章

  1. 白话Https
  2. ansible的使用技巧
  3. C#的Attribute
  4. Fragment 操作原理
  5. crm plugin 未能加载文件或程序集“xxxx,”或它的某一个依赖项。系统找不到指定的文件。
  6. 2dx关于js响应layer触摸消息的bug
  7. SecureCRT+WinSCP 共用 key pub 密钥 转换 ppk 登录ssh
  8. Linux下开启MySQL的远程连接
  9. DM8168硬件平台
  10. angularjs-1.3代码学习-$parse
  11. Spring+SpringMVC+MyBatis整合进阶篇(四)RESTful实战(前端代码修改)
  12. Opengl4.5 中文手册—G
  13. 目录导航「深入浅出ASP.NET Core系列」
  14. 【原创】讲讲亿级PV的负载均衡架构
  15. SSM框架的sql中参数注入(#和$的区别)
  16. 转发:CentOS下tar压缩排除某个文件夹或文件及解压
  17. Redis 分布式缓存 Java 框架
  18. CentOS6.4下Mysql数据库的安装与配置
  19. JavaScript学习 - 基础(八) - DOM 节点 添加/删除/修改/属性值操作
  20. VSCode代码修改后跑起来没反应,打开本地文件,代码没变化

热门文章

  1. 面向对象多继承(c3算法)、网络基础和编写网络相关的程序
  2. canvas制作完美适配分享海报
  3. git fetch 、git pull 与 git pull --rebase
  4. Shell - 简明Shell入门11 - 调用脚本(CallTheScript)
  5. 机器学习基石笔记:14 Regularization
  6. One difference between AngularJS' $location and window.location
  7. C# 添加修改防火墙端口及程序
  8. Numpy.random中shuffle与permutation的区别(转)
  9. Mahout使用(一)
  10. IE中透明度的读写