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无法新建文件

Linux常用基本命令(chmod)的更多相关文章

  1. Linux常用基本命令(less)

    转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...

  2. Linux 常用基本命令及应用技巧

    需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...

  3. Linux常用基本命令[cp]

    cp:复制文件或者目录 用法格式: cp [option] [source] [dest] cp [选项] [源文件] [目标文件] >用root账户,创建文件,复制文件 root@dev:/h ...

  4. 【Linux】linux常用基本命令(转)

    (转自:http://blog.csdn.net/xiaoguaihai/article/details/8705992) Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用 ...

  5. 【Linux】linux常用基本命令

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们.   这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显示日 ...

  6. linux常用基本命令

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们.   系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器 ...

  7. linux常用基本命令整理小结

    linux系统遵循的基本原则 由目标单一的小程序组成,组合小程序完成复杂任务: 一切皆文件: 尽量避免捕捉用户接口: 配置文件保存为纯文本文件: Linux命令行常识 命令格式 命令+选项+参数 选项 ...

  8. Linux 常用基本命令

    这两天有俩哥们问了我linux的事,问我在工作中需不需要用到,需不需要学会 一个是工作1年不到的,我跟他说,建议你学学,在以后肯定是要用到的,虽然用到的机会不多,但是会总比不会好 另一个是工作6年的, ...

  9. Linux常用基本命令:三剑客命令之-awk内置函数用法

    awk的内置函数大致可以分类为算数函数.字符串函数.时间函数.其他函数等 算数函数 最常用的算数函数有rand函数.srand函数.int函数. 可以使用rand函数生成随机数,但是使用rand函数时 ...

随机推荐

  1. [转]高端又易学的vbs表白程序了解一下

    第一个. 打开txt文件,复制以下代码粘贴进去(可以修改中文部分,其它代码不要动!).保存并关闭txt文件. msgbox("做我女朋友好吗?") msgbox("房产证 ...

  2. Python基础 --函数的参数

    定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解 ...

  3. 初识node.js(通过npm下载项目依赖的包的过程)

    一.初识node.js 简单的说Node.js 就是运行在服务器端的JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事 ...

  4. mysql查询语句分析 explain/desc用法

    explain或desc显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. explain 数据表 或 desc 数据表 显示数据表各字段含义 ...

  5. 同一域名基于源ip地址分配给不同ip的dns配置--bind9.8.2

    1.安装bind.略. 2./etc/named.conf配置文件. // named.conf // // Provided by Red Hat bind package to configure ...

  6. 抓取分析网页批量下载评书(3)之批量下载mp3

         本系列目录:    <1.搜索有声小说>    <2.分析详细页地址>     <3.批量下载mp3>      本篇是大结局,看过前两篇的放心吧,不会有 ...

  7. cglib invoke 和 invokeSuper 可用的组合

    在深入字节码理解invokeSuper无限循环的原因中,我们理解的cglib的原理和其中一个合理的调用方式.但是这个调用方式是基于类的,对所有实例生效.实际场景中,我们可能只是希望代理某个具体的实例, ...

  8. [Umbraco] Data Types介绍

    Data Types是在建立document type时需要用到的,系统自带了很多用于开发的类型,如常用的下拉列表dropdown, textbox, radiobox, checkbox以及上传,h ...

  9. 让机器说话(文字转美女语音,擅长中英文哦),大小600K(免费下载)!

    机器人之路的第二小步:说话(文字转语音美女哦),大小600K(免费下载)! 机器人之路的第二小步:说话(文字转语音美女哦),准确率特别高,普通话标准,中英文都可以说,大家可以体验一下,请下载到电脑上在 ...

  10. dbvisulizer 存储过程

    --/ CREATE PROCEDURE test () BEGIN DECLARE v CHAR(10) DEFAULT 'Hello';SELECT CONCAT(v, ', ', current ...