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. Linux系统Bash的常用功能(9)

    了解了基本的Linux文件文件系统的概念后,我们将更深入的了解一下Linux的其他方面的内容,那就是我们所使用的用户接口,也就是大家常听到的 『Shell』 ,『这个shell并不是黑客反弹的shel ...

  2. MariaDB 单表查询与聚合(5)

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...

  3. 预处理命令使用详解----#if、#endif、#undef、#ifdef、#else、#elif

    预处理命令 在接触#if.#undef这类预处理指令前,大部分都都接触过#define.#include等预处理命令,通俗来讲预处理命令的作用就是在编译和链接之前,对源文件进行一些文本方面的操作,比如 ...

  4. GoLang学习控制语句之if/else

    if语句 if 是用于测试某个条件(布尔型或逻辑型)的语句,如果该条件成立,则会执行 if 后由大括号括起来的代码块,否则就忽略该代码块继续执行后续的代码. if condition { // do ...

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

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

  6. MySQL数据导入到Mongo

    背景:如题干所述,需要将一份数据导入到mongo数据库,减少项目依赖的数据源. 解决方案: 使用mongo自带的mongoimport工具. 首先在test库里创建一个空集合:[import_test ...

  7. ListView中的TextView实现跑马灯效果

    1.TextView首先添加android:ellipsize="marquee"   android:marqueeRepeatLimit="marquee_forev ...

  8. 记住,永远不要在MySQL中使用“utf8”编码[转载]

    记住,永远不要在MySQL中使用“utf8”编码 原创: 无明.Adam 聊聊架构 6月15日 最近工作中我遇到了一个 bug,我试着通过 Rails 在以“utf8”编码的 MariaDB 中保存一 ...

  9. redisTemplate实现轻量级消息队列, 异步处理excel并实现腾讯云cos文件上传下载

    背景 公司项目有个需求, 前端上传excel文件, 后端读取数据.处理数据.返回错误数据, 最简单的方式同步处理, 客户端上传文件后一直阻塞等待响应, 但用户体验无疑很差, 处理数据可能十分耗时, 没 ...

  10. PHP多进程系列笔记(二)

    上一篇文章讲解了pcntl_fork和pcntl_wait两个函数的使用,本篇继续讲解PHP多进程相关新知识. 僵尸(zombie)进程 这里说下僵尸进程: 僵尸进程是指的父进程已经退出,而该进程de ...