一、权限位

权限位分为两个部分,第一个部分是谁的权限,第二部分是权限是多少。其中第一个部分一般分为:用户,用户组,其他用户。第二部分分为r:读权限,w:写权限,x:执行权限。可读,可写,可执行的权限,用数字表示分别是:421。

用一段代码来说明:

[root@iZ25lzba47vZ ~]# ls -ld
drwxr-xr-x root root Oct :

rwxr-xr-x:表示权限位的值。

root root 分别代表文件的所有者,和所属组。那么他们的权限分别是多少呢?只需要把rwxr-xr-x每三个字母切割开来就是了:

root:rwx root用户的权限是可读可写可执行。

root:r-x root组织有读和执行的权限。

other:r-x 其他用户只有读和执行的权限。

上面这个目录1的权限用数字表示是:755。

可读,可写,可执行对目录的意义:

[ruanwenwu@iZ25lzba47vZ ~]$ mkdir test
[ruanwenwu@iZ25lzba47vZ ~]$ ls
test
[ruanwenwu@iZ25lzba47vZ ~]$ touch /test/.txt
touch: cannot touch ‘/test/.txt’: No such file or directory
[ruanwenwu@iZ25lzba47vZ ~]$ touch test/.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls test
.txt
[ruanwenwu@iZ25lzba47vZ ~]$ chmod test
[ruanwenwu@iZ25lzba47vZ ~]$ ls test
ls: cannot access test/.txt: Permission denied
.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
drw-rw-rw- ruanwenwu ruanwenwu Oct : test
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l test
ls: cannot access test/.txt: Permission denied
total
-????????? ? ? ? ? ? .txt
[ruanwenwu@iZ25lzba47vZ ~]$ cd test/
bash: cd: test/: Permission denied
[ruanwenwu@iZ25lzba47vZ ~]$ chmod test
[ruanwenwu@iZ25lzba47vZ ~]$ ls test/
.txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -ld test/
drwxrwxrwx ruanwenwu ruanwenwu Oct : test/

以上代码说明:

1.切换到ruanwenwu普通用户。

2.在家目录下建立test目录。

3.在test目录下简历1.txt文件。

4.修改test目录为644的权限。

5.ls test发现下面的1.txt为乱码,切提示“Permission denied”。这里说明,我们已经不能看到test目录下的1.txt了。

6.ls -ld test发现一切正常。说明有r的权限,目录就可以用ls -ld看的。

7.cd test/ 发现也是不允许的。

可执行,对于目录的意义,就是可不可以往目录里增减文件:

[ruanwenwu@iZ25lzba47vZ ~]$ chmod  test
[ruanwenwu@iZ25lzba47vZ ~]$ mv .txt test/
mv: cannot stat ‘.txt’: No such file or directory
[ruanwenwu@iZ25lzba47vZ ~]$ ls
test
[ruanwenwu@iZ25lzba47vZ ~]$ touch .txt
[ruanwenwu@iZ25lzba47vZ ~]$ mv .txt test/
mv: cannot move ‘.txt’ to ‘test/.txt’: Permission denied

可读,可写,可执行对文件的意义比较好理解:

可读就是可以查看,可写就是可修改,可执行就是能执行。

二、chmod

chmod用来修改文件的权限位。

使用数字的方法修改权限(建议使用):

[ruanwenwu@iZ25lzba47vZ ~]$ chmod  test
[ruanwenwu@iZ25lzba47vZ ~]$ ls -ld test/
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test/

使用针对用户的方法:

修改所有用户:

[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-rw-rw-r-- ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test
[ruanwenwu@iZ25lzba47vZ ~]$ chmod a=rwx .txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-rwxrwxrwx ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test

分别写:

[ruanwenwu@iZ25lzba47vZ ~]$ chmod u=rx,g=rwx,o=x .txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-r-xrwx--x ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test

使用增加和减少的办法:

[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-r-xrwx--x ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test
[ruanwenwu@iZ25lzba47vZ ~]$ chmod u+w .txt
[ruanwenwu@iZ25lzba47vZ ~]$ chmod g-w .txt
[ruanwenwu@iZ25lzba47vZ ~]$ ls -l
total
-rwxr-x--x ruanwenwu ruanwenwu Oct : .txt
dr-xr-xr-x ruanwenwu ruanwenwu Oct : test

三、chown

修改文件的所属者或者所属组。

修改文件的所有者:

[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- ruanwenwu root Oct :
[root@iZ25lzba47vZ ~]# chown root
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root root Oct :

修改文件的所属组:

[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root root Oct :
[root@iZ25lzba47vZ ~]# chown :ruanwenwu
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root ruanwenwu Oct :

同时修改所有者和所属组:

[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- root root Oct :
[root@iZ25lzba47vZ ~]# chown ruanwenwu:ruanwenwu
[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- ruanwenwu ruanwenwu Oct :

如果目录下有文件需要修改权限,可以用-R参数,级联修改:

[root@iZ25lzba47vZ ~]# ls -ld
drw-rw-rw- ruanwenwu ruanwenwu Oct :
[root@iZ25lzba47vZ ~]# cd
[root@iZ25lzba47vZ ]# ls [root@iZ25lzba47vZ ]# ls -l
total
drwxr-xr-x root root Oct :
[root@iZ25lzba47vZ ]# cd
[root@iZ25lzba47vZ ]# ls
.txt
[root@iZ25lzba47vZ ]# ls -l
total
-rw-r--r-- root root Mar .txt
[root@iZ25lzba47vZ ]# cd ..
[root@iZ25lzba47vZ ]# cd ..
[root@iZ25lzba47vZ ~]# ls
.ipt Application Document.pdf npm-debug.log ruanwenwu syncwithgit.sh
.cap a.php a.txt iptables.bak oneinstack shellscripts
[root@iZ25lzba47vZ ~]# chown -R ruanwenwu.ruanwenwu
[root@iZ25lzba47vZ ~]# cd
[root@iZ25lzba47vZ ]# ls -l
total
drwxr-xr-x ruanwenwu ruanwenwu Oct :
[root@iZ25lzba47vZ ]# cd
[root@iZ25lzba47vZ ]# ls -l
total
-rw-r--r-- ruanwenwu ruanwenwu Mar .txt

四、umask

umask决定了我们默认建立一个文件或者目录的默认权限。

查看当前umask:

[root@iZ25lzba47vZ ]# umask

那么在当前0022的umask下新建目录或者文件的权限是多少呢?我们看看:

[root@iZ25lzba47vZ ]# umask

[root@iZ25lzba47vZ ]# touch b.txt
[root@iZ25lzba47vZ ]# ls -l b.txt
-rw-r--r-- root root Oct : b.txt
[root@iZ25lzba47vZ ]# mkdir
[root@iZ25lzba47vZ ]# ls -ld
drwxr-xr-x root root Oct :

可见,文件是644,目录是755。那么,这两组权限是怎么算出来的呢?

正确的算法:

目录:rwxrwxrwx 减去 ----w--w- = rwxr-xr-x。

文件没有执行权限,为rw-r--r--所以就是644了。

修改umask:

[root@iZ25lzba47vZ ]# umask
[root@iZ25lzba47vZ ]# umask [root@iZ25lzba47vZ ]# touch .txt
[root@iZ25lzba47vZ ]# ls -l .txt
-rw-rw-r-- root root Oct : .txt

五、lsattr,chattr

lsattr用来查看文件的隐藏权限。

chattr用来修改隐藏权限。

那么隐藏权限可以用来干嘛呢?比方说,你有一个文件谁都不让动,连root用户都不让动,也就是不让修改,删除等任何操作。这个时候就可以用到隐藏权限。常用的隐藏权限有i和a。

i属性的特征是不让修改,删除,也不能touch。a属性的特征是可以追加和touch,别的操作不允许。

添加i属性:

[root@iZ25lzba47vZ ]# lsattr .txt
-------------e-- .txt
[root@iZ25lzba47vZ ]# chattr +i .txt
[root@iZ25lzba47vZ ]# lsattr .txt
----i--------e-- .txt

现在我们尝试对它进行删除,修改,touch操作:

[root@iZ25lzba47vZ ]# lsattr .txt
----i--------e-- .txt
[root@iZ25lzba47vZ ]# touch .txt
touch: cannot touch ‘.txt’: Permission denied
[root@iZ25lzba47vZ ]# cat > .txt
-bash: .txt: Permission denied
[root@iZ25lzba47vZ ]# mv .txt .txt
mv: cannot move ‘.txt’ to ‘.txt’: Operation not permitted
[root@iZ25lzba47vZ ]# ls -l .txt
-rw-rw-r-- root root Oct : .txt

刚才试了文件。如果是对一个目录添加i属性呢?

首先看一下,如何看目录的隐藏属性:

[root@iZ25lzba47vZ ]# lsattr -d
-------------e--
[root@iZ25lzba47vZ ]# lsattr
-------------e-- /
----i--------e-- /.txt
-------------e-- /b.txt
-------------e-- /.txt

发现,加-d属性是看目录的,如果不加-d,可以看到目录下所有文件的隐藏权限。

如果要看到子目录下的隐藏属性呢?

[root@iZ25lzba47vZ ]# lsattr -R
-------------e-- / /:
-------------e-- //b.txt ----i--------e-- /.txt
-------------e-- /b.txt
-------------e-- /.txt

好了,现在我们给目录4添加i属性:

[root@iZ25lzba47vZ ]# chattr +i
[root@iZ25lzba47vZ ]# lsattr -d
----i--------e--
[root@iZ25lzba47vZ ]# ls [root@iZ25lzba47vZ ]# touch .txt
[root@iZ25lzba47vZ ]# mv .txt /
mv: cannot move ‘.txt’ to ‘/.txt’: Permission denied
[root@iZ25lzba47vZ ]# mv /
mv: cannot move ‘’ to ‘/’: Operation not permitted
[root@iZ25lzba47vZ ]# echo >> /b.txt
[root@iZ25lzba47vZ ]# cat /b.txt

通过上面这段代码我们发现,给目录加了i属性后,目录不能重命名,也不能往里面放新的文件。但是,并不妨碍我们修改里面的文件4/b.txt,但是不能删除:

[root@iZ25lzba47vZ ]# rm -rf /b.txt
rm: cannot remove ‘/b.txt’: Permission denied
[root@iZ25lzba47vZ ]# echo >> /b.txt

我们再来看一下a属性:

[root@iZ25lzba47vZ ]# lsattr .txt
-------------e-- .txt
[root@iZ25lzba47vZ ]# chattr +a .txt
[root@iZ25lzba47vZ ]# lsattr .txt
-----a-------e-- .txt
[root@iZ25lzba47vZ ]# touch .txt
[root@iZ25lzba47vZ ]# echo > .txt
-bash: .txt: Operation not permitted
[root@iZ25lzba47vZ ]# echo >> .txt
[root@iZ25lzba47vZ ]# mv .txt .txt
mv: cannot move ‘.txt’ to ‘.txt’: Operation not permitted
[root@iZ25lzba47vZ ]# rm -rf .txt
rm: cannot remove ‘.txt’: Operation not permitted

上面的代码说明,有a属性的文件,同样是不能删除,修改,但是可以追加和touch的。那么对于有a属性的目录呢?

[root@iZ25lzba47vZ ]# lsattr -d
-------------e--
[root@iZ25lzba47vZ ]# chattr +a
[root@iZ25lzba47vZ ]# lsattr -d
-----a-------e--
[root@iZ25lzba47vZ ]# ls
.txt
[root@iZ25lzba47vZ ]# mv .txt /
mv: cannot move ‘.txt’ to ‘/.txt’: Operation not permitted

删除对应的隐藏属性:

[root@iZ25lzba47vZ ]# chattr -a
[root@iZ25lzba47vZ ]# lsattr -d
-------------e--

同样,修改目录的隐藏属性,并不影响我们对目录下面的文件的修改。

linux学习(八)chmod、chown、umask、lsattr、chattr的更多相关文章

  1. 第四节 mount /who / mkdir /rmdir /rm /cp /mv /touch /cat /tac/head /tail /more /less / chmod /chown /umask /chattr /lsattr /history /echo

    ***Linux下的文件类型如下: 9 8 7 6 5 4 3 2 1 0- r w x r - x r - x 第9位表示文件类型,可以为p.d.l.s.c.b和-:p表示命名管道文件 -pipe ...

  2. Linux第四节 组管理、用户管理、权限管理 / chmod /chown / umask / vim

    三期第三讲1.组管理/用户管理(重要文件系统会实时备份 file-) vim/etc/group: 组管理文件://组名:密码控位键:组id:成员 vim/etc/gshadow:组密码管理文件:// ...

  3. linux下的chmod,chown和chgrp

    对于linux的权限掌握以下几个命令就可以非常熟练的操作系统中的各种权限了. 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode f ...

  4. Linux 学习 (八) Shell

    Linux达人养成计划 I 学习笔记 Shell 是什么: Shell 是一个命令解释器 Shell 还是一个功能相当强大的编程语言,易编写,易调试,灵活性较强 Shell 的分类: Bourne S ...

  5. 【Linux学习八】脚本编程

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.多层bash#.和source都是当前bash [root@nod ...

  6. Linux 学习

    远程登录Linux(05) 文本方式远程: putty   SecureCRT  winSCP  SshClient图形方式远程:Xmanager  Xming ifconfigps -ef | gr ...

  7. Linux CentOS7 VMware 文件和目录权限chmod、更改所有者和所属组chown、umask、隐藏权限lsattr/chattr

    一.文件和目录权限chmod u User,即文件或目录的拥有者:g Group,即文件或目录的所属群组:o Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围:a All,即全部 ...

  8. /文件和目录权限chmod /更改所有者和所属组chown/umask/隐藏权限lsattr/chattr

    2.14 文件和目录权限chmod 2.15 更改所有者和所属组chown2.16 umask2.17 隐藏权限lsattr/chattr 文件和目录权限chmod 文件权限: r     4     ...

  9. 文件和目录权限chmod、更改所有者和所属组chown、umask、隐藏权限lsattr/chattr 使用介绍

    第2周第3次课(3月28日) 课程内容:2.14 文件和目录权限chmod2.15 更改所有者和所属组chown2.16 umask2.17 隐藏权限lsattr/chattr 2.14 文件和目录权 ...

  10. linux基础2-cd、mkdir、touch、umask、chattr、lsattr、SUID/SGID/Sticky Bit

    一 cd : . 代表当前目录 .. 代表上一层目录 - 代表前一个工作目录 ~ 代表[目前用户身份]所在的自家目录 与cd效果相同 ~account 代表 account 这个用户的自家家目录 二m ...

随机推荐

  1. geotrellis使用(三十四)矢量瓦片技术研究——矢栅一体化

    前言 本文所涉及技术与Geotrellis并无太大关系,仅是矢量瓦片前端渲染和加载技术,但是其实我这是在为Geotrellis的矢量瓦片做铺垫.很多人可能会说,Geotrellis为什么要搞矢量瓦片, ...

  2. YYHS-NOIP2017Training0921-逆光

    题目描述 有一束光/那瞬间/是什么痛得刺眼/你的视线是谅解/为什么舍不得熄灭/我逆着光却看见/那是泪光/那力量/我不想再去抵挡/面对希望/逆着光/感觉爱存在的地方/一直就在我身旁 Descriptio ...

  3. WebSocket部署服务器外网无法连接解决方案

    首先要说的是我遇见的问题: WebSocket connection to 'ws://www.xxxx.com/xxx/xx' failed: Error during WebSocket hand ...

  4. 原型模式和基于原型继承的js对象系统

    像同样基于原型编程的Io语言一样,javascript在原型继承方面,实现原理和Io非常类似,javascript也遵守这些原则 所有数据都是对象 要得到一个对象,不是通过实例化类,而是找到一个对象作 ...

  5. 关于数据库中datareader的用法

    1.C#中提供的DataReader可以从数据库中每次提取一条数据. using System; using System.Collections.Generic; using System.Comp ...

  6. spring cloud+dotnet core搭建微服务架构:Api网关(三)

    前言 国庆假期,一直没有时间更新. 根据群里面的同学的提问,强烈推荐大家先熟悉下spring cloud.文章下面有纯洁大神的spring cloud系列. 上一章最后说了,因为服务是不对外暴露的,所 ...

  7. 深入理解C# 静态类与非静态类、静态成员的区别 [转载]

    静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 new 关键字创建静态类类型的变量.在声明一个类时使用static关键字,具有两个方面的意义:首先,它防止程序员写代码来实例 ...

  8. springMVC中的redirect和forward区别?

    1.forward在跳转后可以取到message值,redirect在跳转后无法取到message值. 2.forward跳转后地址栏URL不会改变,而redirect会改变.

  9. Python自学笔记-Django分页器小实例

    from django.core.paginator import Paginator iter = 'abcdefhijklmnopqw' paginator = Paginator(iter,4) ...

  10. ThinkJS框架入门详细教程(二)新手入门项目

    一.准备工作 参考前一篇:ThinkJS框架入门详细教程(一)开发环境 安装thinkJS命令 npm install -g think-cli 监测是否安装成功 thinkjs -v 二.创建项目 ...