一、权限位

权限位分为两个部分,第一个部分是谁的权限,第二部分是权限是多少。其中第一个部分一般分为:用户,用户组,其他用户。第二部分分为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. 第4章 同步控制 Synchronization ----Interlocked Variables

    同步机制的最简单类型是使用 interlocked 函数,对着标准的 32 位变量进行操作.这些函数并没有提供"等待"机能,它们只是保证对某个特定变量的存取操作是"一个一 ...

  2. nodejs中http-proxy使用小结

    最近在写xmocker的工具,用于开发前期的mock数据,不可避免的用到了代理的中间件.当然,github上有关于http-proxy封装的中间件.毕竟是自己练手的项目,就自己写了个中间件,方便定制功 ...

  3. 【Linux笔记(002) 】-- centos7 文档操作基本命令

    一.cd -- ChangeDirectory a) 切换到 /DemoLM/ 文件夹 b) 回到用户 Home 根目录:是哪个账户登录的就会进入哪个用户的根目录 二.pwd -- PrintWork ...

  4. 机器学习实战K-近邻算法

    今天开始学习机器学习,第一章是K-近邻算法,有不对的地方请指正 大概总结一下近邻算法写分类器步骤: 1. 计算测试数据与已知数据的特征值的距离,离得越近越相似 2. 取距离最近的K个已知数据的所属分类 ...

  5. js循环生成多个easyui datagrid数据网格时,初始化表格

    $.each( content, function(i, item){ var info_tpl = "";var result_tpl = "";var pr ...

  6. dom4j之小小工具

    dom4j经常不用,方法忘了又记,故做出读取xml和把document写入xml的小小工具~~~ /** * 读取document和将document对象写入到xml的小工具 * 使用该类必须给出do ...

  7. Django安装Xadmin步骤

    在Django中安装Xadmin替换原始的admin,下面介绍两种方法安装 第一种方法:pip安装 第一步: 直接pip安装xadmin pip install xadmin pip会同时安装上面三个 ...

  8. RoportNG报表显示中文乱码和TestNG显示中文乱码实力解决办法

    最近在进军测试自动化框架学习阶段,但无意间总是会伴随小问题的困扰,比如中文乱码,而导致显示总是不舒服,个人觉得,就一定要解决,似乎有点点强迫症.所以遇到RoportNG报表显示中文乱码和TestNG显 ...

  9. Inno Setup打包注意事项

    Inno Setup是一个开源的,商业的,快捷的脚本打包工具. 具体打包流程根据界面提示就可以搞定,下面讲解几个注意事项 1.在安装包进行安装的过程当中,很多程序都需要修改配置信息,这就要求我们在安装 ...

  10. win10 uwp 按下等待按钮

    我们经常需要一个按钮,在按下时,后台执行Task,这时不能再次按下按钮. 我们使用自定义控件,首先新建一个类,我把它命名是ProgressButton 一个进度条按钮,也就是我们按下时发生进度条,完成 ...