Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141611
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 770
  • 用 户 组: 普通用户
  • 注册时间: 2017-11-04 11:19
文章分类

全部博文(70)

文章存档

2018年(69)

2016年(1)

我的朋友

分类: LINUX

2018-06-04 22:49:43

2.14文件或目录权限chmod
之前讲的"ls -l"命令中,展示的内容第一列:
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 1483 6月   2 22:24 anaconda-ks.cfg
-rw-r--r--. 1 root root    0 6月   4 21:14 a.txt
第一列的第一位指定文件类型,这部分之前的内容中说过了,后面的九位就是指定相关用户的相关权限了。最后的那个点,表示该文件受制于selinux。(如果开启了selinux,那么创建的文件或目录这就会有这个点,受制于selinux。想要关闭的话,需要修改配置文件/etc/selinux/conf中SELINUX=disabled,然后重启计算机,即可生效。)
第二列硬链接个数。第三列属主。第四列属组。第六列文件大小等等

回到权限:
第一列后面九位权限是每三位(rwx)一组,r=可读,w=可写,x=可执行。
第一组是属主的权限,第二组是属组的权限,第三组是其他用户的权限
eg:
-rw-r--r--. 1 root root    0 6月   4 21:14 a.txt
它是一个普通文件,属主root,对应权限可读可写不可执行;属组root,对应权限可读不可写不可执行;其他用户只可读不可写不可执行

权限还可以用数字表示:r=4,w=2,x=1。上面的例子a.txt文件的权限:644

命令chmod:change mode修改权限的命令
修改a.txt的权限为700
[root@localhost ~]# chmod 700 a.txt 
[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 1483 6月   2 22:24 anaconda-ks.cfg
-rwx------. 1 root root    0 6月   4 21:14 a.txt
这里的使用,其实之前在密钥认证中使用过的~

chmod选项:
-R:递归的修改目录中所有的文件权限

[root@localhost ~]# ls -l a
总用量 0
-rw-r--r--. 1 root root 0 6月   4 21:36 1.txt
-rw-r--r--. 1 root root 0 6月   4 21:36 b.sh
[root@localhost ~]# chmod -R 700 a
[root@localhost ~]# ls -ld a
drwx------. 2 root root 31 6月   4 21:36 a
[root@localhost ~]# ls -l a                       //查看到目录及目录下所有文件批量都被修改了权限。
总用量 0
-rwx------. 1 root root 0 6月   4 21:36 1.txt
-rwx------. 1 root root 0 6月   4 21:36 b.sh

另一种使用形式:
[root@localhost ~]# chmod u=rwx,g=r,o=r a    //只把你需要的权限写上,不可以使用g=r-x
[root@localhost ~]# ll -d a
drwxr--r--. 2 root root 31 6月   4 21:36 a

[root@localhost ~]# chmod u-x a            //也可以单独针对某种用户进行单独的修改,这里是减去属主的可执行权限
[root@localhost ~]# ll -d a
drw-r--r--. 2 root root 31 6月   4 21:36 a

[root@localhost ~]# chmod o+x a    //给其他用户添加可执行权限。
[root@localhost ~]# ll -d a
drw-r--r-x. 2 root root 31 6月   4 21:36 a


2.15 更改所有者和所属组 chown
chown:change owner更改所有者和所属组

查看系统用户都有哪些,可以使用下面命令查看
[root@localhost ~]# cat /etc/passwd
当中会有很多系统本身的用户,不要去修改

[root@localhost ~]# ll -d a
drw-r--r-x. 2 root root 31 6月   4 21:36 a
[root@localhost ~]# chown xiao a            //修改文件的属主
[root@localhost ~]# ll -d a
drw-r--r-x. 2 xiao root 31 6月   4 21:36 a
[root@localhost ~]# chown .xiao a        //修改文件的属组,前面是个”.“点
[root@localhost ~]# ll -d a
drw-r--r-x. 2 root xiao 31 6月   4 21:36 a    //还可以使用前面用":冒号
[root@localhost ~]# chown :root a
[root@localhost ~]# ll -d a
drw-r--r-x. 2 root root 31 6月   4 21:36 a

[root@localhost ~]# chown xiao:xiao a        //可以同时修改属主属组,中间用冒号分隔
[root@localhost ~]# ll -d a
drw-r--r-x. 2 xiao xiao 31 6月   4 21:36 a


修改属组:chgrp=change group
[root@localhost ~]# chgrp xiao a
[root@localhost ~]# ll -d a
drw-r--r-x. 2 xiao xiao 31 6月   4 21:36 a        //修改了文件的属组

前面学习的chmod有选项递归修改,这里的属主属组也可以使用递归修改
-R:递归修改

[root@localhost ~]# ll a
总用量 0
-rwx------. 1 root root 0 6月   4 21:36 1.txt
-rwx------. 1 root root 0 6月   4 21:36 b.sh
[root@localhost ~]# chown -R xiao:xiao a
[root@localhost ~]# ll a
总用量 0
-rwx------. 1 xiao xiao 0 6月   4 21:36 1.txt
-rwx------. 1 xiao xiao 0 6月   4 21:36 b.sh


2.16 umask
问题:通过touch创建一个文件,会有一个默认权限644。使用mkdir创建一个目录,会有默认权限755。那么为什么默认权限是这样的呢?

因为系统有个概念,就是umask
[root@localhost ~]# umask                   
0022                                                    //当前登陆用户的umask值

通过它就可以知道创建文件目录的默认权限



[root@localhost ~]# umask   //这里是默认umask是0022时创建的1.txt文件和1目录,及权限
0022
[root@localhost ~]# touch 1.txt
[root@localhost ~]# mkdir 1
[root@localhost ~]# ls -l
总用量 4
drwxr-xr-x. 2 root root    6 6月   4 22:06 1
-rw-r--r--. 1 root root    0 6月   4 22:06 1.txt
drwxr-xr-x. 2 root root    6 6月   4 22:05 a
-rw-r--r--. 1 root root    0 6月   4 22:05 a.txt

[root@localhost ~]# umask 0002      //当我们把umask修改之后,创建2.txt和目录2进行全乡对比
[root@localhost ~]# touch 2.txt        //上面修改umask时,第一位的0可以省略
[root@localhost ~]# mkdir 2
[root@localhost ~]# ls -l
总用量 4
drwxr-xr-x. 2 root root    6 6月   4 22:06 1
-rw-r--r--. 1 root root    0 6月   4 22:06 1.txt
drwxrwxr-x. 2 root root    6 6月   4 22:06 2
-rw-rw-r--. 1 root root    0 6月   4 22:06 2.txt
drwxr-xr-x. 2 root root    6 6月   4 22:05 a
-rw-r--r--. 1 root root    0 6月   4 22:05 a.txt

总结:
目录一定要有x权限,因为目录需要执行权限用来查看目录当中的文件(查看当中的文件就需要执行权限)
而文件不同,文件可以没有x权限进行去读,有x权限是为了执行当中的程序的。

所以这个我们得出结论:
目录权限:777 - umask = 最终创建目录时的权限777-002=775
文件权限:666 - umask = 最终创建文件时的权限666-002=664
当然也不是所有的都可以这样表示:比如003 (666-003=663)
[root@localhost ~]# umask
0003
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ls -l 1.txt 
-rw-rw-r--. 1 root root 0 6月   4 22:15 1.txt
[root@localhost ~]# 666(rw-rw-rw-) - (-------wx) -> rw-rw-r--=664 //目录也是按照这样的算法
[root@localhost ~]# mkdir 1
[root@localhost ~]# ll
总用量 4
drwxrwxr--. 2 root root    6 6月   4 22:16 1


2.17 隐藏权限  lsattr_chattr
设置隐藏权限的命令:chattr
chattr - change file attributes on a Linux file system            //附加权限

[root@localhost ~]# chattr +i 1.txt
[root@localhost ~]# ll
总用量 4
drwxrwxr--. 2 root root    6 6月   4 22:16 1
-rw-rw-r--. 1 root root    0 6月   4 22:15 1.txt
-rw-------. 1 root root 1483 6月   2 22:24 anaconda-ks.cfg
[root@localhost ~]# head -n2 /etc/passwd > 1.txt
bash: 1.txt: 权限不够         //如果遇到本身权限可以写入,却无法写入的情况,需要查看隐藏权限

[root@localhost ~]# lsattr 1.txt
----i----------- 1.txt
[root@localhost ~]# touch 2.txt
[root@localhost ~]# lsattr 2.txt
---------------- 2.txt                        //不修改隐藏文件,那么是没有那个”i“的
[root@localhost ~]# mv 1.txt a.txt
mv: 无法将"1.txt" 移动至"a.txt": 不允许的操作
[root@localhost ~]# rm -rfv 1.txt        //不止写入,删除改名的权限都没有了
rm: 无法删除"1.txt": 不允许的操作
[root@localhost ~]# touch 1.txt            //创建时间修改也不可以
touch: 无法创建"1.txt": 权限不够

这是个特别严谨的权限:对于一个不需要任何人做任何修改的文件,就可以使用这个隐藏权限。

[root@localhost ~]# chattr -i 1.txt                //删除隐藏权限把之前的”+“换成”-“,如此修改即可
[root@localhost ~]# mv 1.txt 3.txt
[root@localhost ~]# ls
1  2.txt  3.txt  anaconda-ks.cfg

[root@localhost ~]# chattr +a 1.txt     //这里”a“权限:允许在文件末尾进行追加。touch也可以
[root@localhost ~]# lsattr 1.txt
-----a---------- 1.txt

[root@localhost ~]# lsattr 1
[root@localhost ~]# mkdir 1/2
[root@localhost ~]# lsattr 1    //查看了目录下的子目录子文件,无法查看当前目录
---------------- 1/2
[root@localhost ~]# lsattr -d 1    //通过”-d“选项查看,当前目录隐藏权限
---------------- 1
[root@localhost ~]# chattr +i 1    //跟文件添加”i“权限是一样。但该目录下已经存在的文件可以被修改。
目录添加隐藏”a“权限,可以创建文件,并且修改权限等

[root@localhost ~]# lsattr -R 1    //递归列出子文件子目录的隐藏权限。
[root@localhost ~]# chattr -R +i 1    //同理设置也可以。
[root@localhost ~]# lsattr -R 1
----i----------- 1/2
1/2:
----i----------- 1/a.txt
----i----------- 1/b.txt
[root@localhost ~]# lsattr -aR 1        //”-a“隐藏文件的隐藏选项。
----i----------- 1/.
---------------- 1/..
----i----------- 1/2
1/2:
----i----------- 1/2/.
----i----------- 1/2/..
----i----------- 1/a.txt
----i----------- 1/b.txt

阅读(884) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~