2.14 文件和目录权限chmod

2.15 更改所有者和所属组chown

2.16 umask

2.17 隐藏权限lsattr/chattr

2.14 文件和目录权限chmod:

8!.更改权限 更改所有者所属组 umask 隐藏权限lsattr chattr-LMLPHP

-表示类型

往后的9位表示它的权限,每3位代表一个

第一段表示所有者的权限

第二段表示所属组的权限

第三段表示除了所有者所属组之外,其他的用户的权限

后面的“.”表示受制于selinux(防火墙),目前做了解就好

也可用数字表示r(是否可读)=4  w(是否可写)=2  x(是否可执行)=1

所以,rwx=7(就代表可读、可写、可执行)rw-=6(就代表可读、可写)--x=1(就代表可执行)

那么rw- r-- r--就=644

chmod表示更改权限,只更改于这个文件本身

chmond -R表示把目录本身和目录下的目录和文件批量的更改权限

比如把一个文件更改为rwx --- ---那么对应的数字就是700

所以写法为chmod 700 

也可以这样写

8!.更改权限 更改所有者所属组 umask 隐藏权限lsattr chattr-LMLPHP

u代表所有者,g代表所属组,o代表其他,不可加“-”

也可以

[root@afeilinux-01 test1]# chmod a+x 1.sh
[root@afeilinux-01 test1]# ls
1.sh
[root@afeilinux-01 test1]# ls -l
总用量 4
-rwxr-xr-x. 1 root root 288 9月   3 16:53 1.sh

chmod a+x表示所有搜加上x,或者a-x

u g o也可以+或者-

---------------------------------------------------------------------------------------------------------------------------------------

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

chown可更改所有者

比如,将一个文件改到aming这个用户下,写法为:

chown aming /()该文件的绝对路径)

chgrp可更改所属组,写法是一样的

其实chown也可以更改所有者和所属组,写法为

chown user1:aming /(该文件的路径)        中间加个“:”即可

[root@afeilinux-01 ~]# cd test1
[root@afeilinux-01 test1]# ls -l 1.sh
-rwxr-xr-x. 1 root root 288 9月   3 16:53 1.sh
[root@afeilinux-01 test1]# chown user1:aming /root/test1/1.sh
[root@afeilinux-01 test1]# ls -l 1.sh
-rwxr-xr-x. 1 user1 aming 288 9月   3 16:53 1.sh

当然,chown也可以改所属组,写法为:

chown :root /(该文件的绝对路径)      加个“:”即可,意思是将“:”前的所有者省略就是更改了所属组

[root@afeilinux-01 test1]# chown :root 1.sh   :前有空格
[root@afeilinux-01 test1]# ls -l 1.sh
-rwxr-xr-x. 1 user1 root 288 9月   3 16:53 1.sh

chown也可以使用-R(表示把目录本身和目录下的目录和文件批量的更改权限)

[root@afeilinux-01 test1]# chown -R :aming /root/test1/
[root@afeilinux-01 test1]# ls -l
总用量 4
-rwxr-xr-x. 1 user1 aming 288 9月   3 16:53 1.sh
-rw-r--r--. 1 root  aming   0 9月  17 11:30 test3
-rw-r--r--. 1 root  aming   0 9月  17 11:30 test4

---------------------------------------------------------------------------------------------------------------------------------------

2.16 umask:

umask默认目录和文件的权限的,这个umask值是可以改写的

root下umask值是022,umask 命令可直接改写umask值

文件要4(可读),目录要5(可读执可行)因为只有进入到目录才能查看,所以要可执行。

因此目录为777,文件为666

那怎么判断你的默认权限是多少?用减法为:

目录的权限算法是:777-umask值 rwxrwxrwx减----w--w-=rwxr-xr-x,所以目录的默认权限是755

文件的权限算法是:666-umask值 rw-rw-rw-减----w--w-=rw-r--r--,所以文件的默认权限是644

需注意的是,如果rw-减--x=rw-(没有- 减 有x就等于没有)

---------------------------------------------------------------------------------------------------------------------------------------

2.17 隐藏权限lsattr/chattr:

chattr设置隐藏权限

lsattr查看隐藏权限

chattr +i 增加该属性 表示文件不能删除 重命名 设定链接 写入以及新增数据

如果用lsattr查看一个隐藏文件,就会提示i权限

[root@afeilinux-01 test1]# chattr 1.sh
Must use '-v', =, - or +
[root@afeilinux-01 test1]# lsattr 1.sh
---------------- 1.sh
[root@afeilinux-01 test1]# chattr +i 1.sh
[root@afeilinux-01 test1]# lsattr 1.sh
----i----------- 1.sh
[root@afeilinux-01 test1]# rm 1.sh
rm:是否删除普通文件 "1.sh"?y
rm: 无法删除"1.sh": 不允许的操作
[root@afeilinux-01 test1]# rm -f 1.sh
rm: 无法删除"1.sh": 不允许的操作

此时无法对这个文件做任何操作

可以用chattr -i来取消这个文件的隐藏权限

chattr +a a 增加该属性 表示只能追加不能删除 非root用户不能设定该属性

chattr -a取消这个文件的隐藏属性

以上操作等同于目录

[root@afeilinux-01 test1]# chattr +a 1.sh
[root@afeilinux-01 test1]# lsattr 1.sh
-----a---------- 1.sh
[root@afeilinux-01 test1]# rm -f 1.sh
rm: 无法删除"1.sh": 不允许的操作
[root@afeilinux-01 test1]# chattr -a 1.sh
[root@afeilinux-01 test1]# lsattr 1.sh
---------------- 1.sh

-------

lsattr -R可查看目录以及子目录下的文件

lsattr -a连同隐藏文件一起列出

lsattr -d查看目录本身

[root@afeilinux-01 test1]# lsattr -R /root/test1/
---------------- /root/test1/1.sh
---------------- /root/test1/test3
---------------- /root/test1/test4
[root@afeilinux-01 test1]# lsattr -a /root/test1/
---------------- /root/test1/.
---------------- /root/test1/..
---------------- /root/test1/1.sh
---------------- /root/test1/test3
---------------- /root/test1/test4
[root@afeilinux-01 test1]# lsattr -d /root/test1/
---------------- /root/test1/
09-17 18:42