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. js判断是否手机自动跳转移动端

    写法一: {literal} <script> //判断是否手机自动跳转 var browser={versions:function(){var u=navigator.userAgen ...

  2. [NOI2017]蔬菜(贪心)

    神仙题啊! 早上开了两个多小时,终于肝出来了,真香 我们考虑从第 \(10^5\) 天开始递推,先生成 \(p=10^5\) 的解,然后逐步推出 \(p-1,...,2,1\) 的解. 那怎么推出 \ ...

  3. java 路径的问题

    在项目开发中会碰到各种各样的获取项目路径的一些问题: 1:java项目: 以获取  类路径下的mess.properties 为例来说明: 文件在项目中的位置: src/bz/beppe/demo/r ...

  4. C# Windows Service中执行死循环轮询

    用C#编写Windows Service时,执行轮询一般有两种方式,一种是用Timer,System.Timers或者是System.Thread下的,这种执行是按时间循环执行,缺点是也许上个执行还没 ...

  5. Golang 中哪些值是不可以寻址的

    不可以寻址, 指的是不能通过&获得其地址. golang中不能寻址的可以总结为:不可变的,临时结果和不安全的.只要符合其中任何一个条件,它就是不可以寻址的. 具体为: 常量的值. 基本类型值的 ...

  6. C++ Enum 转 Lua Table工具

    C++ Enum转Lua Table工具 观察C++ Enum结构 总结结构 enum GameMessage { //******* ///****************** GM_GAMESER ...

  7. [工具]Tomcat CVE-2017-12615 远程代码执行

    工具: K8_TocmatExp编译: VS2012  C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.b ...

  8. 剑指offer十七之树的子结构

    一.题目 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 二.思路 1.首先设置标志位result = false,因为一旦匹配成功result就设为tru ...

  9. c++程序时间统计

    如下所示,引入<time.h>我们就可以统计时间了: #include<iostream> #include<time.h> #include<windows ...

  10. Configuration problem: Failed to import bean definitions from relative location

    问题现象: 最近开始做新需求,然后在Tomcat上部署项目时,出现了如下报错: [12-05 09:54:27,161 ERROR] ContextLoader.java:351 - Context ...