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. .NET Core 常用加密和Hash工具NETCore.Encrypt

    前言 在日常开发过程中,不可避免的涉及到数据加密解密(Hash)操作,所以就有想法开发通用工具,NETCore.Encrypt就诞生了.目前NETCore.Encrypt只支持.NET Core ,工 ...

  2. 【文档】使用Sphinx + reST编写文档

    0 前言 写文档是开发人员日常工作中的一项重要内容,除了word之外,我更偏爱使用标记语言(Markup Language).使用标记语言,可以利用简单.免费的文本编辑器(记事本,vim, emacs ...

  3. 机器学习基石笔记:14 Regularization

    一.正则化的假设集合 通过从高次多项式的H退回到低次多项式的H来降低模型复杂度, 以降低过拟合的可能性, 如何退回? 通过加约束条件: 如果加了严格的约束条件, 没有必要从H10退回到H2, 直接使用 ...

  4. Python小白学习之路(二十五)—【装饰器的应用】

    通过一个任务来加深对装饰器的理解和应用 回顾:装饰器的框架 def timmer(func): def wrapper(): func() return wrapper 任务:给以下正在运行的程序加一 ...

  5. C++ Enum 转 Lua Table工具

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

  6. [0day]微软VS全版本DLL却持漏洞(VS2015 VS2013 VS2012 VS2010 VS2008)

    <无敌破坏王>大师兄说的 "我不是针对谁,而是在座的各位,都是垃圾"前几天在国外论坛看到一个VS2010 DLL却持漏洞 测试发现是全版本 实际上2014年在某越南黑客 ...

  7. sql 导入导出表数据 命令

    那么在我们使用BCP命令之前,我们首先要在Sql Server数据库中执行下列语句,以修改Sql Server的配置,启用对BCP命令的支持. --允许配置高级选项 exec sp_configure ...

  8. C#:struct的陷阱:无法修改“xxx”的返回值,因为它不是变量

    示例代码如下: public struct SpiderResult { public string robotName; public string RobotName { get { return ...

  9. spring cloud(服务消费者(利用ribbon实现服务消费及负载均衡)——初学二)

    Ribbon是一个基于HTTP和TCP客户端的负载均衡器,利用ribbon实现服务消费,并实现客户端的负载均衡. 一.准备工作(利用上一节的内容) 启动服务注册中心 启动computer-servic ...

  10. Python+selenium+webdriver 安装与环境配置

    1.python安装:访问python.org/download,下载最新版本,安装过程与其他windows软件类似.记得下载后设置path环境变量,然后Windows命令行就可以调用: 2.Sele ...